2017-10-20 99 views
4

我需要得到Access rights inhuman readable的文件或文件夾格式與symbolic符號這樣u=rwx,g=srwx,o-rwx(可能粘位)UNIX權限轉換器在象徵性的符號(包括粘滯位)

  • 使用stat --format '%a',我獲得結果與格式2770,八進制格式
  • 使用stat --format '%A',我獲得具有格式drwxrws---結果,人類可讀

我需要一個命令,獲取格式像u=rwx,g=srwx,o-rwx(與chmod symbolic modes兼容)

  • [u|g|o]:爲user/group/othera所有
  • [=]:是理所當然的權利
  • [rwxst] :授予的權利列表無重要性
  • [-rwx]:右撤銷(如果沒有權利授予)

我曾經嘗試這樣做(但它不能處理所有的情況下,specialy粘位):

stat --format '%A' temp | 
    sed -E 's/^.(...)(...)(...)/u=\1,g=\2,o=\3/g' | # split by triplet 
    sed 's/=---/-rwx/g' | # revoker grants 
    sed 's/rws/srwx/g' | # setgid with x ... 
    sed 's/--S/s/g'  | # setgid without x ... 
    sed ... nead more transormation... # manage sticky bit setuid setgid 

我尋找一個更優雅的方式。

例如輸入==>輸出

  • drwxrws--- ==>u=rwx,g=srwx,o-rwx(由d ==>目錄開始)
  • drwxrwxrwx ==>u=rwx,g=rwx,o=rwxugo=rwxa=rwx
  • -r-xrw-r-- ==>u=rx,g=rw,o=r (開始 - ==>常規文件)
  • -rwx--S--- ==>u=rwx,g=s,o-rwx(S大寫)
  • ------s--t ==>u=-srwx,g=sx,o=xt(粘滯位)

輸入格式==>像命令statls -al
輸出格式==>必須與chmod

這個完整的版本似乎作品兼容,但我相信我們可以簡化它,(即沒有多個sed)

stat --format '%A' a | 
sed -E 's/^.(...)(...)(...)/u=\1,g=\2,o=\3/g' | # split by triplet  
sed -E 's/s/xs/g'   | # setgid ou setuid with x ... 
sed -E 's/t/xt/g'   | # sticky bit with x ...  
sed -E 's/S/s/g'   | # setgid ou setuid without x ... 
sed -E 's/T/t/g'   | # sticky bit alone 
sed -E 's/-//g'   | # remove - 
sed -E 's/=(,|$)/-rwx\1/g' # revoker grants 
+1

爲什麼downvote?請解釋。 – Indent

+1

因爲你要求別人寫你的代碼而不費吹灰之力。 – 123

+2

好的,我已經更新了我的臨時解決方案(我沒有發佈這個不影響答案) – Indent

回答

2

sed命令採用多種-e 'sed-command'選項,所以它是微不足道的解決您的代碼,以便它使用的sed一個調用:

stat --format '%A' a | 
sed -E -e 's/^.(...)(...)(...)/u=\1,g=\2,o=\3/g' \ 
     -e 's/s/xs/g' \ 
     -e 's/t/xt/g' \  
     -e 's/S/s/g' \ 
     -e 's/T/t/g' \ 
     -e 's/-//g' \ 
     -e 's/=(,|$)/-rwx\1/g' 

不能使用後的意見,但這是其它方面與什麼你表現出來。您的s/s/xs/g操作與您所描述的輸出不符(您顯示sx而不是xs,表示具有組執行權限的SGID)。有些人會用分號分隔表達式,將所有這些選項變成一個-e選項。我更喜歡使用-e來分隔不同的選項。有時候-f script.sed選項從文件中讀取腳本是明智的。我不認爲這個腳本已經達到了這個門檻,但是不要忘記這個選項存在。

美麗是在旁觀者的眼中。我不相信權限的替代表示比正常的更好,但也許我剛剛使用Unix太久了。

+0

感謝您的建議,我已經知道sed的語法已經不同了。我已經使用'多個'命令來添加簡單的評論。對於'xs'vs'sx',順序並不重要。對於替代格式的選擇,我認爲當我們使用粘性位時,'符號'語法更加清晰,大寫'S'和'T'對's'和't'對我來說是一種破解,因爲最初unix不管理粘性位。我會完成我的問題來詳細說明存在的理由。 – Indent

2

最後更簡潔的命令,我發現是:

stat --format '%A' myFile | sed -E -e 's/(...)(...)(...)$/u=\1,g=\2,o=\3/;s/(s|t)/x\1/g;s/(S|T)/\l\1/g;s/-//g;s/(u|g)=,/\1-rwxs,/g;s/o=$/o-rwxt/g' 

我寧願找一個本地命令

可以肯定我寫了詳盡的bash測試的所有組合權(8 * 8 * 8 * 8 = 4096錶殼!)

test_file=/tmp/a 

return_code=0 

echo -e "from itertools import product;\nfor mod in product('', repeat=4) : print ''.join(mod)" | python | shuf | 
while read octalMod 
do 
    chmod $octalMod $test_file # apply octal mod 

    # get symbolic mod 
    symbolicMod=$(stat --format '%A' $test_file | sed -E -e 's/(...)(...)(...)$/u=\1,g=\2,o=\3/;s/(s|t)/x\1/g;s/(S|T)/\l\1/g;s/-//g;s/(u|g)=,/\1-rwxs,/g;s/o=$/o-rwxt/g') 

    chmod 0000 $test_file   # reset mod 
    chmod $symbolicMod $test_file # apply symbolic mod 

    modActual=$(stat --format '%a' $test_file) # get actual mod in octal format to compare it with the original 
    if [ $octalMod -ne $modActual ] 
    then 
    echo "$octalMod ($symbolicMod) !!! octalMod=$octalMod <> modActual=$modActual" >&2 
    return_code=255 
    else 
    echo "$octalMod ($symbolicMod)  octalMod=$octalMod == modActual=$modActual" >&1 
    fi 

done 
exit $return_code 

完整列表:

test_file=/tmp/a 

echo -e "octalMod\thumanMod\tsymbolicMod" 

echo -e "from itertools import product;\nfor mod in product('', repeat=4) : print ''.join(mod)" | python | 
while read octalMod 
do 
    chmod $octalMod $test_file # apply octal mod 

    # get symbolic mod 
    symbolicMod=$(stat --format '%A' $test_file | sed -E -e 's/(...)(...)(...)$/u=\1,g=\2,o=\3/;s/(s|t)/x\1/g;s/(S|T)/\l\1/g;s/-//g;s/(u|g)=,/\1-rwxs,/g;s/o=$/o-rwxt/g') 
    humanMod=$(stat --format '%A' $test_file) 

    echo -e "$octalMod\t$humanMod\t$symbolicMod" 

done 

echo -e "octalMod\thumanMod\tsymbolicMod" 

樣品結果:

octalMod  humanMod  symbolicMod 
0001 ---------x  u-rwxs,g-rwxs,o=x 
0354 --wxr-xr--  u=wx,g=rx,o=r 
1210 --w---x--T  u=w,g=x,o=t 
3337 --wx-wsrwt  u=wx,g=wxs,o=rwxt 
3566 -r-xrwSrwT  u=rx,g=rws,o=rwt 
3734 -rwx-wsr-T  u=rwx,g=wxs,o=rt 
6734 -rws-wsr--  u=rwxs,g=wxs,o=r 
7121 ---s-wS--t  u=xs,g=ws,o=xt 
7430 -r-S-ws--T  u=rs,g=wxs,o=t 
7611 -rwS--s--t  u=rws,g=xs,o=xt