判斷檔案是否被壓縮

判斷檔案是否被壓縮

uncompress我需要在腳本中查找文件是否被壓縮attachement。我的 find 指令產生兩個文件sum12.pdf.Zsum23.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代替多個ifand語句的腳本的替代實作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 {} +

相關內容