for Indexed for loop "Borrowed" from the c-shell. for (( initial condition[s]; test condition[s]; modifier )) do action list done For indexed for loop, bash allows the use of braces {} to encapsulate the action body instead of the do and done keywords. for (( initial condition[s]; limiting condition[s]; modifiers )) { action list }
for (( i=1; $( cat testcond ); i++ )) do echo $i done |
#The final echo will print out 2 for (( num=1; num<2; num++ )); { echo $num; } echo $num |
j=20 for (( i=1; i <= 10; i++,j-- )) do echo $i $j done |
#!/bin/bash # Get a random number from 0-99 target=$( expr $RANDOM % 100 ) # get first guess read -p "Guess : " guess # set guess count to 1 # test guess against target # if mis-guess then test for too high or low # increment guess count if missed. for (( i = 1; "$guess" != "$target"; i++ )) do if [ "$guess" -gt "$target" ] then echo "too high" else echo "too low" fi read -p "Guess : " guess done # show target and number of guesses. echo "$target in $i guesses" |