有効な識別子ではなくシェルスクリプトの実行中にエラーが発生しました。

有効な識別子ではなくシェルスクリプトの実行中にエラーが発生しました。

データベースに接続し、いくつかのタスクを実行する次のシェルスクリプトがあります。

echo Please enter userid:
read USER_NAME

echo "Please enter password:"
read -s PASS   

SID=${ORACLE_SID}

if [ "${ORACLE_SID}" != 'TEST'  ]
then
    sqlplus -s -l $USER_NAME/$PASS@$SID  << EOF
    copy from scott/tiger@orcl insert emp using select * from emp
    exit
EOF

else
    echo  "Cannot copy"
fi

しかし、1を実行するとエラーが発生します。

Please enter local Username:
scott
': not a valid identifierd: `USER_NAME
copy_data.sh: line 3: $'\r': command not found
Please enter local Password:
': not a valid identifierd: `
copy_data.sh: line 6: $'\r': command not found
copy_data.sh: line 8: $'\r': command not found
copy_data.sh: line 18: syntax error near unexpected token `fi'
copy_data.sh: line 18: `fi'

この問題をどのように解決できますか?

デバッグモード

$ bash -x test.sh
+ echo 'please enter username'
please enter username
+ read user_name
vbx
+ echo 'please enter password'
please enter password
+ read -s pass
+ echo
test.sh: line 12: syntax error near unexpected token `else'
test.sh: line 12: `else'

ベストアンサー1

-インデントを使用する場合は、ハイフン(例:)を追加する必要があります<<-EOFので、次のように変更できます。

sqlplus -s -l $USER_NAME/$PASS@$SID  <<-EOF
copy from scott/tiger@orcl insert emp using select * from emp
exit
EOF

構文エラーが正しくない場合は、色で強調表示できるVIMやNotepad ++などのエディタを使用することをお勧めします。

メモ帳++の例:

ここに画像の説明を入力してください。

おすすめ記事