Little help


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Little help
# 1  
Old 06-07-2008
Little help

hello, I am trying to write a script so that when you enter 3 numbers it will give you the largest of them. When I run the script I get 2 errors, I can't figure out what is wrong so any help you could give me would be great! Thank you!
Enter three numbers and I will show you the largest of them >> \c
21
largest: line 9: test: : integer expression expected
largest: line 12: test: : integer expression expected
the largest number is
done

jawaibel@cis:~$ vi largest
1 #
2 #largest
3 #A sample program using the test command
4 #This program accepts three numbers and shos the largest of them
5 #
6 echo
7 echo "Enter three numbers and I will show you the largest of them >> \c"
8 read num1 num2 num3
9 if test "$num1" -gt "$num2" -a "$num1" -gt "$num3"
10 then
11 echo "the largest number is: $num1"
12 elif test "$num2" -gt "$num1" -a "$num2" -gt "$num3"
13 then
14 echo "the largest number is $num2"
15 else
16 echo "the largest number is $num3"
17 fi
18 echo "done"
19 echo
20 exit 0
~
~
~
"largest" 20L, 469C 12,1 All
# 2  
Old 06-08-2008
With your code:

Code:
$ ./largest

Enter three numbers and I will show you the largest of them >> \c
23 12 65
the largest number is 65
done

** The above works fine

Now, If you want the enter the numbers one by one(pressing enter)

$ ./largest

Enter three numbers and I will show you the largest of them >> \c
23
./largest: line 9: test: : integer expression expected
./largest: line 12: test: : integer expression expected
the largest number is
done

The problem you can see is because you are reading the values as

read num1 num2 num3

So

read num1
read num2
read num3

will solve your problem. But with this you can't enter your input numbers as "23 12 65".

//Jadu
# 3  
Old 06-08-2008
Thank you!
Login or Register to Ask a Question

Previous Thread | Next Thread
Login or Register to Ask a Question