Bash Arithmetic Syntax Error
Contents |
Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages bash syntax error invalid arithmetic operator here. Search Forums Show Threads Show Posts Tag Search Advanced Search
Arithmetic Syntax Error In Shell Script
Unanswered Threads Find All Thanked Posts Go to Page... learn unix and linux commands ksh-script "arithmetic
Ksh Arithmetic Syntax Error
syntax error" comparing strings Shell Programming and Scripting Thread Tools Search this Thread Display Modes #1 10-14-2009 old_mike Registered
Bash Syntax Error Near Unexpected Token Done'
User Join Date: Oct 2009 Last Activity: 16 December 2009, 10:44 AM EST Posts: 5 Thanks: 0 Thanked 0 Times in 0 Posts ksh-script "arithmetic syntax error" comparing strings Hi all, Iīve already searched the forum but canīt find what i am doing wrong. I am trying to compare two variables using ksh under red hat. The bash syntax error near unexpected token else' error I get is: -ksh: .[123]: [: 75e245dfe25b753cfd69987a314adfe7: arithmetic syntax error Below are the lines of the script, line 123 is the if-line, MDA and MDB values are correct, near the brackets there is only one space: Code: MDA=`md5sum /tmp/ftp_dir_after_transfer | cut -d' ' -f1 ` MDB=`md5sum /tmp/ftp_dir_before_transfer | cut -d' ' -f1 ` if [ $MDA -eq $MDB ]; then echo "wrong" else echo "correct" fi Thanks for your support old_mike Last edited by old_mike; 10-14-2009 at 10:33 AM.. Remove advertisements Sponsored Links old_mike View Public Profile Find all posts by old_mike #2 10-14-2009 zaxxon code tag tagger Join Date: Sep 2007 Last Activity: 30 September 2016, 10:29 AM EDT Location: St. Gallen, Switzerland Posts: 6,481 Thanks: 158 Thanked 528 Times in 463 Posts To keep the forums high quality for all users, please take the time to format your posts correctly. First of all, use Code Tags when you post any code or data samples so others can easily read yo
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 bash syntax error near unexpected token fi' company Business Learn more about hiring developers or posting ads with us Unix & Linux bash syntax error near unexpected token then' Questions Tags Users Badges Unanswered Ask Question _ Unix & Linux Stack Exchange is a question and answer site for users of bash syntax error near unexpected token do' 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 http://www.unix.com/shell-programming-and-scripting/121329-ksh-script-arithmetic-syntax-error-comparing-strings.html the top Arithmetic syntax error with a number variable up vote 0 down vote favorite I am having a difficulty with an arithmetic syntax error. I am reading the name of text files from the command line and count the number of the lines of each file. NUM=$(wc -l "$text") and then I want to check whether NUM is odd or not. So I put, REMAINDER=$(( $NUM % 2 )) if [ $REMAINDER -ne 0 http://unix.stackexchange.com/questions/265383/arithmetic-syntax-error-with-a-number-variable ] ; then echo "Odd number" fi However, it seems like there is a problem with REMAINDER=$(( $NUM % 2 )) $NUM doesn't seem to be regarded as a number but a '.txt' file. When I checked NUM by itself and it worked fine... bash arithmetic share|improve this question edited Feb 24 at 22:10 Gilles 369k666681119 asked Feb 24 at 1:22 pigletwithcurls 62 add a comment| 1 Answer 1 active oldest votes up vote 4 down vote accepted If you print $NUM, after NUM=$(wc -l "$text") you probably will see (on Linux) a number and a filename on the same line, with some whitespace. For example: 1842 basic.c That string isn't a number, and you usually would read just the first token with your choice of shell/sed/awk, etc., to use that as a number. @steeldriver suggests this for getting just the number: NUM=$(wc -l < "$text") which works (tested with Debian). share|improve this answer edited Feb 24 at 1:31 answered Feb 24 at 1:25 Thomas Dickey 32.5k34083 1 ... or use wc -l < "$text" (wc should return only the count when reading input via stdin instead of from a named file) –steeldriver Feb 24 at 1:28 There are several variations (and aside from Linux, several systems that could be considered). –Thomas Dickey Feb 24 at 1:29 Tha
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 http://unix.stackexchange.com/questions/3648/why-am-i-getting-a-syntax-error-with-my-shell-arithmetic Us Learn more about Stack Overflow the 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 a minute: Sign up Here's how it works: Anybody can ask a question syntax error Anybody can answer 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 bash syntax error technique I used this: a=`expr $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,5501829 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 co