cant figure out the error in this script (adding numbers in a string) using ubantu shell


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting cant figure out the error in this script (adding numbers in a string) using ubantu shell
# 1  
Old 10-19-2012
cant figure out the error in this script (adding numbers in a string) using ubantu shell

hii please help me this is the script
Code:
num=$1
sum=0

while [$num -ne 0]
do
x=`expr $num % 10`
sum=`expr $sum + $x`
num=`expr $num / 10`

done

echo "Summation is $sum"

it is giving error

pratyush@ubuntu:~$ sh shell.sh 123
shell.sh: 11: 123: not found
Summation is 0

please help!!!
# 2  
Old 10-19-2012
Code:
while [ $num -ne 0 ]

(space after [ and space before ] )
This User Gave Thanks to vgersh99 For This Post:
# 3  
Old 10-19-2012
Also, you don't have to use expr here.

Code:
let X=num%10

Much more efficient.
This User Gave Thanks to Corona688 For This Post:
# 4  
Old 10-19-2012
Quote:
Originally Posted by vgersh99
Code:
while [ $num -ne 0 ]

(space after [ and space before ] )
This worked thankyou very much Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Adding Two Array in shell script

Hi Experts, I've got this problem where I need to add two array in shell script such that that is an element is greater than 9 then it get further split into individual digit, something like below : Input :- array1=(2 6 8 9 10 12) array2=(5 4 6 8 12 14) Output :- array3=(7 1 0 1 4 1 7 2 2... (8 Replies)
Discussion started by: mukulverma2408
8 Replies

2. Shell Programming and Scripting

Adding (as in arithmetic) to numbers in columns in file, and writing new file with new numbers

Hi again. Sorry for all the questions — I've tried to do all this myself but I'm just not good enough yet, and the help I've received so far from bartus11 has been absolutely invaluable. Hopefully this will be the last bit of file manipulation I need to do. I have a file which is formatted as... (4 Replies)
Discussion started by: crunchgargoyle
4 Replies

3. Shell Programming and Scripting

Help with change significant figure to normal figure command

Hi, Below is my input file: Long list of significant figure 1.757E-4 7.51E-3 5.634E-5 . . . Desired output file: 0.0001757 0.00751 0.00005634 . . . (10 Replies)
Discussion started by: perl_beginner
10 Replies

4. Shell Programming and Scripting

help with adding up numbers

I have a file which has following contents which I want to add up. 28170.24 28170.24 28170.24 28170.24 28170.24 28170.24 28170.24 28170.24 28170.24 28170.24 28170.24 28170.24 28170.24 28170.24 139038.72 139038.72 139038.72 139038.72 (5 Replies)
Discussion started by: aksijain
5 Replies

5. Shell Programming and Scripting

Adding numbers in a string

I am writing a bash script on ubuntu11.10 I have some string having numbers and letter and want to add all the numbers together For example 1s2d23f I want to perform 1 + 2 + 23 and store it in a variable (3 Replies)
Discussion started by: kristinu
3 Replies

6. Shell Programming and Scripting

Script to replace numbers by string

Hi! I need the following script: - All numbers in a filename (0-9) have to be replace by a String ("Zero"-"Nine") - The script has to go through all the files in the current directory and has to replace the numbers as described above... I have no idea how to do this... Thanks! Michael (5 Replies)
Discussion started by: Michi21609
5 Replies

7. Shell Programming and Scripting

Shell script to check numbers!

Hello All, I have 3 types of files. The names of which starts with P,I,M like P********* D********* M********* now I need to do some operations witht hese files.. so if file name starts with P or p then do the operation for P file... fi else (20 Replies)
Discussion started by: smarty86
20 Replies

8. Shell Programming and Scripting

Adding options to a shell script

I want to add options to my shell script but having problems, my code so far is; #!/bin/bash lflag= iflag= while getopts 'l:i:' OPTION do case $OPTION in l) lflag=1 lval="$OPTARG" ;;... (1 Reply)
Discussion started by: paulobrad
1 Replies

9. Shell Programming and Scripting

add numbers in shell script

cat dailyreports | grep "Important list" | awk -F":" '{print $2}' | awk -F" " '{print $1}' hey guys, after running the above combination of cat and awk, i get the below output: 3 4 2 9 now, i need to add these numbers up all in one line. i dont know what to add to that cat and awk one... (2 Replies)
Discussion started by: Terrible
2 Replies

10. Shell Programming and Scripting

Adding 2 numbers

I would like to write a script with BASH to add two numbers (integer) and write the result to the standard output. Please help! (7 Replies)
Discussion started by: Viper01
7 Replies
Login or Register to Ask a Question