finding biggest number


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting finding biggest number
# 1  
Old 06-19-2007
finding biggest number

I think my script is working but i am trying to understand while I am tracing to see if it's realli working..

can somebody please comment.. also. is there different way to write this in shell?

sh -x findbiggestnum 1 2 3
+ [ 3 -lt 3 ]
big=0
+ [ 1 ]
big=1
+ [ 2 ]
big=2
+ [ 3 ]
big=3
+ echo 3
3

big=0

for i in $*
do
if [ "$i" > "$big" ]
then
big=$i
else
:
fi
done
# 2  
Old 06-19-2007
can someone please tell me why if i do it below.. it doesn't work?
i put echo 1 2 3 4 5 since I will be getting the list of numbers from another program(just for demonstration) and I want to pipe the results into the script.. but it doesn't work..

echo 1 2 3 4 5 | script

################## script itself ###################
#!/usr/bin/sh

z=0
while read a
do
if [ "$a" -gt "$z" ]
then
z=$a
else
:
fi
done
# 3  
Old 06-19-2007
Quote:
Originally Posted by hankooknara
can someone please tell me why if i do it below.. it doesn't work?
i put echo 1 2 3 4 5 since I will be getting the list of numbers from another program(just for demonstration) and I want to pipe the results into the script.. but it doesn't work..

echo 1 2 3 4 5 | script

################## script itself ###################
#!/usr/bin/sh

z=0
while read a
do
if [ "$a" -gt "$z" ]
then
z=$a
else
:
fi
done

You are only sending one line to the script; read will read an entire line.
Code:
printf "%s\n" 1 2 3 4 5 | script

Unless there are very few numbers, it will be faster to use sort than a script:

Code:
z=$( sort -n | tail -1 )

# 4  
Old 06-19-2007
sort -n | tail -1 is something I should have thought of... that is perfect solution..
thank you.

however, for my learning solution(as well as any bignners here who are in same boat as I am )..

here was my original script

#!/usr/bin/sh

z=0


grep -i something /dir/somefile |
sed 's/[^=]*=\([0-9]*\) [^ ].*/\1/g' |
while read a
do
if [ "$a" -gt "$z" ]
then
z=$a
else
:
fi
done
echo $z


IF I do aboe script, it does not work...

but if I do

grep -i something /dir/somefile | sed 's/[^=]*=\([0-9]*\) [^ ].*/\1/g' | filename

it works.. ??

sed's output here produces file that looks like

1
2
3
4 already.. so it should work.. but the way i am running through the script, it does not..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Finding which is Running number

Hi All, I have a list of data in the listing file and I run row by row to process the record, but how to identify what is the current process line number ? Thanks. (2 Replies)
Discussion started by: ckwan
2 Replies

2. Shell Programming and Scripting

find biggest number inside file

Hi, I wanna find the biggest number inside of a file this is kind of example of file: 9 11 55 then i just wanna print out the biggest number i had try sed filenale | sort -k1,1n | paste -s -d',' - but i had no success ... (7 Replies)
Discussion started by: prpkrk
7 Replies

3. UNIX for Dummies Questions & Answers

Help on finding a number

Hello, Here is my question. I have a file which contains the html values. I have to find the decimal number from it. It is tagged around >68.74<. Please help me on it.. alt='' ><\/td>\n\t\t<td align=\"right\" class='data_table_text data_table_conv_averages_text' \ style='padding-right:... (1 Reply)
Discussion started by: sathyaonnuix
1 Replies

4. Shell Programming and Scripting

finding number in exact column

Dear all, I want to find a number in exact column but I don't know how to do it. Here is the thing, data is shown below, and I want to find 416 in the first column and print it out, how should I deal with it? Thank you very much! ab33 50S01S 958 279.068999 67.251013 -150.172544 67.250000... (5 Replies)
Discussion started by: handsonzhao
5 Replies

5. Shell Programming and Scripting

finding the number of dtsessions

I am trying to find out the number of dtsessions. using the command wc -l $FILE_NAME it works fine But when I try to assign this value to some variable using the command set NUM_DT_SESSION=$(wc -l $FILE_NAME) it shows error Variable syntax find below the complete command... (1 Reply)
Discussion started by: hiten.r.chauhan
1 Replies

6. Shell Programming and Scripting

Finding the nice(ni) number with PID?

Hi, is there a command that takes the PID of a process and that only diplays it's ni number? I`m pretty sure it would require pipes but I tried a few things that ended up miserably... Since the ps command doesn't show the ni unless I do ps -o ni but then I can't find a way to search the right... (2 Replies)
Discussion started by: Yakuzan
2 Replies

7. Shell Programming and Scripting

finding the line number from a grep ?

Hi there does anybody know how i can get the line number from an entry or entries in a file ?? for example if i had a file test1 test2 test3 test1 and i needed to get the line numbers for all instances of test1 in that file with the answer being (1,4) Would anybody be able... (7 Replies)
Discussion started by: hcclnoodles
7 Replies

8. Shell Programming and Scripting

Finding larg number and add one

Dear Friends, Below is the output: 901 9010 9010 9011 9011 9011 9012 9013 9014 9015 9016 9017 9018 9019 902 9020 9021 9022 (4 Replies)
Discussion started by: sfaqih
4 Replies

9. Solaris

Finding port number !!

Hi all , I want know the port no on which a particular application is running. How to find that? Thanks in anticipation (11 Replies)
Discussion started by: kumarmani
11 Replies

10. Shell Programming and Scripting

Need help finding number of logins!

Let's say I am trying to find out how many times someone has logged into the machine. Use last to display all the different logins, several names pop up, some multiple times. All I have been able to figure out is the total number of logins, but I can't determine how to compare if $1(user name) is... (3 Replies)
Discussion started by: TiznaraN
3 Replies
Login or Register to Ask a Question