Bash Script 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 bash syntax error operand expected (error token is ) more about Stack Overflow the company Business Learn more about hiring developers or syntax error operand expected (error token is = ) posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community bash script syntax error unexpected end of file 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 error: operand expected up vote 4 down vote bash (error token is ") 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 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
Syntax Error In Expression (error Token Is
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-loop share|improve this question edited Dec 2 '12 at 19:08 asked Dec 2 '12 at 17:52 teutara 3671722 add a comment| 2 Answers 2 active oldest votes up vote 4 down vote accepted That's because at some point ${hitEnd[k]} expands to nothing (it is undefined). I get the same error with ((l<=)). You
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
Invalid Arithmetic Operator (error Token Is
Learn more about Stack Overflow the company Business Learn more about hiring developers expr: syntax error or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack bash check if variable is a number 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 http://stackoverflow.com/questions/13672022/bash-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 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 | http://stackoverflow.com/questions/23961463/bash-script-error-syntax-error-operand-expected-error-token-is-backup 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 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
communities company blog Stack Exchange Inbox Reputation and Badges sign up log in tour help Tour Start 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 http://askubuntu.com/questions/607240/why-am-i-getting-a-syntax-error-in-this-script more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Ask Ubuntu Questions Tags Users Badges Unanswered Ask Question _ Ask Ubuntu is a question and answer site https://github.com/rvm/rvm/issues/2958 for Ubuntu users and developers. Join them; it only takes a minute: Sign up Here's how it works: Anybody can ask a question Anybody can answer The best answers are voted up and rise to syntax error the top Why am I getting a syntax error in this script? up vote -2 down vote favorite When running the script below, I get an error: The script is: clear echo "Enter a num" read num if [ "$num" -ge 0]; then f=1 i=1 while [ $i -le "$num" ]; do f=`expr $f \* $i` i=`expr $i+1` done echo "The factorial of $num is $f" else echo "Enter positive (error token is number" fi Errors: Enter a num 5 prog4: 5: [: missing ] Enter positive number Thankyou bash share|improve this question edited Apr 9 '15 at 16:59 asked Apr 9 '15 at 15:59 Rahul Gohrani 12 2 I don't see a question here. I also don't see how this is an Ubuntu issue, as it's a pure generic coding question and belongs on Stack Overflow, not here. –Thomas Ward♦ Apr 9 '15 at 16:01 you forgot the backtick at the end of f=`expr .... And use indentation. –muru Apr 9 '15 at 16:02 2 I'm voting to close this question as off-topic because problems due to typos are hardly useful. –muru Apr 9 '15 at 16:06 2 @RahulGohrani bash questions are perfectly on topic here, don't worry. Just next time, write an actual question and use a more descriptive title. You should also mention the shell you're using (usually bash). Anyway, if the answer solved your issue, please accept it and then ask a new one for the next error. –terdon♦ Apr 9 '15 at 16:10 1 Just a suggestion, try not to use back-ticks . Instead prefer $(). UPDATE: The reason for not using is mostly because they are easier to miss.
Support Search GitHub This repository Watch 157 Star 3,438 Fork 804 rvm/rvm Code Issues 195 Pull requests 5 Projects 0 Pulse Graphs New issue `syntax error: operand expected (error token is "%")` on rvm install #2958 Closed shiftyp opened this Issue Jul 16, 2014 · 6 comments Projects None yet Labels None yet Milestone rvm 1.25 Assignees No one assigned 5 participants shiftyp commented Jul 16, 2014 I'm getting this error with rvm 1.25.28. I ran rvmsudo rvm get head so I should have the latest version. Output with the --debug flag: Warning! PATH is not properly set up, '/usr/local/rvm/gems/ruby-2.1.1@rails4/bin' is not at first place, usually this is caused by shell initialization files - check them for 'PATH=...' entries, it might also help to re-add RVM to your dotfiles: 'rvm get stable --auto-dotfiles', to fix temporarily in this shell session run: 'rvm use ruby-2.1.1@rails4'. ruby-2.1.2 - install ruby-2.1.2 - #already removed src/ruby-2.1.2 ruby-2.1.2 - #already removed rubies/ruby-2.1.2 Searching for binary rubies, this might take some time. Found remote file https://rvm.io/binaries/ubuntu/14.04/x86_64/ruby-2.1.2.tar.bz2 rvm_autolibs_flag=4 Checking requirements for ubuntu. requirements code for ubuntu loaded Found required packages: bash, curl, patch, bzip2, ca-certificates, gawk, g++, gcc, make, libc6-dev, patch, openssl, ca-certificates, libreadline6, libreadline6-dev, curl, zlib1g, zlib1g-dev, libssl-dev, libyaml-dev, libsqlite3-dev, sqlite3, autoconf, libgdbm-dev, libncurses5-dev, automake, libtool, bison, pkg-config, libffi-dev. Requirements installation successful. % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 50 0 50 0 0 46 0 --:--:-- 0:00:01 --:--:-- 46 bash: 31%: syntax error: operand expected (error token is "%") __rvm_rm_rf already gone: /usr/local/rvm/tmp/1474* \df -pK $HOME Size Used Avail Use% 1.5G 415M 939M 31% rvm version rvm 1.25.28 (master) by Wayne E. Seguin