在評估用戶權限時尋找一種非常簡單的方法來檢查檔案/目錄是否存在,回傳不同的(代碼)錯誤:
有命令test檢查權限,但無法為檔案不存在的情況提供更好的回傳碼:
$ test -r /tmp/; echo $? # 0
$ test -r /tmp/root/; echo $? # 1
$ test -r /tmp/missing/; echo $? # 1
我正在尋找類似于ls我因不同錯誤收到不同訊息的地方:
$ ls /tmp/root
ls: root: Permission denied
$ ls /tmp/missing
ls: /tmp/missing: No such file or directory
我喜歡這種差異化,但1兩者都有錯誤代碼。為了正確處理每個錯誤,我必須決議stderr這確實是一個非常不優雅的解決方案。
難道沒有更好更優雅的方式來做到這一點嗎?
接近某種pythonic方式的東西看起來像這樣:
import os
os.listdir("/tmp/root/dir/") # raises PermissionError
os.listdir("/tmp/foo/") # raises FileNotFoundError
uj5u.com熱心網友回復:
多閱讀手冊。還有-d專門檢查目標是否是目錄,以及大量其他謂詞來檢查符號鏈接、設備節點等。
testthing () {
if ! [[ -e "$1" ]]; then
echo "$1: not found" >&2
return 2
elif ! [[ -d "$1" ]]; then
echo "$1: not a directory" >&2
return 4
elif ! [[ -r "$1" ]]; then
echo "$1: permission denied" >&2
return 8
fi
return 0
}
用法:
testthing "/root/no/such/directory"
請注意,這[[是一個內置的 Bash,它比傳統的[aka更加健壯和通用test。
很難預測優先級應該是什么,但是如果您希望以不同的順序進行比較,請務必這樣做。當缺少對父目錄的讀取訪問權限時,shell 無法正確判斷目錄條目的準確狀態是不可避免的。也許可以通過從根目錄開始檢查路徑中每個條目的存在和權限來從呼叫者那里解決這個問題。
uj5u.com熱心網友回復:
shell 和標準實用程式不提供執行您似乎想要的所有操作的命令:
- 通過單個命令執行,
- 以退出狀態終止,詳細報告給定路徑的存在和可訪問性,
- 為當前用戶情境化,
- 即使在最后一個路徑元素之前的目錄不可遍歷的情況下也準確無誤(注意:無論如何你都不能擁有這個),
- (也許)對目錄和常規檔案都正確。
os.listdir()即使您排除了對常規檔案的適用性和遍歷不可遍歷的目錄,并重新解釋“退出狀態”的含義,Python也不會做所有這些。然而,os.listdir()兩者ls都展示了一種很好且有用的模式:嘗試所需的操作并處理任何導致的失敗,而不是試圖預測如果你嘗試它會發生什么。
此外,不清楚你為什么想要你所說的你想要的。我想獲得有關檔案訪問失敗原因的資訊的主要原因是用戶訊息傳遞,但在大多數情況下,您只需嘗試執行所需的訪問即可免費獲得。如果您將其排除在外,那么為什么給定路徑無法訪問就無關緊要了。無論如何,您需要切換到替代方案或失敗。
如果您仍然確實想要盡可能多地完成上述操作,那么您可能不得不自己動手。由于您在某些評論中表達了對效率的關注,您可能希望在 C 中這樣做。
uj5u.com熱心網友回復:
使用示例。
for d in 1 2 3; do
if [[ -e $d ]]; then printf "%s exists" $d
[[ -r $d ]] && echo " and is readable" || echo " but is not readable"
else echo "$d does not exist"
fi
stat -c "%A %n" $d
done
1 exists and is readable
drwxr-xr-x 1
2 exists but is not readable
d--------- 2
3 does not exist
stat: cannot stat ‘3’: No such file or directory
如果您絕對必須在一個步驟中使用不同的退出代碼,請撰寫一個函式。
doubletest() { if [[ -e "$1" ]]; then [[ -r "$1" ]] && return 0 || return 2; else return 1; fi; }
result=( "exists and is readable" "exists but is unreadable" "does not exist" )
for d in 1 2 3; do doubletest $d; echo "$d ${result[$?]}"; done
1 exists and is readable
2 does not exist
3 exists but is unreadable
uj5u.com熱心網友回復:
鑒于:
$ ls -l
total 0
-rw-r--r-- 1 andrew wheel 0 Mar 22 12:01 can_read
---xr-x--x 1 andrew wheel 0 Mar 22 12:01 root
drwxr-xr-x 2 andrew wheel 64 Mar 22 13:09 sub
請注意,user前三個、group后三個和other或world后三個的權限是 by。
Permission Denied錯誤來自 1) 嘗試在沒有為您的用戶或組設定適當權限位的情況下讀取或寫入檔案或 2) 試圖導航到未x設定的目錄或 3) 嘗試在沒有適當權限的情況下執行檔案。
您可以通過測驗測驗用戶是否可以讀取檔案-r:
$ [[ -r root ]] && echo 'readable' || echo 'not readable'
not readable
因此,如果您只關心用戶權限,-r那么-w和-xtest 就是您要尋找的。
如果要一般測驗權限,則需要使用stat.
這是具有相同目錄的簡單示例:
#!/bin/bash
arr=(can_read root sub missing)
for fn in "${arr[@]}"; do
if [[ -e "$fn" ]]
then
p=( $(stat -f "%SHp %SMp %SLp" "$fn") )
printf "File:\t%s\nUser:\t%s\nGroup:\t%s\nWorld:\t%s\nType:\t%s\n\n" "$fn" "${p[@]}" "$(stat -f "%HT" "$fn")"
else
echo "\"$fn\" does not exist"
fi
done
印刷:
File: can_read
User: rw-
Group: r--
World: r--
Type: Regular File
File: root
User: --x
Group: r-x
World: --x
Type: Regular File
File: sub
User: rwx
Group: r-x
World: r-x
Type: Directory
"missing" does not exist
或者,您可以直接從drwxr-xr-x型別資料中獲取這些值:
for fn in "${arr[@]}"; do
if [[ -e "$fn" ]]
then
p=$(stat -f "%Sp" "$fn")
typ="${p:0:1}"
user="${p:1:3}"
grp="${p:4:3}"
wrld="${p:7:3}"
else
echo "\"$fn\" does not exist"
fi
done
In either case, you can then test the individual permissions with either Bash string functions, Bash regex, or get the octal equivalents and use bit masks.
Here is an example:
for fn in "${arr[@]}"; do
if [[ -e "$fn" ]]
then
p=$(stat -f "%Sp" "$fn")
user="${p:1:3}"
ty="$(stat -f "%HT" "$fn")"
printf "%s \"$fn\" is:\n" "$ty"
[[ $user =~ 'r' ]] && echo " readable" || echo " not readable"
[[ $user =~ 'w' ]] && echo " writeable" || echo " not writeable"
[[ $user =~ 'x' ]] && echo " executable" || echo " not executable"
else
echo "\"$fn\" does not exist"
fi
done
Prints:
Regular File "can_read" is:
readable
writeable
not executable
Regular File "root" is:
not readable
not writeable
executable
Directory "sub" is:
readable
writeable
executable
"missing" does not exist
(Note: stat tends to be platform specific. This is BSD and Linux will have different format strings...)
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/447221.html
上一篇:我可以在Linux上創建一個回圈緩沖區嗎?(當前代碼段錯誤)
下一篇:使用非物體類作為原生查詢的結果類
