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.
Related
I am trying to read a file if file exist in a folder. But when I use if-then-else statement to check file exists or not, this error will occur.
syntax error: unexpected end of file
I wrote script in centos7. I have tried in many ways to fix this error such as, removing square brackets, adding spaces and removing semi-columns. Still i couldn't find any solution.
flag="0"
path="/home/abc/file.txt"
if [ -f $path ]; then
flag="1"
echo "file exists"
else
echo "file could't find"
fi
I expect to print set flag as "1" if file exists and print "file could't find" if file doesn't exist.
Your script runs quite fine. Chances are you've some special characters. Install dos2unix using yum install dos2unix or any other way and then do /usr/bin/dos2unix /path/your_script.sh. Now run your script and see if it works.
I'm trying to execute a very simple script with cygwin, composed of:
#!/bin/bash\n
echo "hi"\n
with cygwinpath\bin\bash.exe /cygdrive/c/my_path/test.bash
but it says
/cygdrive/c/my_path/test.bash: line 1: #!/bin/bash: No such file or directory
However, it still prints 'hi'.
Why is this, and how to fix it ?
Thanks.
The first line of your script should just be #!/bin/bash and not #!/bin/bash\n
The code is still executing because the heading #!/bin/bash specifies a shell, and echo "hi"\n is a command to the terminal.
As for your issue I'm having no problems running it using the following path in the cygwin terminal:
/cygdrive/c/<my_path>/bin/bash.exe /home/user/test.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
I am trying to run a command in a script, something like this one:
ssh user#host:/bin/echo > /home/path/file.log
Now when I run this command on a command line it works fine, however when put in a script (shell or ruby ) it cribs saying:
/bin/sh: /home/path/*.log: No such file or directory
Am I missing something?
Thanks!
Update:
It's weird that same thing is not being executed now even on the shell when I use putty. I have verified that the path and file exists on remote machine which is being ssh'ed into.
You need to loop over the files. If it works from the command line then your interactive shell is not a standard shell.
for f in /home/path/*.log; do
:>"$f"
done
Note also the use of a null command; in many shells, you don't need a command at all. Your echo puts an unattractive empty line at the beginning of each file.
If you are attempting to run this remotely, you will need to quote it:
ssh user#remote 'for f in /home/path/*.log; do :>"$f"; done'
Its working fine when I put quotes:
ssh user#host:"/bin/echo > /home/path/file.log"
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.