Shell

執行 shell 腳本時錯誤不是有效的標識符

  • August 22, 2013

我有以下 shell 腳本來連接數據庫並執行一些任務。

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'

-如果您使用縮進,那麼您需要添加連字元<<-EOF,然後它會起作用,所以只需執行以下更改:

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

我會建議你一些像 VIM 或 Notepad++ 這樣的編輯器,它們會在顏色中突出顯示語法錯誤,如果它們錯誤的話。

記事本++範例:

在此處輸入圖像描述

引用自:https://unix.stackexchange.com/questions/87768