"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.. |
sum=`expr $logicsubs + $renew`
No dollar on sum when setting it. Backquotes `` around expr command. Spaces around +. |
You've defined variable in a wrong way. Try this:-
Quote:
|
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 |
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:
|
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" |
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... |
Quote:
|
Hi,
"expr" or bash arithmetic operator might not handle float value. You may use "bc" command. Try: SUM=`echo "($subscription + $renew)" | bc` |
Try this:-
Quote:
|
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. |
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.. |
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 |
Quote:
I have given an example query... I am getting null in response from one of my queries. Please help if you any idea... Thanks |
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. |