Maximum number from input by user


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Maximum number from input by user
# 1  
Old 05-01-2012
Maximum number from input by user

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.
Code:
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

Moderator's Comments:
Mod Comment Please use code tags

Last edited by jim mcnamara; 05-01-2012 at 12:24 AM..
# 2  
Old 05-01-2012
because your if statement is incomplete.

Code:
for i in 1 2 3 4
do
    read number
    if [ $number -gt $max ]; then
        max=$number
    fi
done

# 3  
Old 05-01-2012
Code:
#!/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?
# 4  
Old 05-01-2012
Ok, so now I cannot get it to compute... I adjusted a couple things


Code:
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


Last edited by Scrutinizer; 05-01-2012 at 04:10 AM..
# 5  
Old 05-01-2012
You must come from another language, and are trying to make up syntax. The & is not only unnecessary but it does something completely undesired.
# 6  
Old 05-01-2012
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?
# 7  
Old 05-01-2012
You don't need to concatenate $max - strings get expanded, even when inside quotes. Change this:
Code:
echo "The maximum is: " & $max

to this
Code:
echo "The maximum is: $max"

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Deleting "user input line number" from a file using sed

Hi I want to delete a line from a txt file for which the line number is user input. Say when user selects 19, the 19th line would be deleted from the file. Can anyone please provide me with a sed one liner for the same... I tried sed -i. The interaction would be like this Enter the line... (4 Replies)
Discussion started by: sudeep.id
4 Replies

2. Shell Programming and Scripting

Counting the number of files within a directory input by the user

So I have a loop that stated if a directory exists or not. If it does it prints the number of files within that directory. I use this code... result=`(ls -l . | egrep -c '^-')` However, no matter which directory I input, it outputs the number "2" What is wrong here? (4 Replies)
Discussion started by: itech4814
4 Replies

3. UNIX for Dummies Questions & Answers

Deleting "user input line number" from a file using sed

Hi I want to delete a line from a txt file for which the line number is user input. Say when user selects 19, the 19th line would be deleted from the file. Can anyone please provide me with a sed one liner for the same... I tried sed -i. The interaction would be like this Enter the line to... (1 Reply)
Discussion started by: sudeep.id
1 Replies

4. UNIX for Dummies Questions & Answers

ls - maximum number of files

what is the maximum number ls can list down (6 Replies)
Discussion started by: karnan
6 Replies

5. UNIX for Dummies Questions & Answers

maximum number of arguments

Hi, What is the maximum number of arguments that could be passed to zsh ? To find out that I tried a simple script. And the maximum number of arguments that could be passed turned out to be 23394 #! /bin/zsh arg=1 i=1 subIndex=23000 while do arg=$arg" "$i i=$(($i + 1))... (9 Replies)
Discussion started by: matrixmadhan
9 Replies

6. Shell Programming and Scripting

Bash : how do i check the user input and make sure is only chracter or only number ?

Bash : how do i check the user input and make sure is only character or only number ? (7 Replies)
Discussion started by: CheeSen
7 Replies

7. UNIX and Linux Applications

handling maximum number characters in an input file

Hi, Can anyone help me? An input file has three lines. Each line should only be 2098 as number of characters however line 2 of the input file has more than the maximum number of characters, it exceeded up to 4098. What should I do so that could handle maximum number of characters? that it could... (1 Reply)
Discussion started by: chrysSty
1 Replies

8. UNIX for Dummies Questions & Answers

maximum number of input on solaris

Hi, Can anyone tell me what the maximum amount of input characters is on solaris command line? (standard ksh I think) (1 Reply)
Discussion started by: marcello
1 Replies

9. UNIX for Advanced & Expert Users

Maximum number of threads per user

Anybody knows how to setup Maximum number of threads per user or some other value on Sun Solaris 8. (1 Reply)
Discussion started by: s_aamir
1 Replies
Login or Register to Ask a Question

Featured Tech Videos