簡體   English   中英

將文件的權限(八進制)與 Bash Shell 中的 integer 進行比較

[英]Comparing the permissions(octal) of files with an integer in Bash Shell

在我的 shell 腳本中,我遇到了一些麻煩。 直到現在我都有這個代碼。

echo "Give directory name"
read  dirname;
if [ -d "$dirname" ]; then
    for filename in "$dirname"/*
    echo "Files found: $(find "$dirname" -type f | wc -l)"
    do
        if [ $(stat -f "%a" "$filename") == "$first" ]; then
        echo "Files with ($first) permission is: $filename"
        fi
    done

fi

當我在終端上運行它時,我可以看到我的計算機要求訪問(這意味着我到目前為止做得很好。整個想法是我搜索文件的權限,並將八進制系統中的這個權限與一個給定數字($first)。最后,它什么也沒顯示,腳本從頭開始循環。

任何幫助都會很棒。

您可以通過以下方式替換此腳本:

#!/usr/bin/env bash

read -r -p $'Give directory name:\n' dirname
if [ -d "$dirname" ]
then
    read -r -p $'Give expected octal permissions:\n' first
    mapfile -d '' -t files < <(
      find "$dirname" -maxdepth 1 -type f -perm "$first" -print0
    )
    if [ "${#files[@]}" -gt 0 ]
    then
      printf 'Found: %d files with the (%s) permission in %s:\n' "${#files[@]}" "$first" "$dirname"
      printf '%s\n' "${files[@]}"
    else
      printf 'Found no file with the (%s) permission in %s\n' "$first" "$dirname"
    fi
fi

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM