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
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
Jump to PostNot 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 …
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"
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"
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.