予期しないファイル終了エラーが発生しましたが、原因を特定できないようです。

予期しないファイル終了エラーが発生しましたが、原因を特定できないようです。

3つまたは4つの引数を使用する作業用スクリプトを作成しました。最初の引数は-e(エンコード)または-d(デコード)、2番目の引数はエンコード/デコードキー、3番目の引数は出力名です。ファイル内の4番目のパラメータはオプションで、エンコード/デコードするターゲットファイルになります。最初の3つのパラメータのみが提供されている場合、このコマンドを使用するにはユーザー入力が必要ですread

ただし、スクリプトを実行しようとすると、次のエラーが発生します。

./cipher.sh: line 20: Unexpected EOF while looking for matching `''
./cipher.sh: line 26: syntax error: unexpected end of file

これは私のスクリプトです。

#!/bin/bash

if [ "$#" -lt 3 ] || [ "$#" -gt 4 ]; then           #checks for 3 or 4 arguments, 
                                                    #error otherwise
    echo "Error: Need 3 or 4 arguments"
    exit 1
fi

if [ "$1" != "-e" ] && [ "$1" != "-d" ]; then       #Checks if the first argument 
                                                    #is -e or -d, error otherwise
    echo "Error: First argument must be -e or -d"
    exit 1
fi

if [ "$#" -eq 3 ]; then                            #If only 3 arguments are given
    read -p "Enter your input: " userinput
    echo $userinput | tr '[a-z]' '[A-Z]' > $3      #changes all letters to capital
    cat $3 | tr '[A-Z] '$2' > $3                   #Replaces all letters with 
                                                   #letters in key..
elif [ "$#" -eq 4]; then                           #if target file is specified..
    if [ -f $4 ]; then                             #If the file exists and is 
                                                   #regular..
        cat $4 | tr '[a-z]' '[A-Z]' > $3
        cat $3 | tr '[A-Z]' '$2' > $3              #(line 20)
    elif [ ! -f $4 ]; then                         #If the file does not exist
        echo "Error: Target file does not exist"
        exit 1
    fi
fi

ベストアンサー1

検索:次から始まる行

cat $3

見積もりがありません

おすすめ記事