syntax error: unexpected end of file reached - shell

#!/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.

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

my shell variable in if statement which is inside of while loop

I have below while loop which reads a file line by line for error codes, if this error code found in log.txt file, then I need to set alert=1 and then those errors are to be written a shell variable by concatenating them with a semicolon. But When I write below while loop it is not giving desired results.
log.txt file data:
ORA-03113
ORA-00933
errors.lst file data:
ORA-03113
ORA-00933
ERROR
export LOGFILE=/temp/log.txt
alert=0
error=""
while IFS= read -r line || [[ -n "$line" ]]; do
if grep -q $line "$LOGFILE"; then
alert=1
error="${error};${line}"
fi
done < errors.lst
echo $alert
echo $error
I was expecting below output:
;ORA-03113;ORA-00933
But I am getting below output:
;ORA-00933
Can you please help me here, where I am doing wrong.
It looks like your files contain \r (carriage return, ASCII
0xd). When your terminal emulator sees it, it removes everything it
has shown so far and moves to the beginning of the line. These
carriage return characters most commonly come from Windows-style line
endings. Run dos2unix on input files to get rid of them:
dos2unix log.txt errors.lst

bash catenate strings right to left

I'm debugging this catalina.sh script, at this point:
echo "$CATALINA_HOME"
echo "dot " .
echo "179"
if [ -r "$CATALINA_HOME"/bin/setclasspath.sh ]; then
echo "qui 81"
. "$CATALINA_HOME"/bin/setclasspath.sh
else
echo "185"
echo "Cannot find $CATALINA_HOME/bin/setclasspath.sh"
echo "This file is needed to run this program"
exit 1
fi
output is:
/home/sysadm/2_KNOWAGE/Knowage_6-2_ARES/Knowage-Server-CE
dot .
179
185
/bin/setclasspath.shsadm/2_KNOWAGE/Knowage_6-2_ARES/Knowage-Server-CE
This file is needed to run this program
when actually setclasspath.sh exixts, has write permissions and is where it's supposed to be. So I expect it to be found and run.
The problem is almost certainly to do with line endings. I strongly suspect that CATALINA_HOME is picking up a carriage return character because it has been saved with Windows line endings.
Try running the script through dos2unix to strip the carriage returns.
You might also want to check that your editor is set to use Unix line ends rather than Windows ones.

Syntax error near unexpected token 'elif'

#!/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.

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.

Resources