Bash 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 more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow
Expr Index Syntax Error
Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow mac expr syntax error 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 math shell script 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 Syntax Error Multiplication
` 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 r=$(expr $a % 10) –Avinash Raj May 28 '15 at 4:09 1 Take a look at shellcheck.net
Expr Length Syntax Error
–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: " 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 enhan
Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts bash expr example and shell scripting languages here. Search Forums Show Threads Show
Bash Expr Float
Posts Tag Search Advanced Search Unanswered Threads Find All Thanked Posts Go to Page... unix bash expr division and linux commands - unix shell scripting Error with expr - "expr: syntax error" Shell Programming and Scripting Tags expr: syntax error http://stackoverflow.com/questions/30497074/i-am-getting-expr-syntax-errors-in-bash-shell-for-a-simple-program Thread Tools Search this Thread Display Modes #1 02-23-2012 jagari Registered User Join Date: Feb 2012 Last Activity: 21 May 2012, 10:09 AM EDT Posts: 6 Thanks: 3 Thanked 1 Time in 1 Post Error with expr - "expr: syntax error" Hi All, I'm writing a shell http://www.unix.com/shell-programming-and-scripting/177534-error-expr-expr-syntax-error.html script in KSH, where I want to store the filename, total record count and actual record count of all the source files. The source files reside in 4 different sub-folders under the same root folder. Below is code: Code: #!/usr/bin/ksh common_path_in="/interface_in/rsc" file_out="/interface_in/rsc/record_count.csv" tot_rec_count=-1 act_rec_count=-1 echo ${common_path_in} echo ${file_out} echo ${tot_rec_count} echo ${act_rec_count} set -A folders horizon mysteryshopper rcc reference echo ${folders[@]} for i in ${folders[@]} do ls -1 ${common_path_in}/$i/*.csv | xargs -n1 basename | while read filename do tot_rec_count=`wc -l ${common_path_in}/$i/*.csv | cut -f1 -d' '` echo ${tot_rec_count} act_rec_count=`expr ${tot_rec_count} - 1` echo ${act_rec_count} #echo "$filename,${tot_rec_count},${act_rec_count}" >> ${file_out} done done I'm getting output till the folder names, after that getting "expr: syntax error", for number of source files I've. Surprisingly among these errors only once I'm getting correct count outputs for the case, where the source folder contains only single data file. I've done the coding in vi editor and have not used MS editor. Please suggest. Reg
Start here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss http://serverfault.com/questions/407367/how-to-avoid-syntax-error-on-missing-command-line-arguments the workings and policies of this site About Us Learn more about http://serverfault.com/questions/501230/can-not-seem-to-get-expr-substr-to-work Stack Overflow the company Business Learn more about hiring developers or posting ads with us Server Fault Questions Tags Users Badges Unanswered Ask Question _ Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute: Sign up syntax error Here's how it works: Anybody can ask a question Anybody can answer The best answers are voted up and rise to the top How to avoid syntax error on missing command line arguments? up vote 0 down vote favorite How to avoid syntax error on missing command line arguments? Example shell script: var1=$1; var2=$2; echo $var1 echo $var2 var3=`expr expr syntax error $var1 + $var2`; echo $var3 Output : shell>sh shelltest 2 3 2 3 5 Output : shell>sh shelltest expr: syntax error As no arguments are passed, How can I avoid this and pass my own message instead of "expr: syntax error"? shell-scripting share|improve this question asked Jul 13 '12 at 5:21 Ahn 661510 add a comment| 2 Answers 2 active oldest votes up vote 2 down vote accepted You can check missing argument in shell script using $# variable. For Example: #!/bin/bash #The following line will print no of argument provided to script #echo $# USAGE="$0 --arg1
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 more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Server Fault Questions Tags Users Badges Unanswered Ask Question _ Server Fault is a question and answer site for system and network administrators. 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 Can not seem to get expr substr to work up vote 0 down vote favorite Can anyone tell me what I am doing wrong ? [ /jlr/sub/libexec/vmr-staging ] $ expr substr "onetwothree" 1 4 expr: syntax error I got the syntax from http://stackoverflow.com/a/220464/1150847 bash scripting shell-scripting share|improve this question asked Apr 20 '13 at 19:24 Jeffrey L. Roberts 1524 which OS du you use? substr is a non standard extention. The came be achieved with cut: echo "onetwothree" | cut -c 1-3 –kofemann Apr 20 '13 at 19:39 I am developing in OS X, Production is CentOS, cut worked! Please post it as an answer and I will check it off =] –Jeffrey L. Roberts Apr 20 '13 at 20:37 Id love to know why this was down voted –Jeffrey L. Roberts Apr 22 '13 at 17:43 add a comment| 1 Answer 1 active oldest votes up vote 1 down vote accepted The substr operation is a non standard extension and not supported on all OSes (probably linux only). The same be achieved with cut: echo "onetwothree" | cut -c 1-3 share|improve this answer edited Nov 25 '15 at 14:51 answered Apr 21 '13 at 7:02 kofemann 1,418817 add a comment| Your Answer draft saved draft discarded Sign up or log in Sign up using Google Sign up using Facebook Sign up using Email and Password Post as a guest Name Email Post as a guest Name Email discard By posting your answer, you agree to the privacy policy and terms of service. Not the answer you're looking for? Browse other questions tagged bash scripting shell-scripting or ask your own question. asked 3 years ago viewed 649 times active 10