LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-02-2012, 09:09 AM   #1
Harish sharma
LQ Newbie
 
Registered: Nov 2012
Posts: 12

Rep: Reputation: Disabled
"Command not found" error using expr in shell script


renew=`mysql -D $DBDATABASE -u $DBUSER -p$DBPASSWORD -e "$renew"`
logicsubs=`mysql -D $DBDATABASE -u $DBUSER -p$DBPASSWORD -e "$lsubs"`

i have to results in
renew=20
logicsubs=30
$sum=expr $logicsubs+ $renew
$sum=`expr $subscription + $renew`
echo "$sum";
i want to sum these variables but error comes
expr: non-numeric argument
./revenue.sh: line 18: =: command not found

Please help soon..
 
Old 11-02-2012, 01:16 PM   #2
linosaurusroot
Member
 
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,Fedora,OpenBSD
Posts: 979
Blog Entries: 2

Rep: Reputation: 235Reputation: 235Reputation: 235
sum=`expr $logicsubs + $renew`

No dollar on sum when setting it. Backquotes `` around expr command. Spaces around +.
 
1 members found this post helpful.
Old 11-02-2012, 02:04 PM   #3
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
You've defined variable in a wrong way. Try this:-
Quote:
#!/bin/bash
renew=20
logicsubs=30
sum=$(expr $renew + $logicsubs)
echo "Sum is= $sum"

Last edited by shivaa; 11-02-2012 at 02:06 PM.
 
Old 11-03-2012, 02:50 AM   #4
Harish sharma
LQ Newbie
 
Registered: Nov 2012
Posts: 12

Original Poster
Rep: Reputation: Disabled
Unhappy

Hi shivaa ,
Thanx for reply,

subscription=`mysql -D $DBDATABASE -u $DBUSER -p$DBPASSWORD -e "select count(*) count from admin"`
renew=`mysql -D $DBDATABASE -u $DBUSER -p$DBPASSWORD -e "select count(*) count from admin"`
$sum=$(expr $subscription+ $renew)



I am getting value from db than adding these value then got error
expr: non-numeric argument
 
Old 11-03-2012, 06:44 AM   #5
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
1. You've again made a mistake! Do not use $ before sum variable, while defining it. Also put space between $subscription + $renew while using expr, as:
expr $subscription + $renew

2. Do not use backticks ("`" symbol) while defining a variable. Instead of this, use following pattern:
variable=$(<command>)

3. Instead of expr, better use following way to sum up 2 variables, as:
Quote:
subscription=$(mysql -D $DBDATABASE -u $DBUSER -p$DBPASSWORD -e "select count(*) count from admin")
renew=$(mysql -D $DBDATABASE -u $DBUSER -p$DBPASSWORD -e "select count(*) count from admin")
sum=$(($subscription + $renew))
echo "Sum is=$sum"
So make a try on this & let's know if it help you!

Last edited by shivaa; 11-03-2012 at 07:06 AM.
 
Old 11-05-2012, 02:19 AM   #6
Harish sharma
LQ Newbie
 
Registered: Nov 2012
Posts: 12

Original Poster
Rep: Reputation: Disabled
Smile

Thanks a lot shivaa its working now...


subscription=$(mysql -D $DBDATABASE -u $DBUSER -p$DBPASSWORD -e "select count(*)as '' from admin")
renew=$(mysql -D $DBDATABASE -u $DBUSER -p$DBPASSWORD -e "select count(*) as '' from admin")
sum=$(($subscription + $renew))
echo "Sum is=$sum"
 
Old 11-05-2012, 05:47 AM   #7
Harish sharma
LQ Newbie
 
Registered: Nov 2012
Posts: 12

Original Poster
Rep: Reputation: Disabled
Unhappy

Hi ,

subscription=$(mysql -D $DBDATABASE -u $DBUSER -p$DBPASSWORD -e "select count(*) count from admin")
renew=$(mysql -D $DBDATABASE -u $DBUSER -p$DBPASSWORD -e "select count(*) count from admin")
sum=$(($subscription + $renew))
echo "Sum is=$sum"

