Bash Case Syntax Error
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies bash syntax error near unexpected token done' of this site About Us Learn more about Stack Overflow the company Business bash syntax error near unexpected token else' Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges
Bash Syntax Error Near Unexpected Token Fi'
Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute:
Bash Syntax Error Invalid Arithmetic Operator
Sign up bash syntax error when using case statement up vote 0 down vote favorite I have bash script that I use regularly in my job to automate a large job. I was making some changes today, but everything seemed fine. The script itself is about 1700 lines long. The first part of the script is all good and runs through all the bash syntax error near unexpected token then' user input and logic just fine. It then proceeds into the core of the script and stops working at exactly line 875 (tested the script with bash -x to find the break point). However, it breaks with the following error: script.sh: line 1341: syntax error near unexpected token `;;' script.sh: line 1341: ` ;;' Line 1341 is in the middle of a case statement. The following code is the beginning of that block of code where it is breaking: if [[ $VAR1 = "TRUE" && $VAR2 = "VAL2" ]]; then VERSION=`XXXXXXXXXXXXXXXX` ## Set variables based on location $VAR3 case $VAR3 in STR1 ) case $VERSION in STR2 ) VAR4 = "STR5" VAR5 = "STR6" VAR6 = "STR7" VAR7 = "STR8" Line 1341 ---> ;; STR3 ) VAR4="STR9" VAR5="STR10" VAR6="STR11" VAR7="STR12" ;; STR4 ) VAR4="STR13" VAR5="STR14" VAR6="STR15" VAR7="STR16" ;; esac VAR8="STR17" VAR9="STR18" VAR10=1 VAR11="STR19" ;; Because of the sensitive nature of what I do, I obviously had to remove quite a bit of information. I know this may make things more difficult to help me with. However, all VAR##="STR##" are standard variable declarations with string va
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow
Bash Syntax Error Near Unexpected Token Do'
the company Business Learn more about hiring developers or posting ads with us Stack bash syntax error near unexpected token newline' Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of bash syntax error unexpected end of file 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Shell Scripting Case issue up vote 0 down vote favorite I am trying to make a shell script http://stackoverflow.com/questions/3818763/bash-syntax-error-when-using-case-statement that takes arguments and then performs the appropriate mathematical equation depending on the second variable. I am receiving "syntax error near unexpected token `in" and "case "$y" in" #!/bin/sh echo "Variable name \$1 $1" echo "Variable name \$2 $2" echo "Variable name \$3 $3" y=$2 echo $y case $y in '+') x=`expr $1 + $3` echo $x ;; '-') x=`expr $1 - $3` echo $x ;; '\*') x=`expr $1 \* $3` echo http://stackoverflow.com/questions/5494522/shell-scripting-case-issue $x ;; '/') x=`expr $1 / $3` echo $x ;; '%') x=`expr $1 % $3` echo $x ;; '*') echo "Invalid option" exit 1 ;; esac shell share|improve this question edited Mar 31 '11 at 1:58 Andrew Cooper 25.3k24688 asked Mar 31 '11 at 1:54 Rumel 547520 add a comment| 3 Answers 3 active oldest votes up vote 0 down vote accepted try this. #!/bin/sh echo "Variable name \$1 $1" echo "Variable name \$2 $2" echo "Variable name \$3 $3" y=$2 echo $y case $y in '+') x=`expr $1 + $3`; echo $x ;; '-') x=`expr $1 - $3`; echo $x ;; '\*') x=`expr $1 * $3`; echo $x ;; '/') x=`expr $1 / $3`; echo $x ;; '%') x=`expr $1 % $3`; echo $x ;; *) echo "Invalid option" exit 1 ;; esac Some hints: (1) '*' should be * at the end of case struct. * means all other case, while '*' means just *. (2) x=expr $1 + $3 should be x=`expr $1 + $3`, ``(the backtick key) means to execute some command in sub shell and assign the output to x. (3) to have multiple commands in one line, ';' should be used. x=`expr $1 % $3`; echo $x share|improve this answer answered Mar 31 '11 at 2:07 tianyapiaozi 346141
Search Tutorials/Articles Search HCL Search Reviews Search ISOs Go to Page... LinuxQuestions.org > Forums > Non-*NIX Forums > Programming syntax error near unexpected token ` User Name Remember Me? Password Programming This forum http://www.linuxquestions.org/questions/programming-9/syntax-error-near-unexpected-token-%60-441992/ is for all programming questions. The question does not have to be directly related to Linux and any language is fair game. Notices Welcome to LinuxQuestions.org, a friendly and active Linux Community. You are http://www.thegeekstuff.com/2010/07/bash-case-statement currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. syntax error Registration is quick, simple and absolutely free. Join our community today! Note that registered members see fewer ads, and ContentLink is completely disabled once you log in. Are you new to LinuxQuestions.org? Visit the following links: Site Howto | Site FAQ | Sitemap | Register Now If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, bash syntax error click here. Having a problem logging in? Please visit this page to clear all LQ-related cookies. Introduction to Linux - A Hands on Guide This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter. For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own. Click Here to receive this Complete Guide absolutely free. Search this Thread 05-05-2006, 04:09 PM #1 mattyspatty LQ Newbie Registered: Jun 2005 Posts: 9 Rep: syntax error near unexpected token ` /csgo: line 1: syntax error near unexpected token `in /csgo: line 1: `case "$1" in i am making a script so i can easily stop/start/restart my CS server. i found some on the web and mashed em together to c
and characters. Case statement is not a loop, it doesn't execute a block of code for n number of times. Instead, bash shell checks the condition, and controls the flow of the program. In this article let us review the bash case command with 5 practical examples. The case construct in bash shell allows us to test strings against patterns that can contain wild card characters. Bash case statement is the simplest form of the bash if-then-else statement. Syntax of bash case statement. case expression in pattern1 ) statements ;; pattern2 ) statements ;; ... esac Following are the key points of bash case statements: Case statement first expands the expression and tries to match it against each pattern. When a match is found all of the associated statements until the double semicolon (;;) are executed. After the first match, case terminates with the exit status of the last command that was executed. If there is no match, exit status of case is zero. Bash Case Example 1. Sending Signal to a Process The following script accepts the signal number and process id as it's arguments, and sends the signal to a given process id using signal name. This script is to demonstrate the usage of the case statement. $ cat signal.sh #!/bin/bash if [ $# -lt 2 ] then echo "Usage : $0 Signalnumber PID" exit fi case "$1" in 1) echo "Sending SIGHUP signal" kill -SIGHUP $2 ;; 2) echo "Sending SIGINT signal" kill -SIGINT $2 ;; 3) echo "Sending SIGQUIT signal" kill -SIGQUIT $2 ;; 9) echo "Sending SIGKILL signal" kill -SIGKILL $2 ;; *) echo "Signal number $1 is not processed" ;; esac In the above example: $1 and $2 are the signal number and process id respectively. Using kill command, it sends the corresponding signal to the given process id. It executes the sleep command for a number of seconds. The optional last comparison *) is a default case and that matches anything. Usage of the above shell script: Find out the process id of the sleep command and send kill signal to that process id to kill the process. $ sleep 1000 $ ps -a | grep sleep 23277 pts/2 00:00:00 sleep $ ./signal.sh 9 23277 Sending SIGKILL signal $ sleep 1000 Killed Also, refer to our earlier kill article - 4 methods to kill a process. Bash Case Example. 2. Pattern Match in a File This example prints the number of lines,number of words and delete the lines that matches the given pattern. $ cat fileop.sh #!/bin/bash # Check 3 arguments are given # if [ $# -lt 3 ] then echo "Usage : $0 option pattern filename" exit fi # Check the given file is exist # if [ ! -f $3 ] then ech