2

I wrote a simple shell script to check for the existence of a xml file and if it exists, then rename an old xml file to be backup and then move the new xml file to where the old xml file was stored.

#!/bin/sh    

oldFile="/Documents/sampleFolder/sampleFile.xml"
newFile="/Documents/sampleFile.xml"
backupFileName="/Documents/sampleFolder/sampleFile2.backup"
oldFileLocation="/Documents/sampleFolder"

if [ -f "$newFile" ] ; then
    echo "File found"
    #Rename old file
    mv $oldFile $backupFileName
    #move new file to old file's location
    mv $newFile $oldFileLocation
else
    echo "File not found, do nothing"
fi   

However, every time I try to run the script, I get 4 command not found messages and a syntax error: unexpected end of file. Any suggestions on why I get these command not found errors or the unexpected end of file? I double checked that I closed all my double quotes, I have code highlight :)

EDIT: output from running script:

: command not found: 
: command not found: 
: command not found1: 
: command not found6: 
replaceXML.sh: line 26: syntax error: unexpected end of file
5
  • Can you post the exact error messages? I don't see any problem with your code. Commented Oct 5, 2012 at 21:16
  • That worked here (with both bash and dash). But you should probably place each variable used on the mv between double quotes. Commented Oct 5, 2012 at 21:16
  • 2
    Your script probably has DOS line endings. Commented Oct 5, 2012 at 21:29
  • learn to turn on shell debugging so you can see what is being executed. set -vx. Good luck. Commented Oct 5, 2012 at 21:36
  • That could be it, I created the shell script in windows and then copied it over to my ubuntu instance in VMWare player to test. I did create another empty shell script, copied the stuff over and the errors disappeared. Now it's not finding the file, but that's another issue :) Commented Oct 5, 2012 at 21:36

3 Answers 3

10

I believe you're running on Cygwin. There's more to the error messages than what you're seeing:

: command not found: 
: command not found: 
: command not found1: 
: command not found6: 
replaceXML.sh: line 26: syntax error: unexpected end of file

You probably used a Windows editor to create the script file, which means it uses Windows-style CR-LF ("\r\n") line endings, rather than Unix-style LF ('\n') line endings. Some programs under Cygwin can handle either form, but the shell doesn't.

For example, the line that looks like

then

looks to the shell like

then^M

where ^M is the ASCII CR character. This would actually be a valid command name if it existed, but it doesn't, so the shell complains:

then^M: command not found

But printing the CR character causes the cursor to go back to the beginning of the line, so everthing before the : is overwritten.

You're getting the "unexpected end of file" message because the shell never saw a fi to match the if.

You can use the dos2unix command to fix the line endings. Be sure to read the man page (man dos2unix); unlike most text filters, dos2unix replaces its input file rather than writing to stdout.

Sign up to request clarification or add additional context in comments.

3 Comments

I didn't even think of cygwin. Good answer.
If you happen to be running ubuntu 10.04, then there is no dos2unix. Instead you would install tofrodos using sudo apt-get install tofrodos and then run fromdos on your file. (ref: ubuntuforums.org/showthread.php?t=1540180)
@arun 12 years later: I suspect that dos2unix was available for Ubuntu 10.04 (it was developed in 1989). It might not have been installed by default.
0

I can't really see anything wrong with your code apart from then not being in a legal place for older shells. Also notice the quotes around arguments to mv (but that should not be a problem if the files are named properly).

Try this:

#!/bin/sh    

oldFile="/Documents/sampleFolder/sampleFile.xml"
newFile="/Documents/sampleFile.xml"
backupFileName="/Documents/sampleFolder/sampleFile2.backup"
oldFileLocation="/Documents/sampleFolder"

if [ -f "$newFile" ]
then
    echo "File found"
    mv "$oldFile" "$backupFileName"
    mv "$newFile" "$oldFileLocation"
else
    echo "File not found, do nothing"
fi  

PS: verify that /bin/sh is (or points to) a bourne based shell.

Comments

0

What I did in my case: I used Bash On Ubuntu on Windows (in Windows 10) instead of Cygwin and then installed dos2unix using sudo apt-get install dos2unixand used the following command to fix this problem:

$ dos2unix < compilelibs.sh > output.sh

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.