ディレクトリからファイルの7zipアーカイブを作成する方法(既存のアーカイブを除く)は何ですか?

ディレクトリからファイルの7zipアーカイブを作成する方法(既存のアーカイブを除く)は何ですか?

〜したい7zip次のすべてのファイルルート/現在フォルダとは別にすべてzipファイルとすべての7z / 7zipファイル。私はif !ステートメントで一度にそれらのうちの1つだけを動作させることができます:

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

残りはディレクトリを除外するだけです。すべてのファイルが移動されます。どのように?

ベストアンサー1

出力を個別にインポート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

おすすめ記事