所以我有這個腳本,我試圖確定檔案的型別并采取相應的行動,我正在使用 file 命令確定檔案的型別,然后使用 grep 查找特定的字串,例如,如果檔案被壓縮然后解壓縮,如果它gzipped 然后 gunzip 它,我想添加很多不同型別的檔案。
我正在嘗試將這些if陳述句替換為case但無法弄清楚
我的腳本如下所示:
##$arg is the file itself
TYPE="$(file $arg)"
if [[ $(echo $TYPE|grep "bzip2") ]] ; then
bunzip2 $arg
elif [[ $(echo $TYPE|grep "Zip") ]] ; then
unzip $arg
fi
感謝所有幫助的人:)
uj5u.com熱心網友回復:
一般語法是
case expr in
pattern) action;;
other) otheraction;;
*) default action --optional;;
esac
所以對于你的片段,
case $(file "$arg") in
*bzip2*) bunzip2 "$arg";;
*Zip*) unzip "$arg";;
esac
如果您想file先將輸出捕獲到變數中,當然可以這樣做;但避免使用大寫的私有變數。
bzip2不過,unzip默認情況下會修改他們的輸入檔案。也許你想避免這種情況?
case $(file "$arg") in
*bzip2*) bzip2 -dc <"$arg";;
*Zip*) unzip -p "$arg";;
esac |
grep "stuff"
還要注意 shell 如何方便地讓您通過管道輸出(和輸入)條件。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/462308.html
上一篇:為什么我得到二元運算子預期的錯誤
