bashでbzip gzip zipを使う

bashでbzip gzip zipを使う
#!/bin/bash

# check if the user put a file name
if [ $# -gt 0 ]; then

    # check if the file is exist in the current directory
    if [ -f "$1" ]; then

        # check if the file is readable in the current directory
        if [ -r "$1" ]; then
            echo "File:$1"
            echo "$(wc -c <"$1")"

            # Note the following two lines
            comp=$(bzip2 -k $1)
            echo "$(wc -c <"$comp")"
        else
            echo "$1 is unreadable"
            exit
        fi
    else
        echo "$1 is not exist"
        exit
    fi
fi

$1現在私の問題は、bzipでファイルを単一のファイルに圧縮できることですが$1.c.bz2、圧縮ファイルのサイズをキャプチャするとどうなりますか?私のコードにはそのようなファイルは表示されません。

ベストアンサー1

私はあなたのためにコードを整理しました:

#!/bin/bash

#check if the user put a file name
if [ $# -gt 0 ]; then
    #check if the file is exist in the current directory
    if [ -f "$1" ]; then
        #check if the file is readable in the current directory
        if [ -r "$1" ]; then
            echo "File:$1"
            echo "$(wc -c <"$1")"
            comp=$(bzip2 -k $1)
            echo "$(wc -c <"$comp")"
        else
            echo "$1 is unreadable"
            exit
        fi
    else
        echo "$1 is not exist"
        exit
    fi
fi

fi最後から2行目の追加を確認してください。

bzip2 -k test.file11行目は出力が生成されないため意味がありません。したがって、変数はcomp空です。

簡単な方法は、拡張が何であるかを簡単に知ることである.bz2ため、次のようにすることができます。

            echo "$(wc -c <"${1}.bz2")"

compそして変数はまったく使用されません。

おすすめ記事