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

Recommended Answers

All 3 Replies

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"

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

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"
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.