Let = Arithmetic 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 of this site About Us Learn more about Stack arithmetic syntax error ksh Overflow the company Business Learn more about hiring developers or posting ads with us Stack
Arithmetic Syntax Error Ksh Linux
Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community arithmetic syntax error bash of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Error in bash script arithmetic syntax up vote 2 down vote favorite Script: #!/bin/bash vpct=5.3 echo $((vpct*15)) expr: syntax error Error: ./abc.sh: line 5: 5.3: syntax error: invalid arithmetic operator (error token is ".3") I know I don't need a script to multiply 5.3 * 15, but this small script to single out the error. Please advise. bash share|improve this question edited May 4 at 12:04 Praveen 5,0012724 asked Feb 18 '11 at 11:34 Humble Debugger 1,23952443 add a comment| 6 Answers 6 active oldest votes up vote 3 down vote
Expr: Non-numeric Argument
accepted Besides bc, there are other tools you can tools you can try awk -v vpct="$VPCT" 'BEGIN{print vpct * 15}' echo $vpct | ruby -e 'print gets.to_f * 15 ' echo "$vpct 15 * p" | dc share|improve this answer answered Feb 18 '11 at 13:58 kurumi 14.7k22341 add a comment| up vote 4 down vote According to http://www.softpanorama.org/Scripting/Shellorama/arithmetic_expressions.shtml: Bash does not understand floating point arithmetic. It treats numbers containing a decimal point as strings. You should use bc to perform such calculations, just as in dogbane's solution, except that you should escape the expression using quotes so the * character doesn't cause unwanted shell expansion. echo "$vpct*15" | bc share|improve this answer answered Feb 18 '11 at 11:47 SirDarius 21.7k44661 add a comment| up vote 2 down vote You should use bc for floating point arithmetic: echo "$vpct*15" | bc share|improve this answer edited Feb 18 '11 at 11:48 answered Feb 18 '11 at 11:43 dogbane 137k42237324 add a comment| up vote 0 down vote $(( $vpct * 15 )) // (add a $ sign should do it) share|improve this answer answered Feb 18 '11 at 11:41 Cedric 1,86821535 maybe : vpct="5.3" ? –Cedric Feb 18 '11 at 11:41 --sorry I cannot try these solution, but will
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 bash expr about hiring developers or posting ads with us Unix & Linux Questions Tags Users Badges Unanswered Ask Question _ Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. 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 the top arithmetic syntax error in string compare up http://stackoverflow.com/questions/5040784/error-in-bash-script-arithmetic-syntax vote 2 down vote favorite I have written a shell script which takes file name as parameter e.g user/test.txt. I want to make this file parameter optional if user does not wish to provide file name he/she can give as "None" Inside script I'm just checking if filename parameter contains "None" if [ $filename -eq "NONE" ];then cmd; fi When "None" is passed as parameter script works fine but when user/test.txt is passed I get below error message which http://unix.stackexchange.com/questions/220333/arithmetic-syntax-error-in-string-compare I don't want to print on console arithmetic syntax error Can somebody help ? shell shell-script test share|improve this question edited Aug 5 '15 at 22:22 Gilles 372k696761127 asked Aug 5 '15 at 13:23 chhaya vishwakarma 210411 consider using bash's argument handling. It will help you be more consistent with other programs. –richard Aug 5 '15 at 13:28 3 Not part of this problem, but I'd put quotes around that variable ("$filename") in case, e.g., you end up with spaces in there. –goldilocks Aug 5 '15 at 13:36 1 @goldilocks, spaces or any character in$IFS, or wildcard characters. Funny how the quotes are put where they're not needed ("NONE") and not where they're needed (typical beginner mistake as that's true that it's counter-intuitive to anyone used to other kinds of languages). Note that [ itself is a glob character, though because it's not matched in a single word (or as a special case for zsh), it doesn't need to be quoted. –Stéphane Chazelas Aug 5 '15 at 14:26 add a comment| 1 Answer 1 active oldest votes up vote 7 down vote accepted [ bla bla bla ] is equivalent to test bla bla bla. From man test STRING1 = STRING2 the strings are equal STRING1 != STRING2 the strings are not equal INTEGER1 -eq INTEGER2 INTEGER1 is equal to INTEGER2 Therefore you need = not -eq. share|improve this answer edite
some complexity to doing math in shell script. To keep with script programming paradigm and allow for better math support, languages such Perl or Python would be better suited when math is desired. http://faculty.salina.k-state.edu/tim/unix_sg/bash/math.html However, it is possible to do math with shell script. In fact, over the https://www.mkssoftware.com/docs/man1/let.1.asp years, multiple facilities have been added to Unix to support working with numbers. Note As we will see, some of the commands used to facilitate math are a little picky about things like spaces around operators. declare¶ You may recall, that when the text book introduced the declare statement, it said that it is not syntax error always needed. So what do you get by declaring a variable to be an integer? The following example illustrates that a declared integer is not treated as a string. $ n=6/3 $ echo $n 6/3 $ declare -i n $ n=6/3 $ echo $n 2 When you do not need the declare statement is when you will use a program or built-in command to evaluate a math statement. expr¶ An arithmetic syntax error old Unix program that can evaluate math is expr. expr became popular in the days of the Bourne Shell, which did not support math. With Bash and Korn shell, it is generally not needed. Since it is a command, command substitution is needed. We are still treating the variable as a string. As you can see, it is picky about spaces. $ z=5 $ z=`expr $z+1` ---- Need spaces around + sign. $ echo $z 5+1 $ z=`expr $z + 1` $ echo $z 6 let¶ A Bash and Korn shell built-in command for math is let. As you can see, it is also a little picky about spaces, but it wants the opposite of what expr wanted. let also relaxes the normal rule of needing a $ in front of variables to be read. $ let z=5 $ echo $z 5 $ let z=$z+1 $ echo $z 6 $ let z=$z + 1 # --- Spaces around + sign are bad with let -bash: let: +: syntax error: operand expected (error token is "+") $let z=z+1 # --- look Mom, no $ to read a variable. $echo $z 7 BASH Arithmetic¶ With the BASH shell, whole arithmetic expressions may be placed inside double parenthesis. T
left to right with normal algebraic precedence (multiplication before addition, for example). let uses long integer arithmetic with no checks for overflow. No output is generated; the exit status is 0 if the last expression has a non-zero value and 1 otherwise. If you issue the command set -o korn the line let "expression" can also be expressed as ((expression)) This form avoids quoting and enhances readability. The portable way to write these commands is to use the POSIX command: : $((expression)) Expressions consist of named variables, numeric constants and operators. See Arithmetic Substitution in sh for syntax of expressions. EXAMPLE The commands let a=7 'b=4*2' c=b+1 echo $a $b $c produce 7 8 9 DIAGNOSTICS Possible exit status values are: 0 The last argument evaluated to a non-zero value. 1 The last argument evaluated to a zero value, or the expression contained a syntax error or tried to divide by zero. PORTABILITY Windows Vista. Windows 7. Windows Server 2008. Windows 8. Windows Server 2012. Windows 10. let is built into the KornShell on UNIX systems and is not a Bourne Shell command. NOTE This command is built into the MKS KornShell. AVAILABILITY PTC MKS Toolkit for Power Users PTC MKS Toolkit for System Administrators PTC MKS Toolkit for Developers PTC MKS Toolkit for Interoperability PTC MKS Toolkit for Professional Developers PTC MKS Toolkit for Enterprise Developers PTC MKS Toolkit for Enterprise Developers 64-Bit Edition PTC Integrity SEE ALSO Commands: expr, sh, test Using the MKS KornShell PTC MKS Toolkit 10.0 Documentation Build 6.