case in bash: "line 4: syntax error near unexpected token `)'" - linux

case in bash:
line 4: syntax error near unexpected token `)'
I'm trying to use the command case in Bash (on my Raspberry Pi again), but when I run my script, Bash spits out errors. I've read over many tutorials and I think I'm doing the same thing as them, but something's just not right.
Here's my code:
#!/bin/bash
case "$1" in
help) echo "You asked for help. Sorry, I'm busy."
*) echo "You didn't say anything. Try 'help' as the first argument."
esac
Here's the output (the filename is newmkdir and I ran it with no arguments):
./newmkdir: line 4: syntax error near unexpected token `)'
./newmkdir: line 4: ` *) echo "You didn't say anything. Try 'help' as the first argument."'
I'm trying to have my script interpret help and then make anything else output the next line.
(Note this is just an example of a glitched script. This script has no meaning and might not even make sense, it's just a test.)

You are missing ;; at the end of each pattern:
#!/bin/bash
case "$1" in
help)
echo "You asked for help. Sorry, I'm busy."
;;
*)
echo "You didn't say anything. Try 'help' as the first argument."
;;
esac
Think of it as a break statement in a programming language. They are compulsory on case.

Related

How to solve Linux If-Else statement syntax error?

I am trying to check whether a directory already exists or not inside a case-control statement. But it is giving an error in 'then' statement.
case $choice in
1)echo "Enter directory name: "
read dname
mkdir $dname
if[-d "$dname"]
then
echo "$dname directory already exists."
else
echo "$dname directory successfully created."
fi
read
;;
error message:
uan.sh: line 13: syntax error near unexpected token `then'
uan.sh: line 13: ` then'
The parser is seeing then outside of an if statement, because you don't have the keyword if in a command position. You have the word if[-d which the parser accepts as an ordinary command name; the parser doesn't know or care whether the command actually exists or not.
Whitespace is important:
if [ -d "$dname" ]
The brackets are supposed to remind you of syntax, but have probably caused more trouble than they have ever saved. [ is the command, and it requires ] as its final argument. Using the name test is much simpler and doesn't lull you into thinking the brackets are somehow special to the parser:
if test -d "$dname"

Can't populate variable from command in Bash

I'm new to bash scripting, and I'm working on a script where the user enters a username and gets a list of the associated information from /etc/passwd. Unfortunately, I seem to be having trouble populating a variable from a command. The error message I'm getting suggests the if statement isn't being entered into, but I'm not sure why.
The script currently looks like this:
#!/bin/bash
#readifs
FILE=/etc/passwd
read -p "Enter a username > " user_name
file_info=$(grep "^$user_name:" $FILE)
if [ -n "$file_info" ]; then
IFS=":" read user pw uid gid name home shell <<< "$file_info"
echo "User = '$user'"
echo "UID = '$UID'"
echo "GID = '$GID'"
echo "Full Name = '$name'"
echo "Shell = '$shell'"
else
echo "No such user '$user_name'" > &2
exit 1
fi
When I run it, using a valid username, I get the following two lines:
readifs.sh: line 20: syntax error near unexpected token `&'
readifs.sh: line 20: ` echo "No such user '$user_name'" > &2'
I'm pretty sure I'm missing something obvious, or doing something bash doesn't allow but I'm too new to catch. Can anyone point out and correct the error in my script?
Thank you to Charles Duffy for all the great feedback on not just this script, but bash scripting and Stack Overflow in general.
I was able to fix the script as I wanted. I removed the ^ and : from the file_info line, which was stopping the grep command from finding the line I wanted. I also renamed $UID and $GID to use lower case letters, and removed the space in "> &2".
Thank you again for your assistance.

Difference between ;& and ;; in a bash script

I'm copying a bash script from a Linux box to my Mac laptop and in the process, the script has started complaining about the usage of ;&.
With the error:
./build.sh: line 122: syntax error near unexpected token `;'
./build.sh: line 122: ` ;&'
There's a few uses in the script, but a short one is:
case ${OPTION} in
xxx)
export DEVICES=xxx
;;
yyy)
export DEVICES=yyy
;;
*)
echo "${OPTION}: Unknown device type: use xxx|yyy"
exit 1
;&
esac
;;
I've replaced all usages of ;& with ;; and I think that everything is ok, but I'm still curious what I've done. What's the difference between semicolon-ampersand and double semicolon in case statements?
From man bash:
Using ;& in place of ;; causes execution to continue with the list
associated
with the next set of patterns.
Since ;& occurs on the last pattern in the case statement, it should make no difference in that script.

Shell script - multiline statements work from command line (PuTTy) but not from script file

(I apologize in advance: I don't know whether my problem concerns rather the code syntax or the file system and would - in the latter case -rather fit into a Linux forum)
I'm trying to set up a litte shell script. But as soon as it comes to multiline statements, I came across a strange behaviour.
It took the example blow from a tutorial page:
number=1
if [ $number = "1" ]; then
echo "Number equals 1"
else
echo "Number does not equal 1"
fi
That works fine if I connect via PuTTy to my virtual linux machine (openSUSE 13.1) and copy&paste the code. It does what is expected.
But when I create a file named shell_test.sh (connected via SFTP Net Drive) containing the content below
#!/bin/bash
number=1
if [ $number = "1" ]; then
echo "Number equals 1"
else
echo "Number does not equal 1"
fi
and call it from the command line with bash shell_test.sh I get an error:
line 7: syntax error near unexpected token `fi'
The same happens with a for loop. The syntax error is then near the token "do".

How do I fix unexpected end of file in bash script?

I had much luck last time I submitted a question so here goes: I am trying to debug a somewhat large BASH script when I get the following error:
./test.sh: line 418: unexpected EOF while looking for matching `"'
./test.sh: line 427: syntax error: unexpected end of file
The code below starts at line 400:
echo "###########################################################"
echo
;;
4)
culebra_carriers
get_month
get_day
logs_cdrs
logs_wap
get_mdn
echo
echo "###########################################################"
echo
echo "Searching for activity of $mobileNumber on $MON $DAY......."
echo
zgrep $mobileNumber $HOME/culebrapeak/$LOGCDR/$CULEB/$MON/$WAPLOG
echo
echo "###########################################################"
echo
;;
esac
done
}
clear
main_menu
How do I make this error go away? It looks like I have the double quotes in all the right places... but this is only my 4th or 5th bash script... so please go easy on me.
I was, indeed, missing a double quote at the top of my script. Thanks to all for the help!
A good way to solve problems like this is to use a text editor that highlights code between quotes. Short of that, if the "find" feature of your text editor gives a count too, you may be able to use it to quantitatively detect start/end character symmetry problems. The highlighting from the find feature will aide your eye tremedously.

Resources