Syntax error near unexpected token 'elif' - bash

#!/bin/bash
if [ "$1" = "boot" ]
then
if [ -f /var/log/boot.log ]
then
echo /var/log/boot.log
elif [ -f /var/log/boot ]
then
echo /var/log/boot
fi
fi
This shows the output:
: command not foundline 8: GetLogfileName.sh: line 15: syntax error
near unexpected token `elif' 'etLogfileName.sh: line 15: `
elif [ -f /var/log/boot ]
What is going wrong here?

The garbled error message indicates your file has carriage returns before the newline. Did you edit your script on Windows? Either use your text editor to save the file without carriage returns or run the script through dos2unix (or perhaps d2u)

If you are using vi editor, set ":set ff=unix", save the file, and re-execute it.
This file format (ff) set command tells vi to use LF-only line endings when the file is saved.

Related

trouble executing script in cygwin

I am completely new to scripting, so take it easy on me, thanks. I use the command bash greetings2 and receive
greetings2: line 17: unexpected EOF while looking for matching `"'
greetings2: line 21: syntax error: unexpected end of file.
#
# greetings2
# greetings program version 2
# A sample program using the if-then-elif construct
# This program displays greetings according to the time of day
# Version2: using the date command format control option
#
echo # skip a line
hour=`date +%H` # store the part of the date string that shows the hour
if [ "$hour" -le 18 ] # check for the morning hours
then
echo "GOOD MORNING"
elif [ "$hour -le 18 ] # check for the afternoon hours
then
echo "GOOD AFTERNOON"
else #it must be evening
echo "GOOD EVENING"
fi
echo # skip a line
exit 0 # end of the program, exit
your script has CRLF line termination (Windows style).
Convert it with d2u filename to LF line termination (Unix style)
d2u belongs to dos2unix package
To verify you can use
$ file greetings2
greetings2: ASCII text, with CRLF line terminators
$ d2u greetings2
dos2unix: converting file greetings2 to Unix format...
$ file greetings2
greetings2: ASCII text

Bash Script Won't Run - Word unexpected and unexpected token

So I have 1 bash scripts,
findFungible.sh
#!/bin/bash
for file in $*;
for word in $(cat $file);
if [ $word == Fungible ];
echo Fungible found
fi
done
done
Which should be checking files if they contain the word fungible.
It's pretty much verbatim out of my lecture example.
So if I run it with bash findFungible.sh
I get:
findFungible.sh: line 2: syntax error near unexpected token "$\r''
'indFungible.sh: line 2: 'for file in $*;
So I think it has something to do with windows putting in extra line \r characters or something. As there is a \r after $.
Then if I run it with sh findFungible.sh
I get:
findFungible.sh: 2: findFungible.sh Syntax error: word unexpected (expecting "do")
Any help would be great thanks.
As someone mentioned in a reply, you have syntax errors in this, meaning, you are missing "do's" and "then's".
#!/bin/bash
for file in $*; do
for word in $(cat $file); do
if [ $word == Fungible ]; then
echo Fungible found
fi
done
done
And yes, like mentioned in the reply, bash is very sensitive to white spaces, new lines and quotes, I'm not getting into too much detail there.

syntax error: unexpected end of file reached

#!/bin/bash
if [ "$PATH" = "blah" ]
then
echo "Success"
else
echo "Failure"
fi
Trying to understand how shell sript if/else works but after running it through the interpreter it returns Unexpected end of file
It is likely that your script file has been saved with CRLF line terminators, instead of just LF. The shell does not accept CRLF line endings. Change your editor settings so that the file line endings are LF only.

Why would a bash script produce the error "command not founde"?

What is wrong with the 5th line in this script ( I have included the snippet that gives me the error and the actual error is listed in the bottom after the code and a link to complete script)?
#! /bin/bash
INSTALLDIR=/usr/local/mapguideopensource
CLEAN_FLAG=0
while [ $# -gt 0 ]; do # Until you run out of parameters...
case "$1" in
-prefix|--prefix)
INSTALLDIR="$2"
shift
;;
-clean|--clean)
CLEAN_FLAG=1
shift
;;
-help|--help)
echo "Usage: $0 (options)"
echo "Options:"
echo " --prefix [installation directory]"
echo " --clean [clean all objects and binaries in Oem]"
echo " --help [Display usage]"
exit
;;
esac
shift # Check next set of parameters.
done
This is the error i get when i run this bash script on linux (REHL5) :
: command not founde 4:
: command not founde 8:
: command not founde 8:
: command not founde 12:
MapGuide Open Source build script for OEM components
'/build_oem.sh: line 17: syntax error near unexpected token `in
'/build_oem.sh: line 17: ` case "$1" in
Please note, that the line number above corresponds to the actual script i am running (i have included a link to that script below)
The original script i am running
From the errors, I'm pretty sure you have carriage returns (aka CR or ^M) at the end of the lines. Windows/DOS text files have carriage return AND linefeed at the end of each line, but unix programs (like bash) just expect a linefeed, and get horribly confused if there's a CR as well. The giveaway is error messages like:
: command not founde 4:
What this really is is ./build_oem.sh: line 4: ^M: command not found, but the carriage return makes the terminal go back to the beginning of the line, and write the end of the message over the beginning of the message:
./build_oem.sh: line 4:
: command not found
|
V
: command not founde 4:
To fix the script, use dos2unix to convert it to proper unix format, then switch to a text editor that saves in unix format.
What choroba says, but also note that your shebang has to be on the first line (which it is not), otherwise it is useless since it's just a plain comment then and it won't necessarily execute under bash.
In the original script, lines 4 and 8 are empty. There is probably some invisible control character on the lines. Try xxd build_oem.sh.

"if" statements of bash under mac os

If there some difference between the bash of Mac OS and other linuxs'?
I wrote a simple bash script named "test.sh" like this:
#!/bin/bash
MYVAR=abc
if [ $MYVAR = abc ]; then
echo "ok"
fi
When I run it in terminal, some error occurs:
./test.sh: line 3: syntax error near unexpected token `then'
./test.sh: line 3: `if[ $MYVAR = abc ]; then'
then I delete the character ";" before "then" and run the script again, some infos occurs:
./test.sh: line 3: if[ abc = abc ]: command not found
ok
./test.sh: line 5: syntax error near unexpected token `fi'
./test.sh: line 5: `fi'
Could someone tell me what's wrong with my script?
Consider putting spaces into your file the way you put it in your example (if [).
[ is a command (same as test). It must be separated by spaces on both sides.

Resources