Case in Bash: "Line 4: Syntax Error Near Unexpected Token ')'"

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

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.

syntax error near unexpected token `case'

c.f. https://www.shellcheck.net/, which will check your bash syntax for you.

Line 6:
select menu in "Food1" "Food2" "Food3";
^-- SC1073: Couldn't parse this select loop. Fix to allow more checks.

Line 7:
case $REPLY in
^-- SC1058: Expected 'do'.
^-- SC1072: Expected 'do'. Fix any mentioned problems and try again.

shellcheck is your friend. :)

select menu in "Food1" "Food2" "Food3"
do : your logic here, AFTER the do
done

One more thing to simplify your code - rather than checking the case of everything, just force it for simple things like this.

$ declare -l foo
$ foo=BAR
$ echo $foo
bar

so if you do this with $again

declare -l again=y
while [[ y == "$again" ]]
do : ...

you never need to check for Y because it will be y. By the same token, at the bottom -

read again
until [[ "$again" =~ [yt] ]]
do printf "Y or T only please. Continue? [Y/t] "
read again
done

Bash with case syntax error `;;'

Problem are the parenthesis in your function calls. That is not valid BASH code.

Try the following:

read auswahl
case $auswahl in
"1")
echo "Sternbox"
;;
"2")
auswertung # note no () here!
;;
"3")
array
;;
"4")
exit 0
;;
*)
echo "Falsche Eingabe - Probieren Sie es nochmal"
esac

syntax error near unexpected token in a case

Had to surround it with double quotes:

case "${microserviceName}" in

syntax error near unexpected token ' - bash

It could be a file encoding issue.

I have encountered file type encoding issues when working on files between different operating systems and editors - in my case particularly between Linux and Windows systems.

I suggest checking your file's encoding to make sure it is suitable for the target linux environment. I guess an encoding issue is less likely given you are using a MAC than if you had used a Windows text editor, however I think file encoding is still worth considering.

--- EDIT (Add an actual solution as recommended by @Potatoswatter)


To demonstrate how file type encoding could be this issue, I copy/pasted your example script into Notepad in Windows (I don't have access to a Mac), then copied it to a linux machine and ran it:

jdt@cookielin01:~/windows> sh ./originalfile             
./originalfile: line 2: syntax error near unexpected token `$'{\r''
'/originalfile: line 2: `test() {

In this case, Notepad saved the file with carriage returns and linefeeds, causing the error shown above. The \r indicates a carriage return (Linux systems terminate lines with linefeeds \n only).

On the linux machine, you could test this theory by running the following to strip carriage returns from the file, if they are present:

cat originalfile | tr -d "\r" > newfile

Then try to run the new file sh ./newfile . If this works, the issue was carriage returns as hidden characters.

Note: This is not an exact replication of your environment (I don't have access to a Mac), however it seems likely to me that the issue is that an editor, somewhere, saved carriage returns into the file.

--- /EDIT

To elaborate a little, operating systems and editors can have different file encoding defaults. Typically, applications and editors will influence the filetype encoding used, for instance, I think Microsoft Notepad and Notepad++ default to Windows-1252. There may be newline differences to consider too (In Windows environments, a carriage return and linefeed is often used to terminate lines in files, whilst in Linux and OSX, only a Linefeed is usually used).

A similar question and answer that references file encoding is here: bad character showing up in bash script execution

Syntax error near unexpected token 'then'

There must be a space between if and [, like this:

#!/bin/bash
#test file exists

FILE="1"
if [ -e "$FILE" ]; then
if [ -f "$FILE" ]; then
echo :"$FILE is a regular file"
fi
...

These (and their combinations) would all be incorrect too:

if [-e "$FILE" ]; then
if [ -e"$FILE" ]; then
if [ -e "$FILE"]; then

These on the other hand are all ok:

if [ -e "$FILE" ];then  # no spaces around ;
if [ -e "$FILE" ] ; then # 1 or more spaces are ok

Btw these are equivalent:

if [ -e "$FILE" ]; then
if test -e "$FILE"; then

These are also equivalent:

if [ -e "$FILE" ]; then echo exists; fi
[ -e "$FILE" ] && echo exists
test -e "$FILE" && echo exists

And, the middle part of your script would have been better with an elif like this:

if [ -f "$FILE" ]; then
echo $FILE is a regular file
elif [ -d "$FILE" ]; then
echo $FILE is a directory
fi

(I also dropped the quotes in the echo, as in this example they are unnecessary)

bash: syntax error near unexpected token `)'

You have to end the switch-branch with ;; like

case "$x" in
a)
echo "a"
;;
*)
echo "not a"
;;
esac

and end the case with esac.

bash - syntax error near unexpected token ` '

I suspect you've made the mistake of assuming that /bin/sh is bash everywhere. A number of Linux distributions use simpler shells (often dash, sometimes ksh) as /bin/sh; if you want to use bash-specific extensions such as <(pipeline) syntax, you should explicitly use bash as the shell.

bash syntax error near unexpected token `(' when using parentheses in wildcard in script

You need to enable bash's extglob option with shopt -s extglob to use the negation pattern.

Change the echo line to

printf '%s\n' '#!/bin/bash' 'shopt -s extglob' 'ls ./a!(_)' > run.sh

You can disable extended globbing afterwards with shopt -u extglob.

Related:

  • Pattern Matching (Bash manual)


Related Topics



Leave a reply



Submit