1. if 判断
1.1 基本语法
if [ 条件判断式 ];then程序fi#或者if [ 条件判断式 ]then程序fi
注意事项:
(1) [ 条件判断式 ],中括号和条件判断式之间必须要有空格
(2)if 后面要有空格
1.2 案例实操
输入一个数字,如果是1,则输出number 1,如果是2,则输出number 2,如果是其它,什么也不输出。
[root@localhost ~]# touch if.sh[root@localhost ~]# vim if.sh方式一:#!/bin/bashif [ $1 -eq "1" ]thenecho "number 1"elif [ $1 -eq "2" ]thenecho "number 2"fi方式二:#!/bin/bashif [ $1 -eq "1" ];thenecho "number1"elif [ $1 -eq "2" ];thenecho "number2"fi[root@localhost ~]# chmod 777 if.sh[root@localhost ~]# ./if.sh 1number 1[root@localhost ~]# ./if.sh 2number 2[root@localhost ~]# ./if.sh 3[root@localhost ~]#
2. case 语句
2.1 基本语法
case $变量名 in"值1")如果变量的值等于值1,则执行程序1;;case $变量名 in"值2")如果变量的值等于值2,则执行程序2;;...省略其他区分支...*)如果变量的值都不是以上的值,则执行此程序;;esac
注意事项:
1)case 行尾必须为单词“in”,每一个模式匹配必须以右括号“)”结束
2)双分号“;;”表示命令序列结束,相当于java中的break
3)最后的“*)”表示默认模式,相当于java中的default
2.2 案例实操
输入一个数字,如果是1,则输出number 1,如果是2,则输出number 2,如果是其它,输出other。
[root@localhost ~]# touch case.sh[root@localhost ~]# vim case.sh#!/bin/bashcase $1 in"1")echo "number 1";;"2")echo "number 2";;*)echo "other";;esac[root@localhost ~]# chmod 777 case.sh[root@localhost ~]# ./case.sh 1number 1[root@localhost ~]# ./case.sh 2number 2[root@localhost ~]# ./case.sh 3other[root@localhost ~]# ./case.sh 4other[root@localhost ~]#
3. for 循环
3.1 基本语法
for((初始值;循环控制条件;变量变化))do程序done
3.2 案例实操
(1)从1加到100
[root@localhost ~]# touch for1.sh[root@localhost ~]# vim for1.sh#!/bin/bashs=0for((i=0;i<=100;i++))dos=$[$s+$i]doneecho $s[root@localhost ~]# chmod 777 for1.sh[root@localhost ~]# ./for1.sh5050
(2)输入参数n,计算n的累加值
[root@localhost ~]# touch forn.sh[root@localhost ~]# vim forn.sh#!/bin/bashecho "用于计算n的累加,n的值为:$1"s=0for((i=0;i<=$1;i++))dos=$[$s+$i]doneecho "结果为:$s"[root@localhost ~]# chmod 777 forn.sh[root@localhost ~]# ./forn.sh 100用于计算n的累加,n的值为:100结果为:5050
3.3 基本语法2
for 变量 in 值1 值2 值3...do程序done
3.4 案例实操2
打印所有输入参数
[root@localhost ~]# touch for2.sh[root@localhost ~]# vim for2.sh#!/bin/bashfor i in $*doecho "$i"done[root@localhost ~]# chmod 777 for2.sh[root@localhost ~]# ./for2.sh abes 123 helloabes123hello
3.5 变量$*和$@的区别
- $*和$@都表示传递给函数或脚本的所有参数,不被双引号“”包含时,都以$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
2. 当它们被双引号“”包含时,“$*”会将所有的参数作为一个整体,以“$1 $2 …$n”的形式输出所有参数;“$@”会将各个参数分开,以“$1” “$2”…”$n”的形式输出所有参数。```shell[root@localhost ~]# cp for3.sh for4.sh[root@localhost ~]# vim for4.sh#!/bin/bashfor i in "$*"doecho "hello $i"doneecho "*****************"for i in "$@"doecho "hello $i"done[root@localhost ~]# ./for4.sh abes 123hello abes 123*****************hello abeshello 123
4. while 循环
4.1 基本语法
while [ 条件判断式 ]do程序done
4.2 案例实操
(1) 从1加到100
[root@localhost ~]# touch while1.sh[root@localhost ~]# chmod 777 while1.sh[root@localhost ~]# vim while1.sh#!/bin/bashs=0i=0while [ $i -le 100 ]dos=$[$s+$i]i=$[$i+1]doneecho $s[root@localhost ~]# ./while1.sh5050
(2)计算n的累加
[root@localhost ~]# cp while1.sh while2.sh[root@localhost ~]# vim while2.sh#!/bin/bashs=0i=0while [ $i -le $1 ]dos=$[$s+$i]i=$[$i+1]doneecho $s[root@localhost ~]# ./while2.sh 1005050
