Cygwin Expr Syntax Error
Contents |
'expr' To: cygwin at sources dot redhat dot com Subject: RE: Problems with 'expr' From: jens at uniweb expr syntax error multiplication dot se Date: Thu, 21 Sep 2000 17:50:31 +0330 expr syntax error in shell script (GMT+03:30) Hi, Silly me. How could I forgett/miss that. It's just that expr bash expr syntax error is not something I normaly use. So I did not think of that when I tried it out. Thanks. Jens Yllman >Hi expression syntax error Jens, > >Use quotes around * and (, like > >expr 3 '*' 4 > >Otherwise bash will expand * to all the files in the current dir. >( has special meaning to bash, too. > >expr '3 * 4' > >on the other hand,
Expression Syntax Error In Assembly
is evaluated to the string "3 * 4" > >See bash's manpage. > >Karsten > >> -----Original Message----- >> From: jens@uniweb.se [mailto:jens@uniweb.se] >> Sent: Donnerstag, 21. September 2000 14:32 >> To: cygwin@sources.redhat.com >> Subject: Problems with 'expr' >> >> >> Hi, >> >> I tried to use 'expr'. And it worked fine for this >> 'expr 1 + 2 + 3'. But if I try to use '(' or '*' it >> will print 'expr: syntax error'. Shouldn't the >> following work? 'expr 3 * 4'? >> >> Jens Yllman >> >> -- >> Want to unsubscribe from this list? >> Send a message to cygwin-unsubscribe@sourceware.cygnus.com >> >> > -- Want to unsubscribe from this list? Send a message to cygwin-unsubscribe@sourceware.cygnus.com Index Nav: [DateIndex] [SubjectIndex] [AuthorIndex] [ThreadIndex] Message Nav: [DatePrev][DateNext] [ThreadPrev][ThreadNext]
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 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 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 ] do r = ` expr $a % 10 ` s = ` expr $s + $r ` https://cygwin.com/ml/cygwin/2000-09/msg00742.html 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 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. http://stackoverflow.com/questions/30497074/i-am-getting-expr-syntax-errors-in-bash-shell-for-a-simple-program ((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: " number ((sum = 0)) while [[ $number -gt 0 ]]; do ((sum += number % 10)) ((number /= 10)) done echo "Sum of digits is $sum" You'll notice I've made some other minor changes which I believe enhances the readability, but you could revert back to the your original code if you wish and just use
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings http://unix.stackexchange.com/questions/109965/cygwin-shell-scripting-eval-and-echo and policies of this site About Us Learn more about Stack Overflow the http://www.linuxquestions.org/questions/linux-general-1/how-to-multiply-with-expr-expr-3-*-4-doesn't-work-though-expr-3-4-does-631064/ company Business Learn more 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 syntax error 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 Cygwin - Shell Scripting (eval and echo) up vote 1 down vote favorite I have the following file f1.txt: A=0 A=\`expr $A + 1\` and the following script file s1.sh for line in $(cat testfile.txt) do expr syntax error echo "$line" # eval $line done echo $A When I run the script using "sh s1.sh" I get the following output: A=0 A=`expr $A + 1` I was expecting the output of the echo to be: "A=`expr $A + 1`" I'd like to know why echo is putting newlines between the words? As well, when I uncomment the eval line I get the following error: s1.sh: eval: line 4: unexpected EOF while looking for matching ``' s1.sh: eval: line 5: syntax error: unexpected end of file I'd like to know what's wrong. shell-script cygwin share|improve this question edited Jan 19 '14 at 8:40 Timo 3,9271024 asked Jan 19 '14 at 4:22 EggHead 155117 add a comment| 2 Answers 2 active oldest votes up vote 2 down vote accepted Echo is putting newlines between words because it's splitting the input at the spaces, not at the newline. This is also why the eval doesn't work - it sees the first word, containing a ', and doesn't see the end of the line, which contains the closing ' Solution: Change the value of IFS (Internal Field Separator
Search HCL Search Reviews Search ISOs Go to Page... LinuxQuestions.org > Forums > Linux Forums > Linux - General How to multiply with expr? ( expr 3 * 4 doesn't work, though expr 3 + 4 does) User Name Remember Me? Password Linux - General This Linux forum is for general Linux questions and discussion. If it is Linux Related and doesn't seem to fit in any other forum then this is the place. Notices Welcome to LinuxQuestions.org, a friendly and active Linux Community. You are 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. 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, 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 03-27-2008, 12:49 PM #1 lumix Member Registered: Mar 2007 Distribution: Hardy (Gnome on Ubuntu 8.04) on Compaq N600c laptop Posts: 323 Rep: How to multiply with expr? ( expr 3 * 4 doesn't work, though expr 3 + 4 does) I've tried many combinations and googled this ad nauseam, but I keep getting a syntax error on multiply only. I've also tried using a bash shell. Ultimately, I want to assign a number to $x, then multiply $x * 25, fo