Expr 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
Bash Expr
more about Stack Overflow the company Business Learn more about hiring developers or posting expr syntax error multiplication ads with us Unix & Linux Questions Tags Users Badges Unanswered Ask Question _ Unix & Linux Stack Exchange is a
Expr Syntax Error In Shell Script
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 expr syntax error file The best answers are voted up and rise to the top Why am I getting a syntax error with my Shell arithmetic? up vote 4 down vote favorite I have a problem with a linux shell script (sh). I'm trying to write a script that calculates combinations, but I think I get overflows when trying to calculate faculties. So, I'm trying another technique. In this technique I used this: a=`expr expr substr syntax error $var1 \* $var2 / $var3` This is giving errors, and apperently, I can't do this: a=`expr ($var1 \* $var2) / $var3` because this gives errors as well. How do I do it? shell share|improve this question edited Oct 29 '11 at 4:13 John 2001310 asked Oct 30 '10 at 20:09 3sdmx 12114 What errors are you getting? That works in bash at least –Michael Mrozek♦ Oct 30 '10 at 20:20 expr: syntax error –3sdmx Oct 30 '10 at 20:21 add a comment| 2 Answers 2 active oldest votes up vote 6 down vote You can use dc for arbitrary precision results: precision=4 var1=3 var2=4 var3=5 a=`echo "$precision k $var1 $var2 * $var3 /p" | dc` should work in about every Unix variant since 1977 or so. If you know you have gnu dc available the more concise a=`dc -e "$precision k $var1 $var2 * $var3 /p"` can be used. share|improve this answer answered Oct 30 '10 at 20:36 msw 7,5701829 add a comment| up vote 2 down vote For your example with parens, you must escape them too: expr \( $var1 + $var2 \) / $var3 (and don't omit the spaces, or expr will complain). In bash you can also use built-in arithmetic expan
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
Bash Expr Index Syntax Error
posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss expr length syntax error 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
Rkhunter Expr Syntax Error
takes a minute: Sign up I am getting expr syntax errors in bash shell for a simple program up vote 1 down vote favorite #!/bin/bash clear echo "Enter a number" read a s = 0 while [ $a -gt 0 http://unix.stackexchange.com/questions/3648/why-am-i-getting-a-syntax-error-with-my-shell-arithmetic ] do r = ` expr $a % 10 ` s = ` expr $s + $r ` a = ` expr $a / 10 ` done echo "sum of digits is = $s" This is my code guys . I am getting a bunch of expr syntax errors. I am using the bash shell. Thanks! bash expr share|improve this question edited May 28 '15 at 4:08 Avinash Raj 126k84883 asked May 28 '15 at 4:08 Robo Smith 61 http://stackoverflow.com/questions/30497074/i-am-getting-expr-syntax-errors-in-bash-shell-for-a-simple-program r=$(expr $a % 10) –Avinash Raj May 28 '15 at 4:09 1 Take a look at shellcheck.net –Cyrus May 28 '15 at 5:33 Funny. ((sum=$(echo $number | sed -e 's/./&+/g' -e 's/$/0/' ) )) would do about the same, but the performance of @paxdiablo is much better. –Walter A May 28 '15 at 9:07 The only use-case for expr anymore is for regular expression matching in POSIX shell; any arithmetic it can perform can be done in-shell with $((...)), and bash has its own (superior) regular expression matching builtin. –chepner May 28 '15 at 13:46 add a comment| 1 Answer 1 active oldest votes up vote 3 down vote Your error is caused by the spaces surrounding the = in the assignments, the following replacements should work (I prefer $() to using backticks since they're much easier to nest): s=0 r=$(expr $a % 10) s=$(expr $s + $r) a=$(expr $a / 10) For example, s = 0 (with the spaces) does not set the variable s to zero, rather it tries to run the command s with the two arguments, = and 0. However, it's not really necessary to call the external expr1 to do mathematical manipulation and capture the output to a variable. That's because bash itself can do this well enough without resorting to output capture (see ARITHMETIC EVALUATION in the bash man page): #!/bin/bash clear read -p "Enter a number: " numbe
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 http://askubuntu.com/questions/607240/why-am-i-getting-a-syntax-error-in-this-script 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 Ask Ubuntu Questions Tags Users Badges Unanswered Ask Question _ Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute: Sign up Here's how syntax error it works: Anybody can ask a question Anybody can answer The best answers are voted up and rise to 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]; expr syntax error 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 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 (us