1. if 判断

1.1 基本语法

  1. if [ 条件判断式 ];then
  2. 程序
  3. fi
  4. #或者
  5. if [ 条件判断式 ]
  6. then
  7. 程序
  8. fi

注意事项:
(1) [ 条件判断式 ],中括号和条件判断式之间必须要有空格
(2)if 后面要有空格

1.2 案例实操

输入一个数字,如果是1,则输出number 1,如果是2,则输出number 2,如果是其它,什么也不输出。

  1. [root@localhost ~]# touch if.sh
  2. [root@localhost ~]# vim if.sh
  3. 方式一:
  4. #!/bin/bash
  5. if [ $1 -eq "1" ]
  6. then
  7. echo "number 1"
  8. elif [ $1 -eq "2" ]
  9. then
  10. echo "number 2"
  11. fi
  12. 方式二:
  13. #!/bin/bash
  14. if [ $1 -eq "1" ];then
  15. echo "number1"
  16. elif [ $1 -eq "2" ];then
  17. echo "number2"
  18. fi
  19. [root@localhost ~]# chmod 777 if.sh
  20. [root@localhost ~]# ./if.sh 1
  21. number 1
  22. [root@localhost ~]# ./if.sh 2
  23. number 2
  24. [root@localhost ~]# ./if.sh 3
  25. [root@localhost ~]#

2. case 语句

2.1 基本语法

  1. case $变量名 in
  2. "值1")
  3. 如果变量的值等于值1,则执行程序1
  4. ;;
  5. case $变量名 in
  6. "值2")
  7. 如果变量的值等于值2,则执行程序2
  8. ;;
  9. ...省略其他区分支...
  10. *)
  11. 如果变量的值都不是以上的值,则执行此程序
  12. ;;
  13. esac

注意事项:
1)case 行尾必须为单词“in”,每一个模式匹配必须以右括号“)”结束
2)双分号“;;”表示命令序列结束,相当于java中的break
3)最后的“*)”表示默认模式,相当于java中的default

2.2 案例实操

输入一个数字,如果是1,则输出number 1,如果是2,则输出number 2,如果是其它,输出other。

  1. [root@localhost ~]# touch case.sh
  2. [root@localhost ~]# vim case.sh
  3. #!/bin/bash
  4. case $1 in
  5. "1")
  6. echo "number 1"
  7. ;;
  8. "2")
  9. echo "number 2"
  10. ;;
  11. *)
  12. echo "other"
  13. ;;
  14. esac
  15. [root@localhost ~]# chmod 777 case.sh
  16. [root@localhost ~]# ./case.sh 1
  17. number 1
  18. [root@localhost ~]# ./case.sh 2
  19. number 2
  20. [root@localhost ~]# ./case.sh 3
  21. other
  22. [root@localhost ~]# ./case.sh 4
  23. other
  24. [root@localhost ~]#

3. for 循环

3.1 基本语法

  1. for((初始值;循环控制条件;变量变化))
  2. do
  3. 程序
  4. done

3.2 案例实操

(1)从1加到100

  1. [root@localhost ~]# touch for1.sh
  2. [root@localhost ~]# vim for1.sh
  3. #!/bin/bash
  4. s=0
  5. for((i=0;i<=100;i++))
  6. do
  7. s=$[$s+$i]
  8. done
  9. echo $s
  10. [root@localhost ~]# chmod 777 for1.sh
  11. [root@localhost ~]# ./for1.sh
  12. 5050

(2)输入参数n,计算n的累加值

  1. [root@localhost ~]# touch forn.sh
  2. [root@localhost ~]# vim forn.sh
  3. #!/bin/bash
  4. echo "用于计算n的累加,n的值为:$1"
  5. s=0
  6. for((i=0;i<=$1;i++))
  7. do
  8. s=$[$s+$i]
  9. done
  10. echo "结果为:$s"
  11. [root@localhost ~]# chmod 777 forn.sh
  12. [root@localhost ~]# ./forn.sh 100
  13. 用于计算n的累加,n的值为:100
  14. 结果为:5050

3.3 基本语法2

  1. for 变量 in 1 2 3...
  2. do
  3. 程序
  4. done

3.4 案例实操2

打印所有输入参数

  1. [root@localhost ~]# touch for2.sh
  2. [root@localhost ~]# vim for2.sh
  3. #!/bin/bash
  4. for i in $*
  5. do
  6. echo "$i"
  7. done
  8. [root@localhost ~]# chmod 777 for2.sh
  9. [root@localhost ~]# ./for2.sh abes 123 hello
  10. abes
  11. 123
  12. hello

3.5 变量$*和$@的区别

  1. $*和$@都表示传递给函数或脚本的所有参数,不被双引号“”包含时,都以$1,$2,$3的形式输出所有参数 ```shell [root@localhost ~]# touch for3.sh [root@localhost ~]# vim for3.sh

!/bin/bash

for i in $* do echo “hello $i” done

echo “*

for i in $@ do echo “hello $i” done

[root@localhost ~]# chmod 777 for3.sh [root@localhost ~]# ./for3.sh abes 123 hello abes hello 123


hello abes hello 123

  1. 2. 当它们被双引号“”包含时,“$*”会将所有的参数作为一个整体,以“$1 $2 $n”的形式输出所有参数;“$@”会将各个参数分开,以“$1 $2”…”$n”的形式输出所有参数。
  2. ```shell
  3. [root@localhost ~]# cp for3.sh for4.sh
  4. [root@localhost ~]# vim for4.sh
  5. #!/bin/bash
  6. for i in "$*"
  7. do
  8. echo "hello $i"
  9. done
  10. echo "*****************"
  11. for i in "$@"
  12. do
  13. echo "hello $i"
  14. done
  15. [root@localhost ~]# ./for4.sh abes 123
  16. hello abes 123
  17. *****************
  18. hello abes
  19. hello 123

4. while 循环

4.1 基本语法

  1. while [ 条件判断式 ]
  2. do
  3. 程序
  4. done

4.2 案例实操

(1) 从1加到100

  1. [root@localhost ~]# touch while1.sh
  2. [root@localhost ~]# chmod 777 while1.sh
  3. [root@localhost ~]# vim while1.sh
  4. #!/bin/bash
  5. s=0
  6. i=0
  7. while [ $i -le 100 ]
  8. do
  9. s=$[$s+$i]
  10. i=$[$i+1]
  11. done
  12. echo $s
  13. [root@localhost ~]# ./while1.sh
  14. 5050

(2)计算n的累加

  1. [root@localhost ~]# cp while1.sh while2.sh
  2. [root@localhost ~]# vim while2.sh
  3. #!/bin/bash
  4. s=0
  5. i=0
  6. while [ $i -le $1 ]
  7. do
  8. s=$[$s+$i]
  9. i=$[$i+1]
  10. done
  11. echo $s
  12. [root@localhost ~]# ./while2.sh 100
  13. 5050