Hi, I am taking my first UNIX class in school and I am very confused on my 3rd script I have to write. I am hoping some of you can help me. Well what I have to do is write a script that when I execute it is takes the largest number. For example say I write a script called largest, I then execute it ./largest 50 100 90 5, it should tell me the largest number in the set. So it should say "The largest number is 100." You should be able to enter up to 9 numbers. Okay well this is what I have and I don't know why it isn't working.
count=$#
args=("$@")
max=$1
while [ $count -gt 0 ]
do
num=${args[count]}
if test num -gt max
then
let max=num
let count=count-1
fi
done
echo "The largest number is $max"
echo "Done"
exit 0
what I am trying to do is set the first number entered into max then go through the array each time assigning the value to num, then I want to test the value in num to see if its greater then max and if it is then that value will take over the max value. For example say I enter ./largest 50 10 6 90. max will be set at 50 then num would be set to whatever is in array 4 which would be 90. Then is will see if num(90) is greater then max(50) and since it is max will become 90. then it will go down the list so num will become the next array which is 6 and do the same test and since 6 isnt greater than 50 max will remain as 50. So it will do this for all the numbers and the largest number will be in max. Then "echo "The largest number is $max"" will print out the largest number. Seems like it should work but it keeps saying "./sum line 8: test: num: integer expression expected" no clue what to do. Please help.vi
count=$#
args=("$@")
max=$1
while [ $count -gt 0 ]
do
num=${args[count]}
if test num -gt max
then
let max=num
let count=count-1
fi
done
echo "The largest number is $max"
echo "Done"
exit 0
what I am trying to do is set the first number entered into max then go through the array each time assigning the value to num, then I want to test the value in num to see if its greater then max and if it is then that value will take over the max value. For example say I enter ./largest 50 10 6 90. max will be set at 50 then num would be set to whatever is in array 4 which would be 90. Then is will see if num(90) is greater then max(50) and since it is max will become 90. then it will go down the list so num will become the next array which is 6 and do the same test and since 6 isnt greater than 50 max will remain as 50. So it will do this for all the numbers and the largest number will be in max. Then "echo "The largest number is $max"" will print out the largest number. Seems like it should work but it keeps saying "./sum line 8: test: num: integer expression expected" no clue what to do. Please help.vi