LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   "Command not found" error using expr in shell script (https://www.linuxquestions.org/questions/linux-newbie-8/command-not-found-error-using-expr-in-shell-script-4175435302/)

Harish sharma 11-02-2012 08:09 AM

"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..

linosaurusroot 11-02-2012 12:16 PM

sum=`expr $logicsubs + $renew`

No dollar on sum when setting it. Backquotes `` around expr command. Spaces around +.

shivaa 11-02-2012 01:04 PM

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"

Harish sharma 11-03-2012 01:50 AM

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

shivaa 11-03-2012 05:44 AM

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!

Harish sharma 11-05-2012 01:19 AM

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"

Harish sharma 11-05-2012 04:47 AM

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...

catkin 11-05-2012 04:58 AM

Quote:

Originally Posted by shivaa (Post 4821299)
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.

ba_kirang 11-05-2012 05:39 AM

Hi,

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

Try:

SUM=`echo "($subscription + $renew)" | bc`

shivaa 11-05-2012 08:47 AM

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"

David the H. 11-06-2012 10:28 AM

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.

Harish sharma 11-08-2012 02:53 AM

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..

chrism01 11-08-2012 04:03 AM

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

Harish sharma 11-08-2012 04:18 AM

Quote:

Originally Posted by chrism01 (Post 4824820)
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

chrism01 11-08-2012 04:24 AM

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/


All times are GMT -5. The time now is 11:05 AM.