I am trying to calculate the maximum number from four numbers input by the user. I have the following code, but it does not work. It says there's an error with the last line "done". Any help would be appreciated.
max=0
echo "Please enter four numbers: "
for i in 1 2 3 4
do
read number
max=`expr $max + $number`
if [ $number -gt $max ]
done
because your if
statement is incomplete.
for i in 1 2 3 4
do
read number
if [ $number -gt $max ]; then
max=$number
fi
done
#!/bin/bash
echo 'Please enter four numbers'
read max
for i in 1 2 3
do
read num
[ $num -gt $max ] && max=$num
done
echo 'maximum number is: $max"
Is this schoolwork?
Ok, so now I cannot get it to compute... I adjusted a couple things
max=0
echo "Please enter four numbers: \c"
for i in 1 2 3 4
do
read number
max=`expr $max + $number`
if [ $number -gt $max ]; then
max=$number
echo "The maximum is: " & $max
fi
done
You must come from another language, and are trying to make up syntax. The &
is not only unnecessary but it does something completely undesired.
Sorry I am new in the Unix world. I adjusted it to the way jim said, but it does nothing when I type in the numbers. Am I supposed to type spaces in between the numbers?
You don't need to concatenate $max - strings get expanded, even when inside quotes. Change this:
echo "The maximum is: " & $max
to this
echo "The maximum is: $max"
Here's what I have....
echo "Please enter four numbers: \c"
read max
for i in 1 2 3 4
do
read number
[ $number -gt $max ] && max=$number
done
echo "The maximum is: $max"
When I run it, for example, I type 1 2 3 4 and it does nothing...
this would require you hit enter between each number. also, please don't start a new thread for the same thing.
This looks like schoolwork, so I'll just give you some generic pointers for how to finish it.
1) There is a separate forum for schoolwork. Please put future posts in that forum.
2) RTFM. Type man bash
or man ksh
to learn the syntax of your shell. There is a ton of help in those manuals.
3) When you have no idea where a script is failing, put echo commands between every line so you can tell where it stops.
echo "Please enter four numbers: \c"
read max
echo "1"
for i in 1 2 3 4 do
echo "2"
etc.
It is actually a project for work. I am using it to find a data file with the highest values, so I altered it here a little to just find the maximum number. How can I adjust it to read each number separated by spaces instead of hitting enter after each number?
You should have started off asking that, rather than asking us to fix a specific syntax error, then.
You've just 1 line of many numbers? Give us more details on the actual input file, pasting a sample of it if you could, and the desired output. I'd say awk is better suited than the shell for such a task though. Say you've a file of just numbers:
awk '{for (i=1;i<=NF;i++) if (0+$i > max) max=0+$i}END{print "max: " max}' file
Should show the highest number in that file. It scans every line and every word. It assumes the max will be greater than 0 though, since it's not initialized.
This is what I have..
echo "Please enter four numbers: \c"
read max
for i in 1 2 3
do
read number
[ $number -gt $max ] && max=$number
done
echo "The maximum is: $max"
How would I embed the sample you provided into the above?
That line replaces the whole script. I'm still confused as to what you want to do. Do you want to read from the keyboard or a file? Does the file only have 4 numbers in it on 1 line?
I want to read four number on one single line input by the user. Then I want to calculate the maximum number based on those four numbers.
You would then use read with four variables, to get four numbers from 1 line without pressing enter.
read num1 num2 num3 num4
then some logic to find the highest one
max=$num1
for num in "$num2" "$num3" "$num4"; do
[ "$num" -gt "$max" ] && max=$num
done
echo "Max: $max"
or
read max other_nrs
for num in $other_nrs; do
if [ $num -gt $max ]; then
max=$num
fi
done
echo "Max $max"
1 Like
ah good idea. i've been using quoted arrays for such things that I did not even think to utilize word splitting to my advantage.