Syntax error: "fi" unexpected (expecting "then") in bash script - bash

I try to do the script:
#!/bin/bash
IP='192.168.1.1'
fping -c1 -t300 $IP 2>/dev/null 1>/dev/null
if [ "$?" = 0 ]
then
echo "Host found"
else
echo "Host not found"
fi
and i turn it:
pi#raspberrypi ~ $ sh /home/pi/sh/test.sh
/home/pi/sh/test.sh: 9: /home/pi/sh/test.sh: Syntax error: "fi" unexpected (expecting "then")
where is the problem?

You can try
$ dos2unix /home/pi/sh/test.sh
and run it again.

Most probably this is because carriage-return \r in your script. Try run this command to clean-up your script. Just run once. Original file will be backed up.
perl -pi.bak -e 's/\r$//' /home/pi/sh/test.sh

If you are editing the script file with Notepad++ on windows you can convert the EOL from the program menu with
Edit => EOL Conversion => Unix (LF)

if xxx then
commond
fi
Syntax error: “fi” unexpected (expecting “then”)
try it :
if xxx
then
commond
fi
it's ok.

It may be that you saved to the file from an ftp server rather than via nano or other console file edit prog.
Try pasting the code into the (empty) file via nano.
This fixed that exact issue for me.

Related

syntax error near unexpected token done bash script

I am new to Bash scripting, and I am getting this syntax error near unexpected token done. Could anyone help me out. Thanks in advance.
#!/bin/bash -x
echo "starting the process"
while true
do
echo "Fetching environment variables"
envsubst < /opt/config.conf.template > /opt/config.conf.prod
if [ -f /opt/config.conf.prod ]
then
echo "Config file exists."
python /opt/Deon.py /opt/config.conf.prod
fi
done
You have Windows line endings in your script. Run dos2unix on it, and it'll work. As other commenters have said, you'll also want to check that Notepad++ is set to using Unix line endings for Bash script.
Try this:
#!/bin/bash -x
echo "starting the process"
while true; do
echo "Fetching environment variables"
envsubst < /opt/config.conf.template > /opt/config.conf.prod
if [ -f /opt/config.conf.prod ]; then
echo "Config file exists."
python /opt/Deon.py /opt/config.conf.prod
fi
done
Not sure if required but I usually put the do on the same line as my condition and put a semicolon between the two (same thing with the if statement). I tried running this this way and it seems to work fine so I'm fairly certain that was the problem, feel free to correct me if I am wrong.

Shell script: command not "foundne", but works well on terminal

