Maximum number from input by user


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Maximum number from input by user
# 8  
Old 04-30-2012
Here's what I have....


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

Last edited by Scrutinizer; 05-01-2012 at 03:11 AM.. Reason: code tags
# 9  
Old 05-01-2012
this would require you hit enter between each number. also, please don't start a new thread for the same thing.
# 10  
Old 05-01-2012
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.

Code:
echo "Please enter four numbers: \c" 
read max
echo "1"
for i in 1 2 3 4 do
echo "2"

etc.

# 11  
Old 05-01-2012
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?
# 12  
Old 05-01-2012
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:

Code:
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.
# 13  
Old 05-01-2012
This is what I have..

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

Last edited by Scrutinizer; 05-01-2012 at 02:28 PM..
# 14  
Old 05-01-2012
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?
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