![判斷檔案是否被壓縮](https://rvso.com/image/38697/%E5%88%A4%E6%96%B7%E6%AA%94%E6%A1%88%E6%98%AF%E5%90%A6%E8%A2%AB%E5%A3%93%E7%B8%AE.png)
uncompress
我需要在腳本中查找文件是否被壓縮attachement
。我的 find 指令產生兩個文件sum12.pdf.Z
,sum23.pdf.Z
我的腳本是
dir=/home/as1234/bills
cd $dir
for file in `find . -ctime -1 -type f -name "Sum*pdf*"`
do
if [ ${file: -1} == "Z" ]; then
echo "$file is Zipped"
uncompress $file
uuencode $file
fi
done
uuencode $file $file | mailx -s "subject" [email protected]
當我運行這個腳本時,我收到了類似的錯誤
${file: -1}: 0403-011 The specified substitution is not valid for this command.
我在用ksh
。
答案1
最後一個點之後的檔名後綴可能帶有${file##*.}
.
但在這種情況下,我會考慮直接像這樣進行解壓縮和 uuencoding find -exec
:
#!/bin/sh
dir=/home/as1234/bills
find "$dir" -type f -ctime -1 -name "Sum*.pdf*" -exec sh -c '
for pathname do
filename=$( basename "${pathname%.pdf*}.pdf" )
if [ "${pathname##*.}" = "Z" ]; then
uncompress -c "$pathname"
elif [ "${pathname##*.}" = "gz" ]; then
gzip -cd "$pathname"
else
cat "$pathname"
fi |
uuencode "$filename" |
mailx -s "subject ($filename)" [email protected]
done' sh {} +
這樣,您就可以支援帶有空格和其他麻煩字元的路徑名。該sh -c
腳本也不儲存未壓縮的文件,而是對其進行解壓縮、uuencode 並一次發送。
我還添加了對gzip
壓縮檔案的處理。
有關的:
sh -c
使用case ... esac
代替多個if
and語句的腳本的替代實作elif
。
find "$dir" -type f -ctime -1 -name "Sum*.pdf*" -exec sh -c '
for pathname do
filename=$( basename "${pathname%.pdf*}.pdf" )
case $pathname in
*.Z) uncompress -c "$pathname" ;;
*.gz) gzip -cd "$pathname" ;;
*) cat "$pathname"
esac |
uuencode "$filename" |
mailx -s "subject ($filename)" [email protected]
done' sh {} +