I am new to shell script, I created a sh file and it works well on the terminal, see the code below:
#!/bin/bash
# Use dmidecode to get version
dmidecode=`dmidecode -t chassis | grep 'Version' | sed -r 's/.*(.{6})/\1/'`
# Use ipmitool to get version
fru=`ipmitool fru | grep 'Chassis Part Number' | sed -r 's/.*(.{6})/\1/'`
# Compare the result
compare_result=0
if [ "$dmidecode" == "$fru" ]; then
compare_result="pass"
else
compare_result="false"
fi
# Create json
printf '"tcresult": {"dmidecode":{"chassis_type":"%s"},"fru":{"chassis_type":"%s"},"compare_result":"%s"}\n' "$dmidecode" "$fru" "$compare_result"
And the outcome is:
"tcresult": {"dmidecode":{"chassis_type":"N42.12"},"fru":{"chassis_type":"N42.12"},"compare_result":"pass"}
However, when I execute the sh file, the error shows below:
[root#localhost ~]# cd Desktop/
[root#localhost Desktop]# ls
avms avms.tar check_chasis.sh
[root#localhost Desktop]# sh check_chasis.sh
: command not foundne 3:
: command not foundne 7:
check_chasis.sh: line 15: syntax error: unexpected end of file
Thanks in advance for any advise or comment. Also see screenshot below
The "foundne" message is due to the fact you have an extra CR (carriage return) followed by a space at the beginning of the lines 3 and 7. The shell tries to execute that CR leading to the error message:
check_chasis.sh: line 3: \r : command not found
which is displayed as:
: command not foundne 3:
Remove it with:
tr -d '\r' < check_chasis.sh > check_chassis.bash
Note that dos2unix cannot fix this issue unless used with the -c mac option which is equivalent to running mac2unix.

bash IF statement doesn't work - getting error

i'm trying write simple IF statement in bash, but no mater what i try i allway get error, that shouldn't rise at all, can someone please take a look at my code and tell me what am I doing wrong?
#!/bin/bash
echo "Checking..."
echo
if [ -f /var/www/git/repos/last_change.txt ];
then
echo "/var/www/git/repos/last_change.txt exists."
cd /var/www &&
git fetch --all &&
git reset --hard origin/develop &&
rm -f "/var/www/git/repos/last_change.txt"
fi;
echo
echo "...done."
I allways get error:
root#machine:/var/www/git/repos# sh check.sh
Checking...
: not found: 3: check.sh: echo
check.sh: 12: check.sh: Syntax error: "fi" unexpected (expecting "then")
Everything seems to be ok, right?
I am glad for any help, thanks.
Judging by error description, most likely your file uses DOS newlines (CRLF). sh expects UNIX newlines (LF). Convert your file with dos2unix your_file_name.sh.

Bash - Escaping SSH commands

I have a set of scripts that I use to download files via FTP and then delete them from the server.
It works as follows:
for dir in `ls /volume1/auto_downloads/sync-complete`
do
if [ "x$dir" != *"x"* ]
then
echo "DIR: $dir"
echo "Moving out of complete"
# Soft delete from server so they don't get downloaded again
ssh dan#172.19.1.15 mv -v "'/home/dan/Downloads/complete/$dir'" /home/dan/Downloads/downloaded
Now $dir could be "This is a file" which works fine.
The problem I'm having is with special characters eg:
"This is (a) file"
This is a file & stuff"
tend to error:
bash: -c: line 0: syntax error near unexpected token `('
bash: -c: line 0: `mv -v '/home/dan/Downloads/complete/This is (a) file' /home/dan/Downloads/downloaded'
I can't work out how to escape it so both the variable gets evaluated and the command gets escaped properly. I've tried various combinations of escape characters, literal quotes, normal quotes, etc
If both sides are using bash, you can escape the arguments using printf '%q ', eg:
ssh dan#172.19.1.15 "$(printf '%q ' mv -v "/home/dan/Downloads/complete/$dir" /home/dan/Downloads/downloaded)"
You need to quote the whole expression ssh user#host "command":
ssh dan#172.19.1.15 "mv -v /home/dan/Downloads/complete/$dir /home/dan/Downloads/downloaded"
I'm confused, because your code as written works for me:
> dir='foo & bar (and) baz'
> ssh host mv -v "'/home/dan/Downloads/complete/$dir'" /home/dan/Downloads/downloaded
mv: cannot stat `/home/dan/Downloads/complete/foo & bar (and) baz': No such file or directory
For debugging, use set -vx at the top of the script to see what's going on.
Will Palmer's suggestion of using printf is great but I think it makes more sense to put the literal parts in printf's format.
That way, multi-command one-liners are more intuitive to write:
ssh user#host "$(printf 'mkdir -p -- %q && cd -- "$_" && tar -zx' "$DIR")"
One can use python shlex.quote(s) to
Return a shell-escaped version of the string s
docs

Bash syntax error: unexpected end of file

Forgive me for this is a very simple script in Bash. Here's the code:
#!/bin/bash
# june 2011
if [ $# -lt 3 -o $# -gt 3 ]; then
echo "Error... Usage: $0 host database username"
exit 0
fi
after running sh file.sh:
syntax error: unexpected end of file
I think file.sh is with CRLF line terminators.
run
dos2unix file.sh
then the problem will be fixed.
You can install dos2unix in ubuntu with this:
sudo apt-get install dos2unix
Another thing to check (just occured to me):
terminate bodies of single-line functions with semicolon
I.e. this innocent-looking snippet will cause the same error:
die () { test -n "$#" && echo "$#"; exit 1 }
To make the dumb parser happy:
die () { test -n "$#" && echo "$#"; exit 1; }
i also just got this error message by using the wrong syntax in an if clause
else if (syntax error: unexpected end of file)
elif (correct syntax)
i debugged it by commenting bits out until it worked
an un-closed if => fi clause will raise this as well
tip: use trap to debug, if your script is huge...
e.g.
set -x
trap read debug
I got this answer from this similar problem on StackOverflow
Open the file in Vim and try
:set fileformat=unix
Convert eh line endings to unix endings and see if that solves the
issue. If editing in Vim, enter the command :set fileformat=unix and
save the file. Several other editors have the ability to convert line
endings, such as Notepad++ or Atom
Thanks #lemongrassnginger
This was happening for me when I was trying to call a function using parens, e.g.
run() {
echo hello
}
run()
should be:
run() {
echo hello
}
run
I had the problem when I wrote "if - fi" statement in one line:
if [ -f ~/.git-completion.bash ]; then . ~/.git-completion.bash fi
Write multiline solved my problem:
if [ -f ~/.git-completion.bash ]; then
. ~/.git-completion.bash
fi
So I found this post and the answers did not help me but i was able to figure out why it gave me the error. I had a
cat > temp.txt < EOF
some content
EOF
The issue was that i copied the above code to be in a function and inadvertently tabbed the code. Need to make sure the last EOF is not tabbed.
on cygwin I needed:-
export SHELLOPTS
set -o igncr
in .bash_profile . This way I didn't need to run unix2dos
FOR WINDOWS:
In my case, I was working on Windows OS and I got the same error while running autoconf.
I simply open configure.ac file with my NOTEPAD++ IDE.
Then I converted the File with EOL conversion into Windows (CR LF) as follows:
EDIT -> EOL CONVERSION -> WINDOWS (CR LF)
Missing a closing brace on a function definition will cause this error as I just discovered.
function whoIsAnIidiot() {
echo "you are for forgetting the closing brace just below this line !"
Which of course should be like this...
function whoIsAnIidiot() {
echo "not you for sure"
}
I was able to cut and paste your code into a file and it ran correctly. If you
execute it like this it should work:
Your "file.sh":
#!/bin/bash
# june 2011
if [ $# -lt 3 -o $# -gt 3 ]; then
echo "Error... Usage: $0 host database username"
exit 0
fi
The command:
$ ./file.sh arg1 arg2 arg3
Note that "file.sh" must be executable:
$ chmod +x file.sh
You may be getting that error b/c of how you're doing input (w/ a pipe, carrot,
etc.). You could also try splitting the condition into two:
if [ $# -lt 3 ] || [ $# -gt 3 ]; then
echo "Error... Usage: $0 host database username"
exit 0
fi
Or, since you're using bash, you could use built-in syntax:
if [[ $# -lt 3 || $# -gt 3 ]]; then
echo "Error... Usage: $0 host database username"
exit 0
fi
And, finally, you could of course just check if 3 arguments were given (clean,
maintains POSIX shell compatibility):
if [ $# -ne 3 ]; then
echo "Error... Usage: $0 host database username"
exit 0
fi
In my case, there is a redundant \ in the like following:
function foo() {
python tools/run_net.py \
--cfg configs/Kinetics/X3D_8x8_R50.yaml \
NUM_GPUS 1 \
TRAIN.BATCH_SIZE 8 \
SOLVER.BASE_LR 0.0125 \
DATA.PATH_TO_DATA_DIR ./afs/kinetics400 \
DATA.PATH_PREFIX ./afs/kinetics400 \ # Error
}
There is NOT a \ at the end of DATA.PATH_PREFIX ./afs/kinetics400
I just cut-and-pasted your example into a file; it ran fine under bash. I don't see any problems with it.
For good measure you may want to ensure it ends with a newline, though bash shouldn't care. (It runs for me both with and without the final newline.)
You'll sometimes see strange errors if you've accidentally embedded a control character in the file. Since it's a short script, try creating a new script by pasting it from your question here on StackOverflow, or by simply re-typing it.
What version of bash are you using? (bash --version)
Good luck!
Make sure the name of the directory in which the .sh file is present does not have a space character. e.g: Say if it is in a folder called 'New Folder', you're bound to come across the error that you've cited. Instead just name it as 'New_Folder'. I hope this helps.
Apparently, some versions of the shell can also emit this message when the final line of your script lacks a newline.
In Ubuntu:
$ gedit ~/.profile
Then, File -> Save as and set end line to Unix/Linux
I know I am too late to the party. Hope this may help someone.
Check your .bashrc file. Perhaps rename or move it.
Discussion here: Unable to source a simple bash script
For people using MacOS:
If you received a file with Windows format and wanted to run on MacOS and seeing this error, run these commands.
brew install dos2unix
sh <file.sh>
If the the script itself is valid and there are no syntax errors, then some possible causes could be:
Invalid end-of-lines (for example, \r\n instead of \n)
Presence of the byte order mark (BOM) at the beginning of the file
Both can be fixed using vim or vi.
To fix line endings open the file in vim and from the command mode type:
:set ff=unix
To remove the BOM use:
:set nobomb
For those who don't have dos2unix installed (and don't want to install it):
Remove trailing \r character that causes this error:
sed -i 's/\r$//' filename
Details from this StackOverflow answer. This was really helpful.
https://stackoverflow.com/a/32912867/7286223

Resources