1)第一种写法:
if [ 条件判断式 ];then
程序
fi
2)第二种写法:
if [ 条件判断式 ]
then
程序
fi
[root@localhost home]# cd /home/jiaoben/; ls -l
总用量 16
-rw-r--r--. 1 root root 27 6月 27 23:38 hello.sh
-rwxr-xr-x. 1 root root 52 6月 28 22:05 parameter.sh
-rw-r--r--. 1 root root 450 6月 29 00:03 para.sh
-rw-r--r--. 1 root root 323 6月 29 23:58 sum.sh
[root@localhost jiaoben]# num=18
[root@localhost jiaoben]# if [ $num -gt 16 ]; then echo "$a大于16"; fi
25大于16
[root@localhost jiaoben]# num=15
[root@localhost jiaoben]# if [ $num -gt 16 ]; then echo "$a大于16"; fi
[root@localhost jiaoben]#
-a 相当于and
-o 相当于or
[root@localhost jiaoben]# num=25
[root@localhost jiaoben]# if [ $num -gt 20 ] && [ $num -lt 30 ]; then echo ok; fi
ok
[root@localhost jiaoben]# if [ $num -gt 20 -a $num -lt 30 ]; then echo ok; fi
ok
[root@localhost jiaoben]# if [ $num -lt 20 -o $num -gt 30 ]; then echo ok; fi
[root@localhost jiaoben]# num=40
[root@localhost jiaoben]# if [ $num -lt 20 -o $num -gt 30 ]; then echo ok; fi
ok
[root@localhost jiaoben]# num=18
[root@localhost jiaoben]# if [ $num -lt 20 -o $num -gt 30 ]; then echo ok; fi
ok
if_demo1.sh
#!/bin/bash
if [ $1 = kevin ]
then
echo "welcome, kevin"
fi
执行脚本:
[root@localhost jiaoben]# chmod +x if_demo1.sh
[root@localhost jiaoben]# ./if_demo1.sh kevin
welcome, kevin
[root@localhost jiaoben]#
if [ 条件判断式 ]
then
程序
elif [ 条件判断式 ]
then
程序
else
程序
fi
if_demo2.sh
#!/bin/bash
if [ $1 -ge 90 ]
then
echo "成绩优秀"
elif [ $1 -ge 75 -a $1 -lt 90 ]
then
echo "成绩良好"
elif [ $1 -ge 60 -a $1 -lt 75 ]
then
echo "成绩及格"
else
echo "成绩不及格"
fi
执行脚本:
[root@localhost jiaoben]# chmod +x if_demo2.sh
[root@localhost jiaoben]# ./if_demo2.sh 93
成绩优秀
[root@localhost jiaoben]# ./if_demo2.sh 80
成绩良好
[root@localhost jiaoben]# ./if_demo2.sh 62
成绩及格
[root@localhost jiaoben]# ./if_demo2.sh 58
成绩不及格
case语句写法:
case $变量名 in
“值 1”)
如果变量的值等于值1,则执行程序1
;;
“值2”)
如果变量的值等于值2,则执行程序2
;;
…省略其他分支
*)
如果变量的值都不是以上的值,则执行此程序
;;
注意:
1)case行尾必须为单词“in”,没一个模式匹配必须以右括号“)”结束
2)双分号“;;”表示命令序列结束,相当于java中的break
3)最后“*)”表示默认模式,相当于java中的default
case_demo.sh
#!/bin/bash
case $1 in
java)
echo "买了本Java的书籍"
;;
python)
echo "买了本Python的书"
;;
golang)
echo "买了本Golang的书"
;;
*)
echo "================"
;;
esac
执行脚本case_demo.sh
[root@localhost jiaoben]# chmod +x case_demo.sh
[root@localhost jiaoben]# ./case_demo.sh java
买了本Java的书籍
[root@localhost jiaoben]# ./case_demo.sh golang
买了本Golang的书
[root@localhost jiaoben]# ./case_demo.sh php
================
for (( 初始值;循环控制条件;变量变化 ))
do
程序
done
for_demo1.sh
#!/bin/bash
for (( i=1; i<=$1; i++ ))
do
sum=$[ $sum + $i ]
done
echo $sum
执行脚本:
[root@localhost jiaoben]# sh for_demo1.sh 100
5050
for 变量 in 值1 值2 值3…
do
程序
done
[root@localhost jiaoben]# for book in Java Python Golang; do echo $book; done
Java
Python
Golang
[root@localhost jiaoben]# for i in {1..100}; do sum=$[$sum+$i]; done; echo $sum
5050
[root@localhost jiaoben]#
for_demo2.sh
#!/bin/bash
for num in $*
do
echo $num
done
执行脚本:
[root@localhost jiaoben]# sh for_demo2.sh 1 2 3 4 5 6 7 8
1
2
3
4
5
6
7
8
双引号包含时,$*会将所有的参数作为一个整体,以$1 $2 $3 … n 的形式列出所有参数; n的形式列出所有参数; n的形式列出所有参数;@会将各个参数分开
for_demo3.sh
#!/bin/bash
echo '================$*================='
for lanague in $*
do
echo $lanague
done
echo '================$@================='
for lanague in $@
do
echo $lanague
done
执行脚本:
[root@localhost jiaoben]# ll | grep for_demo3.sh
-rw-r--r--. 1 root root 181 7月 1 23:20 for_demo3.sh
[root@localhost jiaoben]# chmod u+x for_demo3.sh
[root@localhost jiaoben]# ll | grep for_demo3.sh
-rwxr--r--. 1 root root 181 7月 1 23:20 for_demo3.sh
[root@localhost jiaoben]# ./for_demo3.sh Java Python Golang Sql
================$*=================
Java
Python
Golang
Sql
================$@=================
Java
Python
Golang
Sql
修改脚本for_demo3.sh
#!/bin/bash
echo '================$*================='
for lanague in "$*"
do
echo $lanague
done
echo '================$@================='
for lanague in "$@"
do
echo $lanague
done
执行脚本:
[root@localhost jiaoben]# ./for_demo3.sh Java Python Golang Sql
================$*=================
Java Python Golang Sql
================$@=================
Java
Python
Golang
Sql
while [ 条件判断式 ]
do
程序
done
while_demo.sh
#!/bin/bash
num=1
while [ $num -le $1 ]
do
sum=$[ $sum + $num]
num=$[$num + 1]
done
echo $sum
执行脚本:
[root@localhost jiaoben]# sh while_demo.sh 100
5050
修改while_demo.sh
#!/bin/bash
num=1
while [ $num -le $1 ]
do
let sum+=num
let num++
done
echo $sum
执行脚本:
[root@localhost jiaoben]# sh while_demo.sh 100
5050