Bash If Syntax Error Operand Expected
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 of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us bash syntax error operand expected error token is Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community
Bash (error Token Is ")
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up bash: syntax syntax error in expression error token is error using (( “$var” == “string” )) for comparisons up vote 1 down vote favorite I have this piece of code: if(("$op" == "q")); then which throws out this error: l5p3.sh: line 10: ((: + == q: syntax error: operand expected (error
Invalid Arithmetic Operator (error Token Is
token is "== q") what's the issue? How to compare $op with the letter $q ? linux bash share|improve this question edited Mar 27 '12 at 0:18 Charles Duffy 94.5k15102142 asked Mar 27 '12 at 0:04 FinalDestiny 4252822 1 As a rule, by the way, be more liberal in your spacing when writing shell scripts: if [ "$op" = "q" ] will work, whereas if[$op=q] will not. –Charles Duffy Mar 27 '12 at 0:16 add a comment| 4 Answers 4 active oldest votes up expr: syntax error vote 5 down vote accepted (( ... )) (for arithmetic expressions) is probably not what you want. Check the following: if (("asd" == "bla")); then echo test else echo bah fi It prints test in Bash and an error in a strictly bourne-compatible shell like dash. Try the following instead: if [[ $op == q ]]; then share|improve this answer edited Mar 27 '12 at 0:12 answered Mar 27 '12 at 0:06 Niklas B. 54.8k6121167 why not (( and )) ? That's how I worked before, with (( and )) and it worked just fine –FinalDestiny Mar 27 '12 at 0:07 @FinalDestiny (( and )) are for mathematical operations. You want [[ and ]] or [ and ] (which are quite different -- and the former is usually better) for comparing strings. –Charles Duffy Mar 27 '12 at 0:10 @NiklasB. (( $op == q )) would be actually be dereferencing $q and looking for a number there; it's a very different thing from [[ $op = q ]]. (Quotes aren't needed on the left-hand side of a [[ expression, unlike a [ one, and they're needed on the right-hand side only if that value could be interpreted as a glob pattern). –Charles Duffy Mar 27 '12 at 0:12 @Charles: Yeah, I noticed that. Already deleted that comment (: –Niklas B. Mar 27 '12 at 0:14 add a comment| up vote 1 down vote For string comparisons you want to use double square brackets not parenthesis. Th
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the
Integer Expression Expected
workings and policies of this site About Us Learn more about Stack
Bash Let
Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs unary operator expected Documentation Tags Users Badges 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 http://stackoverflow.com/questions/9881609/bash-syntax-error-using-var-string-for-comparisons them; it only takes a minute: Sign up Bash- syntax error: operand expected up vote 4 down vote favorite 1 I have two arrays that I want to loop in. I construct those properly and before going into for loop, I do echo them to be sure everything is ok with arrays. But when I run the script, it outputs http://stackoverflow.com/questions/13672022/bash-syntax-error-operand-expected an error: l<=: syntax error: operand expected (error token is "<=" I consulted the mighty Google and I understood it suffers from the lack of the second variable, but I mentioned earlier I do echo the values and everything seems to be OK. Here is the snippet.. #!/bin/bash k=0 #this loop is just for being sure array is loaded while [[ $k -le ${#hitEnd[@]} ]] do echo "hitEnd is: ${hitEnd[k]} and hitStart is: ${hitStart[k]}" # here outputs the values correct k=$((k+1)) done k=0 for ((l=${hitStart[k]};l<=${hitEnd[k]};l++)) ; do //this is error line.. let array[l]++ k=$((k+1)) done The variables in the for loop are echoed correctly but for loop won't work.. where am I wrong? # as gniourf_gniourf answered: "... At some point, k will reach the value ${#hitEnd[@]}, and this is exactly when hitEnd[k] is not defined and expands to an empty string! Bang!" meaning error output is displayed not at the beginning of the loop, but when k has a greater value than array's indices, pointing an index that array does not include... linux bash for-
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 the company Business Learn more about hiring developers or posting ads http://stackoverflow.com/questions/23961463/bash-script-error-syntax-error-operand-expected-error-token-is-backup with us Stack Overflow Questions Jobs Documentation Tags Users Badges 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: Sign up Bash Script error: “syntax error: operand expected (error token is ”/backup“)” up vote 1 down vote favorite I was trying to make a small script to check if backup drive is fine and its disk usage details, and to syntax error add it to nagios as an active check in servers. Once I written one, I got some unknown errors that I don't to correct. Script is: #!/bin/sh BACKUP_DRIVE='/backup' if [[ "$( grep $BACKUP_DRIVE /etc/fstab | awk '{print $2}')" -ne "0" ]]; then if [[ "$( df -h | grep $BACKUP_DRIVE | awk '{print $6}')" -ne "0" ]]; then if [[ "$( df -h|grep backup|awk '{print $5}' | cut -d "%" -f1)" -ge "95" ]]; then echo "CRITICAL: Backup Drive usage exceeded 95%" fi error token is else echo "CRITICAL: Backup Drive not mounted" fi else echo "CRITICAL: Backup Drive not added in fstab" fi Error is: # sh /usr/local/src/backupcheck.sh /usr/local/src/backupcheck.sh: line 6: [[: /backup: syntax error: operand expected (error token is "/backup") CRITICAL: Backup Drive not added in fstab It's getting error at the first if condition itself and not getting into the inner if conditions. Is there any corrections need to be done at point where variable $BACKUP_DRIVE mentioned? Please help as I am a beginner to bash scripting. linux bash shell share|improve this question edited May 30 '14 at 19:30 Andrew Medico 15.8k83663 asked May 30 '14 at 18:47 john wilson 4118 why are you using /bin/sh instead of /bin/bash –blade May 30 '14 at 19:19 The repeated grep | awk, df | awk, ... | awk | cut, and similar usage in here is inefficient, hard-to-read, and generally silly. Using the shell's read builtin to parse these contents a whole line at a time would be considerably simpler and more readable. –Charles Duffy May 30 '14 at 19:34 1 ...also, combining [[ ]] with -ge is silly as well -- any shell supporting [[ ]] will also support (( )), in which the far-more-readable >= can be used for numeric comparisons. –Charles Duffy May 30 '14 at 19:36 add a comment| 2 Answers 2 active oldest votes up vote 4 down vote accepted The immediate issue - likely the one triggering the syntax erro