Shell script syntax error: unexpected end of line - bash

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

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.

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.

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

Related

for loop in prompt with git command : -bash: syntax error near unexpected token `do'

im quit new to bash and im trying to do a for loop to find all tags not converted to svn in my git directory.
I've run the following command :
echo < for t in $(git for-each-ref --format='%(refname:short)' refs/remotes/tags); do git tag ${t/tags\//} $t && git branch -D -r $t done
However, i've got this error :
-bash: syntax error near unexpected token `&&'
i tryed several things like moving the semicolon but its always the same error but with differents characters like &&, done or i have the following error :
-bash: for: No such file or directory
because those are unix commands, i work on cygwin on windows.
Any ideas ?
ps:
Im following this tuto : https://www.gitkraken.com/blog/migrating-git-svn !
thank you for your help :)
< is input redirection. To it's right, a file name is expected (or a process specification, which then must be enclosed in parenthesis). In your case, you write the word for to the right of < and with it ask bash to use a file named for to replace stdin.
Furthermore, input redirection does not make sense with echo anyway, because echo does not process stdin. You are permitted to write a
echo <abc
if you want, but bash will only check the existence of the file abc and set up the redirection, and echo will silently ignore it, so there is no purpose in doing this.
First, you are missing a ; before done.
Second, you cannot do echo < for t in..., if you want it to run, remove echo < .
You will get ambiguous redirects if you don't.

Bash keeps throwing "syntax error: unexpected end of file"

I have been trying to write a shell script to copy and rename files, and I am running Ubuntu 20.04 on Windows subsystem for Linux. I'm currently just at the "copy" part, and I have the following, with <path> standing in for my file path.
#!/bin/bash
for file in "<sourcepath>"/*; do echo "$file" && cp "$file" "<destinationpath>"
done
cd "<destinationpath>"
The script appeared to execute correctly until I added the cd "<destinationpath>" line. Now the console says "syntax error: unexpected end of file". I don't have any newlines or anything I can think of that would be causing this.
The last two lines of hexdump -c:
I used dos2unix on the file, and I was able to run it without errors.

Why is awk command not working in shell script [duplicate]

I have written a bash script that gets three paths based on input parameters and then then gets the imagename/filename in the path.
Something like:
I provide:
AA=/home/user
Then it uses the find command to get
/home/user/dir2/images/dir/tellmeimage1fun.bin
Finally I have to get tellmeimage1fun.bin as output.
Script:
#!/bin/bash
echo "arg0 n/k/d"
AA=$1
CC=$3
PATH1="`find $AA/dir2/images/dir/ -name *image1*.bin`"
PATH2="`find $AA/dir2/images/dir/ -name *bimage2*.bin`"
PATH3="`find $AA/dir2/images/dir/ -name *cimage3*.bin`"
if [ $CC = "n" ] ; then
PATH=$PATH1
elif [ $CC = "k" ] ; then
PATH=$PATH2
else
PATH=$PATH3
fi
#Getting filename name from path:
IMG="`ls $PATH | cut -d "/" -f6`"
OUTPUT:
/users/prasapat/bin/sl5: line 22: ls: command not found
/users/prasapat/bin/sl5: line 22: cut: command not found
If I give complete paths to ls and cut they work. But i don't want to do that for all commands in the script. If i remove the last line and echo the PATH variable it is completely fine. Only after adding the last command, I see the problem.
What am I doing wrongly?
The problem is that you are redefining the PATH variable where bash looks into to find the binary files if you don't use a complete path when calling.
You should change the PATH in your bash script to MYPATH or something like that, so that it doesn't mess with the already environmental variables.
If you don't know what the PATH variable is for you can look at wikipedia's article
I had this problem, turns out editing a bash script using Notepad++ was adding DOS line endings instead of UNIX line endings. Running the script in a Linux environment was causing the 'command not found' error to be thrown.
Managed to diagnose the problem by running my script like so:
bash -x testscript.sh
Which will dump any compiler output. The error message that gets thrown is:
bash -x testscript.sh
+ $'\r'
: command not found 2:
'estscript.sh: line 3: syntax error near unexpected token `{
I fixed the issue by changing the formatting of line endings in Notepad++ to be UNIX not DOS by going Edit -> EOL Conversion -> UNIX.
$PATH is a special environment variable that contains a list of directories where your shell (in this case, bash) should look in when you type a command (such as find and ls.) Just try echo $PATH in a script or in a shell to get a feeling of what it looks like (you will typically have /bin, /usr/bin and /usr/local/bin listed there, maybe more.)
As you don't really need to redefine this variable in this particular script, you should use another name than $PATH.
$PATH is a predefined variable which gives the directories to search when looking for executables. Pick a different variable name for your script and you'll be fine.
Use a different variable name than PATH. $PATH is the environment variable which tells your shell where to look for executables (so, e.g., you can run ls instead of /bin/ls).
You are using the PATH that is special and used to locate the commands and that is why ls can't be resolved. Use any name other than PATH
if [ $CC = "n" ] ; then
MY_PATH=$PATH1
elif [ $CC = "k" ] ; then
MY_PATH=$PATH2
else
MY_PATH=$PATH3
fi
export MY_PATH
IMG="`ls $MY_PATH | cut -d "/" -f6`"

If-then-else syntax in tcsh

I'm trying to write a simple script in tcsh (version 6.12.00 (Astron) 2002-07-23), but I am getting tripped up by the if-then-else syntax. I am very new to script writing.
This script works:
#!/bin/tcsh -f
if (1) echo "I disagree"
However, this one does not:
#!/bin/tcsh -f
if ( 1 ) then
echo "I disagree"
else
echo "I agree"
endif
For one thing, this code, when run, echoes both statements. It seems to me it should never see the else. For another, the output also intersperses those echoes with three iterations of ": Command not found."
Edited to add: here is the verbatim output:
: Command not found.
I disagree
: Command not found.
I agree
: Command not found.
I know that the standard advice is to use another shell instead, but I am not really in a position to do that (new job, new colleagues, everyone else uses tcsh, want my scripts to be portable).
When I copy-and-paste your script and run it on my system, it correctly prints I disagree.
When I change the line endings to Windows-style, I get:
: Command not found.
I disagree
: Command not found.
I agree
: Command not found.
So, your script very likely has Windows-style line endings. Fix the line endings, and it should work. The dos2unix command is one way to do that (man dos2unix first; unlike most UNIX text-processing commands, it replaces its input file.)
The problem is that tcsh doesn't recognize ^M ('\r') as an end-of-line character. It sees the then^M at the end of the line as a single command, and prints an error message then^M: Command not found. The ^M causes the cursor to return to the beginning of the line, and the rest of the message overwrite the then.

bash script "unexpected end of file" in simple script

I have a script written in a file.
#!/bin/bash
if [ -f "/bin/uname" ]; then
OS=`/bin/uname`;
export OS="${OS}";
else
echo "Unable to detect OS - modify the appropriate .bashrc to support";
if
If I run it, I get the following error:
./temp.sh: line 9: syntax error: unexpected end of file
However, if I type the same script on bash prompt, it works.
(This piece of code is giving me nightmares. It is included in another large script which is failing due to these 6 lines. I put them in a separate script temp.sh and temp.sh gives the same error)!.
regards,
JP
Your last line should be fi instead of if.

Resources