0

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

Edited by trume: /

3
Contributors
3
Replies
4
Views
6 Years
Discussion Span
Last Post by Potion
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"
0

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

Edited by trume: /

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"

Edited by Potion: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.