赞
踩
目录
Shell流程控制包括:if-else语句、for循环、while语句、无限循环、until循环、case ... esac和跳出循环。
- # if 控制语句
-
- # if判断
- # if command;
- # then
- # code
- # fi
-
-
- # if else 语句
- # if command;
- # then
- # code
- # else
- # code
- # fi
-
-
- # if elif else 语句
- # if command;
- # then
- # code
- # elif command2;
- # then
- # code
- # *n
- # else
- # code
- # fi
- #!/bin/sh
-
- if [ `ps -ef | grep -c "ssh"` -gt 1 ]; # ps -ef 列出当前系统中所有的进程信息。
- # grep -c "ssh" 统计这些进程信息中包含字符串 "ssh" 的行数。
- then
- echo "True"
- fi
-
- echo "-----------------"
- if ((10>29));
- then
- echo "10 bigger then 20"
- else
- echo "10 less then 20"
- fi
-
- echo "------------------"
- # elif
- a=10
- b=20
- if [ $a -eq $b ];
- then
- echo "a=b"
- elif [ $a -gt $b ];
- then
- echo "a>b"
- else [ $a -lt $b ];
- echo "a<b"
- fi
- True
- -----------------
- 10 less then 20
- ------------------
- a<b
- # for循环
- # for i in xxx;
- # do
- # code
- # done
- #!/bin/sh
-
- for i in "I\'m good at coding";
- do
- echo $i
- done
-
- for i in 1 2 3 4 5 ;
- do
- echo the value is $i
- done
-
- for i in "$@"; # 文件传参 $@ 循环获取传入的参数
- do
- echo $i
- done
- [root@tokyo001 shell_code]# sh demo10.sh 2 3 4 5 5 # 文件传参写法
- I\'m good at coding
- the value is 1
- the value is 2
- the value is 3
- the value is 4
- the value is 5
- 2
- 3
- 4
- 5
- 5
- # while循环
- # while codition
- # do
- # code
- # done
- #!/bin/sh
-
- a=1
- while [ $a -le 5 ]
- do
- echo $a
- let "a++" # 等价于 a=`expr $a + 1 `
- done
- 1
- 2
- 3
- 4
- 5
- #无限循环语法格式
- # while : # 或者 while True
- # do
- # echo "im handsome"
- # done
-
- # for ((;;))
- #!/bin/sh
-
- # 死循环
- echo "请按ctrl+c结束"
- echo -n "请输入你喜欢的电影:" # -n 不要在输出末尾添加换行符
- while read film
- do
- echo "${film}是一部好电影"
- done
- 请按ctrl+c结束
- 请输入你喜欢的电影:hhh
- hhh是一部好电影
- xxxx
- xxxx是一部好电影
- gggg
- gggg是一部好电影
- #!/bin/sh
-
- # until循环 直到条件为真为止
- a=0
- until [ ! $a -lt 10 ]
- do
- echo $a
- let "a++"
- done
- 0
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- #!/bin/sh
- # case esac 模式匹配
-
-
- echo "请输入1-4之间的数字"
- read -p "请输入数字:" num
- case $num in
- 1)
- echo "you choose 1"
- ;; # 表示匹配结束
- 2)
- echo "you choose 2"
- ;;
- 3)
- echo "you choose 3"
- ;;
- 4)
- echo "you choose 4"
- ;;
- *) # 8 通配其他结果
- echo "you choose others"
- ;;
- esac
-
- echo "----------------"
- a="hhh"
- case $a in
- "hhh")
- echo "you are hhhhhhhhh"
- ;;
- "xxx")
- echo "you are xxxxxxxxx"
- ;;
- "ggg")
- echo "you are gggggggggg"
- ;;
- esac
- 请输入1-4之间的数字
- 请输入数字:2
- you choose 2
- ----------------
- you are hhhhhhhhh
- #!/bin/sh
-
- # break 跳出循环
- echo "welcome to paris"
- echo "plese tell me yuor age"
- while :
- do
- read -p "plese tell me yuor age of ten " age
- case $age in
- 1|2|3|4)
- echo "your are so yong"
- ;;
- *)
- echo "old man"
- break # 跳出整个循环
- ;;
- esac
- done
- welcome to paris
- plese tell me yuor age
- plese tell me yuor age of ten 1
- your are so yong
- plese tell me yuor age of ten 1
- your are so yong
- plese tell me yuor age of ten 1
- your are so yong
- plese tell me yuor age of ten 5
- old man
- #!/bin/sh
-
- # continue 跳出循环
- echo "welcome to paris"
- echo "plese tell me yuor age"
- while :
- do
- read -p "plese tell me yuor age of ten " age
- case $age in
- 1|2|3|4)
- echo "your are so yong"
- ;;
- *)
- echo "old man"
- continue # 跳出当前循环
- ;;
- esac
- done
- welcome to paris
- plese tell me yuor age
- plese tell me yuor age of ten 1
- your are so yong
- plese tell me yuor age of ten 2
- your are so yong
- plese tell me yuor age of ten 3
- your are so yong
- plese tell me yuor age of ten 1
- your are so yong
- plese tell me yuor age of ten 6
- old man
- plese tell me yuor age of ten 6
- old man
- plese tell me yuor age of ten
Shell流程控制包括:if-else语句、for循环、while语句、无限循环、until循环、case ... esac和跳出循环。
Shell流程控制包括:if-else语句、for循环、while语句、无限循环、until循环、case ... esac和跳出循环。
重要的事情说三遍哈哈哈哈。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。