expr/bc syntax issue


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting expr/bc syntax issue
# 1  
Old 06-14-2010
expr/bc syntax issue

Hi,

I am facing issues in my shell script while trying to add two variables. Look at the snippet below for details:

# Initially variable SAMPLE1 and SAMPLE2 are set from different sources. Have trucated decimal to avoid numeric errors in expr
Code:
SAMPLE1=`/usr/bin/top -n 1 | head -3 | tail -1 | tr -d ' ' | cut -d"," -f4 | cut -d "%" -f1 | cut -d"." -f1`
SAMPLE2=`/usr/bin/top -n 1 | head -3 | tail -1 | tr -d ' ' | cut -d"," -f4 | cut -d "%" -f1 | cut -d"." -f1`
 
$  echo $SAMPLE1
96
$  echo $SAMPLE2
96

$  expr ${SAMPLE1} + ${SAMPLE2}
expr: non-numeric argument
 
$  echo "${SAMPLE1} + ${SAMPLE2}" | bc
(standard_in) 1: illegal character: ^[
(standard_in) 1: parse error
(standard_in) 1: illegal character: ^O
(standard_in) 1: illegal character: ^[
(standard_in) 1: parse error
(standard_in) 1: illegal character: ^[
(standard_in) 1: illegal character: ^O
(standard_in) 1: parse error
(standard_in) 1: illegal character: ^[
(standard_in) 1: illegal character: ^O
(standard_in) 1: illegal character: ^[
(standard_in) 1: parse error
(standard_in) 1: illegal character: ^[
(standard_in) 1: illegal character: ^O
(standard_in) 1: parse error

# However when set manually, it works.
Code:
$  SAMPLE1=96
$  SAMPLE2=96

$  expr ${SAMPLE1} + ${SAMPLE2}
192
$  echo "${SAMPLE1} + ${SAMPLE2}" | bc
192

Any suggestion to make this work?

Regards,
Animesh

Last edited by Franklin52; 06-14-2010 at 06:01 AM.. Reason: Please use code tags!
# 2  
Old 06-14-2010
Change
Code:
SAMPLE1=`/usr/bin/top -n 1 | head -3 | tail -1 | tr -d ' ' | cut -d"," -f4 | cut -d "%" -f1 | cut -d"." -f1`
SAMPLE2=`/usr/bin/top -n 1 | head -3 | tail -1 | tr -d ' ' | cut -d"," -f4 | cut -d "%" -f1 | cut -d"." -f1`

to
Code:
SAMPLE1=`/usr/bin/top -b -n 1 | head -3 | tail -1 | tr -d ' ' | cut -d"," -f4 | cut -d "%" -f1 | cut -d"." -f1`
SAMPLE2=`/usr/bin/top -b -n 1 | head -3 | tail -1 | tr -d ' ' | cut -d"," -f4 | cut -d "%" -f1 | cut -d"." -f1`

This User Gave Thanks to bartus11 For This Post:
# 3  
Old 06-14-2010
Thanks!!! That works. Just wondering what was the issue over here?

I assume the result fetched by my query was text rather than a numeral?
# 4  
Old 06-14-2010
No. Your query fetched characters used by top to control behaviour of the terminal. With -b option top's output is clear from those characters, so it can be saved to file or used in shell scripts.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Expr: non-numeric argument syntax error on line 1, teletype

Hi, I tried to look up the issue i'm experiencing, but i'm confused what's wrong with my script. After executing the script I'm getting the following error expr: non-numeric argument syntax error on line 1, teletype After some research, it seems that the problem relates to bc. I have... (1 Reply)
Discussion started by: nms
1 Replies

2. Shell Programming and Scripting

Dpkg Syntax Issue . . .

Greetings! I'm trying to get dpkg to just completely extract a multi-part *.deb archive set into a single folder called "output". Simple? Hmmmm.... dpkg --noforce --unpack *.deb /output/Console after ops: However, "--noforce" is taken directly from the helpfile Options listing as furnished... (8 Replies)
Discussion started by: LinQ
8 Replies

3. Shell Programming and Scripting

TCSH IF syntax issue

Hi All, I'm trying to write a simple if statement in TCSH and I honestly can't figure out what I'm doing wrong. I've played around with all sorts of permutations of syntax. if ($DESKTOP_SESSION == "kde") then replace "forceFontDPI=0" "forceFontDPI=96" --... (0 Replies)
Discussion started by: VerticalMule
0 Replies

4. Homework & Coursework Questions

How to lclear "expr: syntax error" eventhough everything looks fine?

Hi All, As per my knowledge in unix, my code looks fine. But still I am getting error (expr:syntax error). Please help me to resolve this error. Script : PRE_LBNO=0 PRE_DATE=0 TOT_PAY=0 TOT_REM=0 TOTAL=1 for Record_Type in `cut -c 1 Inputt.dat` do if ; then CURR_LBNO=` cut -c... (6 Replies)
Discussion started by: lathanandhini
6 Replies

5. Shell Programming and Scripting

Syntax Issue

Hello all. Need a bit of help... I have : previous_tmp0=`cat tmp0.txt` previous_tmp1=`cat tmp1.txt` previous_tmp2=`cat tmp2.txt` previous_tmp3=`cat tmp3.txt` previous_tmp4=`cat tmp4.txt` previous_tmp5=`cat tmp5.txt` previous_tmp6=`cat tmp6.txt` previous_tmp7=`cat tmp7.txt` Now I... (5 Replies)
Discussion started by: Junaid Subhani
5 Replies

6. Shell Programming and Scripting

Error with expr - "expr: syntax error"

Hi All, I'm writing a shell 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: #!/usr/bin/ksh... (6 Replies)
Discussion started by: jagari
6 Replies

7. UNIX for Dummies Questions & Answers

expr: syntax error

Hi All, This is a piece of code from one of my scripts: t1=`cat temp3.21447 | grep WEALTHTOUC_TRANS_20100409_233127.txt.txt.TRG | awk '{print $3}' | cut -c1-5` t2=`cat temp3.21447 | grep WEALTHTOUC_TRANS_20100409_233127.txt.txt.TRG | awk '{print $5}' | cut -c1-5` #t1=23:43... (5 Replies)
Discussion started by: prachiagra
5 Replies

8. UNIX for Dummies Questions & Answers

expr - Syntax error

hello to everyone, i am writing a shell script in unix and i use the following command: lnum= cut -f 1 -d : aa passline=`expr $lnum + 1` echo "$passline" with the following command i get the value that is stored in the first field of the file "aa" and i save it in the variable "lnum". i am... (2 Replies)
Discussion started by: omonoiatis9
2 Replies

9. UNIX for Dummies Questions & Answers

expr syntax

HELLO i have a problem i need to do this equation: final_result=*1.5. I am using expr like this: FINAL_RESULT=`expr (($RESULT1 + $RESULT2 / 2) \* 3) / 2` but it is wrong. Someone help me.thank you (2 Replies)
Discussion started by: psalas
2 Replies

10. Shell Programming and Scripting

expr: syntax error. why?

i want to get a substring from a string and used such shell script: var_year=`expr substr "07132006" 5 4` echo $var_year but i got such error message: expr: syntax error. why? Note: Kshell used on solaris 8. :confused: (5 Replies)
Discussion started by: robin.zhu
5 Replies
Login or Register to Ask a Question