954,525 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

For loop, please help

Hi!

I'm new in shell scripting, so please help me with my problem.
How can I do a bash script's for loop to find the smallest number?
If I have an example: 1 4 6 23 5 7 100 2
Answer: 1

It needs to be a for loop.

Any help is welcome!

Thanks

trume
Newbie Poster
19 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
 

Not pretty but it works and I moved 1 from the first position for testing:

#!/bin/bash
 y=0
for x in 4 6 23 1 5 7 100 2
do
if [ $y -eq 0 ]
then
y=$x
fi
if [ $x -lt $y ]
then
y=$x
fi
done
echo "Answer: $y"
rch1231
Posting Shark
959 posts since Sep 2009
Reputation Points: 119
Solved Threads: 142
 

Thank you very much rch1231! It's OK :)

trume
Newbie Poster
19 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
 

Haven't tested it, but it should work...

#!/bin/bash
firstloop=true
for x in 4 6 23 5 7 1 100 2; do
   if $firstloop -or [ $x -lt $y ]; then
      firstloop=false
      y=$x
   fi
done
echo "Lowest Number is $y"
Potion
Newbie Poster
5 posts since Oct 2010
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You