すべてのzipファイルとすべての7z / 7zipファイルを除いて、ルート/現在のフォルダー内のすべてのファイルを7zipしたい。1つのステートメントで一度に機能させることができるのはこれらのうちの1つだけです。if !
for file in ./test2/*
do
if ! { `file $file | grep -i zip > /dev/null 2>&1` && `file $file | grep -i 7z > /dev/null 2>&1`; }; then
## Most of the zip utilities contain "zip" when checked for file type.
## grep for the expression that matches your case
7z a -mx0 -mmt2 -tzip "${file%/}.cbz" "$file"
rm -rf "$file"
fi
done
私は{list;};
他の投稿の「リスト」基準に従いましたが、運がありません。
私の現在の解決策は、次のようにステートメントをネストする if
ことです。
for file in ./test2/*
do
if ! `file $file | grep -i 7z > /dev/null 2>&1`; then
if ! `file $file | grep -i zip > /dev/null 2>&1`; then
## first if its not 7z, now also if not zip.
7z a -mx0 -mmt2 -tzip "${file%/}.cbz" "$file"
rm -rf "$file"
fi
fi
done
残っているのは、ディレクトリを除外することだけです。すべてのファイルが移動します。方法?
の出力をfile
個別に取得し、それを複数のテストまたはcase
ステートメントで使用します。
for file in ./test2/*; do
filetype=$( file "$file" )
if [[ $filetype == *7z* ]] ||
[[ $filetype == *zip* ]]
then
# skip these
continue
fi
# rest of body of loop here
done
または、
for file in ./test2/*; do
filetype=$( file "$file" )
case $filetype in
*7z*) continue ;; # skip these
*zip*) continue ;; # and these
esac
# rest of body of loop here
done
file
自由形式のテキスト文字列の代わりにMIMEタイプを出力することもできます。これはで行うことができfile -i
、スクリプトの移植性が少し向上します(気になる場合)。file(1)
マニュアル(man 1 file
)を参照してください。
ディレクトリを除外するには、
if [ -d "$file" ]; then
continue
fi
への呼び出しの前file
。
または、短絡構文を使用します。
[ -d "$file" ] && continue
このcontinue
ステートメントは、上記で使用されているすべての場合において、現在の反復の残りをスキップし、ループの次の反復に進みます。の現在の値がこの反復で処理したくない$file
ものであると判断したときに使用しています。これは、操作をいつ実行するかについて一連のテストを作成するという、あなたが行うことの反対です。
/bin/sh
互換スクリプトは最後に次のようになります。
#!/bin/sh
for file in ./test2/*; do
[ -d "$file" ] && continue
filetype=$( file "$file" )
case $filetype in
*7z*) continue ;; # skip these
*zip*) continue ;; # and these
esac
# rest of body of loop here
done
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加