for if until

21 Mar 2019

Shell中的循环语句

1. for

for var in list
do
    command1
    command2
    ...
    commandN
done

2. if

if commands; then
commands
[elif commands; then
commands...]
[else
commands]
fi

or

#!/bin/bash

if [ "$#" -gt 0 ]
then
    echo "There's Beans"
fi

if [ "$1" = "cool" ]
then
echo "Cool Beans"
fi

2.1 file conditions

if [ -f “$1” ] then echo “$1 is a file” else echo “$1 is not a file” fi


### 3. until
```sh
#!/bin/sh

a=0

until [ ! $a -lt 10 ]
do
   echo $a
   a=`expr $a + 1`
done

the output is 0 to 9.