let's say subscription=238.000 +2084
2084: syntax error: invalid arithmetic operator (error token is ".000 +
2084")

or
subscription=null
renew =250
than again it wont sum these variables..

I want if variable got null value than it sums with 250 and it should result 250
and if value comes 238.000+250 then it should result 488.


Thanks in advance...
 
Old 11-05-2012, 05:58 AM   #8
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Quote:
Originally Posted by shivaa View Post
2. Do not use backticks ("`" symbol) while defining a variable. Instead of this, use following pattern:
variable=$(<command>)
It is not wrong to use backquotes rather than $( ) to generate the output of a command as a string to assign to a variable (or for any other purpose); it is a matter of personal preference. Details here.

Last edited by catkin; 11-05-2012 at 06:00 AM.
 
Old 11-05-2012, 06:39 AM   #9
ba_kirang
LQ Newbie
 
Registered: Apr 2012
Posts: 17

Rep: Reputation: Disabled
Hi,

"expr" or bash arithmetic operator might not handle float value. You may use "bc" command.

Try:

SUM=`echo "($subscription + $renew)" | bc`
 
Old 11-05-2012, 09:47 AM   #10
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
Try this:-
Quote:
...
...
renew=$(mysql -D $DBDATABASE -u $DBUSER -p$DBPASSWORD -e "select count(*) count from admin")
sum=$(awk "{print $subscription + $renew}")
echo "Sum = $sum"
 
Old 11-06-2012, 11:28 AM   #11
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
1)
Please use ***[code][/code]*** tags around your code and data, to preserve the original formatting and to improve readability. Do not use quote tags, bolding, colors, "start/end" lines, or other creative techniques.

2)
It may not be wrong to use `..` backticks, but it's not recommended, and generally deprecated. $(..) is much better all around. It's also posix-specified, so it's portable to all but the oldest shells. Read the link catkin provided.

3)
Similarly, all posix-compliant shells have integer arithmetic built in to them, so expr is pretty much completely unnecessary too. The $((..)) expansion form is fully portable, and similar to the command substitution brackets.

4)
On the other hand, as mentioned, very few shells have floating point math built-in, and expr doesn't support it either. You have to use an external command such as bc or awk for that.

5)
Finally, if the data is in integer form, you might check to make sure your mysql commands aren't producing dos-based newlines. The invisible carriage returns can confuse many commands.
 
Old 11-08-2012, 03:53 AM   #12
Harish sharma
LQ Newbie
 
Registered: Nov 2012
Posts: 12

Original Poster
Rep: Reputation: Disabled
Unhappy Sum null value to a number

Hi ,
subscription=$(mysql -D $DBDATABASE -u $DBUSER -p$DBPASSWORD -e "select count(*) count from admin")
renew=$(mysql -D $DBDATABASE -u $DBUSER -p$DBPASSWORD -e "select count(*) count from admin")
sum=$(($subscription + $renew))
echo "Sum is=$sum"


if
subscription=NULL and renew =250
then i want to sum these variable in third variable
sum=$(($subscription + $renew))
echo "Sum is=$sum"

but getting error
(standard_in) 1: illegal character: N
(standard_in) 1: illegal character: U
(standard_in) 1: illegal character: L
(standard_in) 1: illegal character: L
(standard_in) 1: parse error



Thanks in advance..
 
Old 11-08-2012, 05:03 AM   #13
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
1. if select using the count fn on a table with no matching rows, you get 0 (zero for no rows, not NULL).

2. NULL is only a valid value inside the SQL environment
 
Old 11-08-2012, 05:18 AM   #14
Harish sharma
LQ Newbie
 
Registered: Nov 2012
Posts: 12

Original Poster
Rep: Reputation: Disabled
Unhappy

Quote:
Originally Posted by chrism01 View Post
1. if select using the count fn on a table with no matching rows, you get 0 (zero for no rows, not NULL).

2. NULL is only a valid value inside the SQL environment
hi chrism01,

I have given an example query...
I am getting null in response from one of my queries.
Please help if you any idea...

Thanks
 
Old 11-08-2012, 05:24 AM   #15
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
Show your real query..

In any case, you'll need to convert the NULL into zero if you want to do arithmetic on it.
(see my point 2. above)

Try IFNULL() as demonstrated here http://stackoverflow.com/questions/1...ting-null-to-0

You really need to bookmark and read https://dev.mysql.com/doc/refman/5.1/en/
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Errors executing shell script: "command not found" and "no such file or directory" eko000 Linux - Newbie 1 01-14-2011 08:54 AM
LFS6.3 livecd "ls : command not found" error after "su - lfs" rotu Linux From Scratch 2 06-19-2008 04:59 PM
Standard commands give "-bash: open: command not found" even in "su -" and "su root" mibo12 Linux - General 4 11-11-2007 11:18 PM
Shell Script: Find "Word" Run "Command" granatica Linux - Software 5 07-25-2007 08:42 AM
Shell: command not found / Runs fine with "Run command" badbunny Linux - Newbie 1 01-22-2007 02:21 AM


All times are GMT -5. The time now is 02:44 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration