Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

This question already has an answer here:

I have the following shell script in order to connect to database and do some tasks.

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

However 1when I execute I am getting errors

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'

How can I resolve this issue

Debug mode

$ 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'
share|improve this question
2  
Looks like your file has Windows (or Mac) line endings... –  Mat Aug 22 '13 at 10:37
    
@Mat I have used notepad+ to create the script, how can I make sure it doesn't have Windows line endings? I have tried opening and saving the file using vi editor of cygwin, however it doesn't help. –  Polppan Aug 22 '13 at 10:42
    
Thanks @Mat I have used a tool called WinVi as editor and errors disappeared. However when I execute shell script I am getting error syntax error near unexpected token else'` –  Polppan Aug 22 '13 at 11:08
    
@Polppan have you checked correction as I mentioned ? –  Rahul Patil Aug 22 '13 at 11:45
add comment

marked as duplicate by Mat, Anthon, terdon, slm, rahmu Aug 22 '13 at 15:01

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1 Answer

up vote 1 down vote accepted

If you are using Indention then you need to add hyphen - like <<-EOF, then it will work, So Just Do following Changes :

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

I Would Suggest you Some Editor like VIM or Notepad++ which Highlight Syntax error in Colors, if they Wrong.

Example of notepad++ :

enter image description here

share|improve this answer
    
Rahul, I have tried as you have mentioned, however I am getting the same error message when I am executing shell script. –  Polppan Aug 22 '13 at 11:47
    
You can run script in debug mode like bash -x script.sh and give me output in pastbin –  Rahul Patil Aug 22 '13 at 11:48
    
I have edited my question to include the output of script execution in debug mode. –  Polppan Aug 22 '13 at 11:53
    
Have you made change like << EOF to <<-EOF and also Space in EOF, if no check copy and paste in script from this page and let me know paste.ubuntu.com/6013791 –  Rahul Patil Aug 22 '13 at 11:58
    
Check this is working paste.ubuntu.com/6013802 –  Rahul Patil Aug 22 '13 at 12:01
show 1 more comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.