Ask Ubuntu Asked by Malin on November 1, 2020
I have to do add an if statement to my while loop.
When I write ./scriptname 1
, it executes my while loop.
When I instead write ./scriptname hello
, it still executes my while loop.
What I want it to do is echo "no"
if I add letters after ./scriptname
but execute the while loop if I add numbers after ./scriptname
.
Does anyone know what I have done wrong in the script attached?
Its a good practice to keep your if-then-statement as short as possible, this way you dont need the else
statement.
#!/bin/bash
x=$1
y=$2
if [[ $x =~ (^$|[^0-9]) ]]; then
echo "no"
exit 0
fi
while (( $x <= 3 )); do
echo "$x $y"
((x++))
sleep 5
done
Or with a for-loop:
for ((z=3; $x<=$z; x++)); do
echo "$x $y"
sleep 5
done
Answered by bac0n on November 1, 2020
0 Asked on January 26, 2021 by john_connor
0 Asked on January 26, 2021 by ultranewbie
1 Asked on January 26, 2021 by user1725216
0 Asked on January 26, 2021
4 Asked on January 25, 2021 by rahul-raj-rai
2 Asked on January 25, 2021 by jorrit
1 Asked on January 24, 2021 by marievi
0 Asked on January 24, 2021 by arsh
0 Asked on January 24, 2021 by paul-wessex
1 Asked on January 24, 2021 by donttakemeseriously
0 Asked on January 23, 2021 by k-madhura-nadh-12a
4 Asked on January 23, 2021 by akaichi
0 Asked on January 23, 2021 by brad
0 Asked on January 23, 2021
0 Asked on January 23, 2021 by asocia
3 Asked on January 22, 2021 by thomas-ding
0 Asked on January 22, 2021 by salem-f
Get help from others!
Recent Questions
Recent Answers
© 2023 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP