'expr: 構文エラー: 予期しない引数' - エイリアスの結果

'expr: 構文エラー: 予期しない引数' - エイリアスの結果

最近、.bash_aliasesファイルにエイリアスを追加しました。

alias runhole="perfect && cd data_series_test && doa=$(ls -1 | wc -l) && a=$(expr $doa / 2 ) && perfect && cd data_series_train && dob=$(ls -1 | wc -l) && b=$(expr $dob / 2 ) && perfect && python3 train.py > results_$b'_'$a"

端末を開くと、エラーが2回表示されます。

expr: syntax error: unexpected argument ‘2’
expr: syntax error: unexpected argument ‘2’

結果_a_bここで、aとbは、エイリアスで定義されたフォルダのファイル数を数えるときに定義された値ですが、コマンド出力は結果__

ベストアンサー1

エイリアスはほとんど常に関数で書くのが最善です。ここで難しい部分は、各コマンドを一緒に接続して&&早期に中断することです。set -eここでは、同じ効果を得るためにサブシェルを使用します。

runhole() {
    ( # run in a subshell to avoid side-effects in the current shell
        set -e
        perfect
        cd data_series_test
        doa=$( files=(*); echo "${#files[@]}" )
        a=$(( doa / 2 ))
        perfect
        cd data_series_train
        dob=$( files=(*); echo "${#files[@]}" )
        b=$(( dob / 2 ))
        perfect
        python3 train.py > "results_${b}_$a"
    )
}

おすすめ記事