美文网首页
shell判断/循环语法

shell判断/循环语法

作者: 冷月成双 | 来源:发表于2019-10-21 22:37 被阅读0次

if判断结构

if expression; then
    command1
    command2
    ...
fi

例 :
:~$ cat score01.sh

#!/bin/bash

echo -n "Please input a score : "
read SCORE
if [ "$SCORE" -lt 60 ]; then
    echo "C"
fi
if [ "$SCORE" -lt 80 -a "$SCORE" -ge 60 ]; then
    echo "B"
fi
if [ "$SCORE" -ge 80 ];then
    echo "A"
fi

执行结果:

:~$ bash score01.sh 
Please input a score : 95
A
:~$ bash score01.sh 
Please input a score : 75
B
:~$ bash score01.sh 
Please input a score : 45
C

if/else 判断结构

if expression; then
    command
else
    command
fi

例:判断文件是否存在
cat check_file.sh

#!/bin/bash
 FILE=/var/log/syslog
# FILE=/var/log/syslog1

if [ -e $FILE ];then
    echo "$FILE exists"
else
   echo "$FILE not exists"
fi

执行命令:

:~$ bash check_file.sh 
/var/log/syslog exists
:~$ bash check_file.sh 
/var/log/syslog1 not exists

if/else 嵌套判断结构

if expression1; then
    command1
else
    if expression2; then
         command2
    else
         command3
    fi
fi

if/elif/else 结构

if expression1; then
    command1
elif expression2; then
    command2
elif expression3; then
    command3
...
fi

case判断结构

case VAR in
var1) command1;;
var2) command2;;
var3) command3;;
...
*) command;;
esac

例:
~$ cat os_type.sh

#!/bin/bash
OS=$(uname -s)
echo "$OS"
case "$OS" in
FreeBSD) echo "this is FreeBSD" ;;
CYGWIN_NT-5.1) echo "this is cygwin" ;;
SunOS) echo "this is Solaris" ;;
Darwin) echo "this is Mac OSX" ;;
AIX) echo "this is AIX" ;;
Linux) echo "this is Linux" ;;
*) echo "faild to identify this os" ;;
esac

执行命令 :

:~$ bash os_type.sh 
Linux
this is Linux

for循环

带列表的for循环

for VARIABLE in (list)
do
    command
done

例:
cat fruit.sh

#!/bin/bash

fruits="apple orange banana pear"

for FRUIT in ${fruits}
do
   echo "$FRUIT is my favorite fruit"
done

echo "no more fruits"

执行 : bash fruit.sh

apple is my favorite fruit
orange is my favorite fruit
banana is my favorite fruit
pear is my favorite fruit
no more fruits
#!/bin/bash

for VAR in {1..5}
do
   echo "Loop $VAR times"
done

还可以使用seq命令:

sum=0

for var in `seq 1 100`
do
    let "sum+=var"
done

echo "total = $sum"

结果为 : total = 5050

还可以使用命令:

for var in $(ls)
do
    ls -l $var
done

会打印出文件列表

一般的for循环

for(i=1;i<=10;i++)这样的结构

for ((expression1; expression2; expression3))
do
    command
done

while循环

while expression
do
    command
done

例 :
cat while.sh

CONTER=5

while [[ $CONTER -gt 0 ]]
do
    echo -n "$CONTER "
    let "CONTER-=1"
done
echo

执行命令 : bash while.sh

5 4 3 2 1 

while 按行读取文件

例 :
cat student_info.sh

john    30    boy
sue     28    girl
wang    25    boy
xu      23    girl

while 循环按行读取:
cat while_student.sh

#!/bin/bash

while read LINE
do
    NAME=`echo $LINE | awk '{print $1}'`
    AGE=`echo $LINE | awk '{print $2}'`
    Sex=`echo $LINE | awk '{print $3}'`
    echo "my name is $NAME,I'm $AGE years old,I'm a $Sex"
done < student_info.txt

也可以这样:

cat student_info.txt | while read LINE
do
    NAME=`echo $LINE | awk '{print $1}'`
    AGE=`echo $LINE | awk '{print $2}'`
    Sex=`echo $LINE | awk '{print $3}'`
    echo "my name is $NAME,I'm $AGE years old,I'm a $Sex"
done 

执行命令:bash while_student.sh
结果为:

my name is john,I'm 30 years old,I'm a boy
my name is sue,I'm 28 years old,I'm a girl
my name is wang,I'm 25 years old,I'm a boy
my name is xu,I'm 23 years old,I'm a girl

until循环

until 条件结果为假时继续执行

until expression
do
    command
done

例 :
cat until_test.sh

#!/bin/bash

sum01=0
sum02=0
i=1
until [[ $i -gt 100 ]]
do
    let "sum01+=i"
    let "j=i%2"
    if [[ $j -ne 0 ]]; then
         let "sum02+=i"
    fi
    let "i+=1"
done
echo $sum01
echo $sum02

结果 :

5050
2500

相关文章

网友评论

      本文标题:shell判断/循环语法

      本文链接:https://www.haomeiwen.com/subject/alaxvctx.html