Hi
I am writing a script that will produce the total of the digits with in the number. Here is the script.
[root@***/]-bash# cat Sum_Digits
if [ "$? -lt 1" ]
then
echo "You must enter at least two digits"
echo "If you enter more than two digits after the base line program this script will print the sum of the digits"
echo "For examples if you enter $0 456 then this script will print the sum of the three digits as 15 (4+5+6)"
fi
n=$1
sd=0
sum=0
while [ "$n" -gt "0" ]
do
sd=`expr $n % 10`
sum=`expr $sum + $sd`
n=`expr $n / 10`
done
echo "The sum of the number: $sum"
The script works fine when I mentioned the number following the base line program as shown below
[root@*** /]-bash# ./Sum_Digits 45667
You must enter at least two digits
If you enter more than two digits after the base line program this script will print the sum of the digits
For examples if you enter ./Sum_Digits 456 then this script will print the sum of the three digits as 15 (4+5+6)
The sum of the number: 28
But it fails when I just enter the script with out any number
[root@crjueyudev01 /]-bash# ./Sum_Digits
You must enter at least two digits
If you enter more than two digits after the base line program this script will print the sum of the digits
For examples if you enter ./Sum_Digits 456 then this script will print the sum of the three digits as 15 (4+5+6)
./Sum_Digits: line 12: [: : integer expression expected
The sum of the number: 0
I tried with out quotes and the error message comes back as usual
Thanks