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.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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:

echo "Please enter password:"
read -s PASS   


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

    echo  "Cannot copy"

However 1when I execute I am getting errors

Please enter local Username:
': 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
+ 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

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.

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. – user75ponic 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'` – user75ponic Aug 22 '13 at 11:08
@Polppan have you checked correction as I mentioned ? – Rahul Patil Aug 22 '13 at 11:45
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

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. – user75ponic 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. – user75ponic 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

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