我有以下 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'
答案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++,它们可以在颜色中突出显示语法错误(如果它们错误)。
记事本++示例: