shell流控制与循环:
1. if-else流控制:
if list1; then
list2
elif list3; then
list4
elif list5; then
list6
else
list7
fi
shell中返回0表示为真,非0为假
例子:
#!/bin/bash
read -p "请输入一个整数A: " A
if test ${A} -lt 1 ; then
echo "A<1"
elif test ${A} -lt 10 ; then
echo "A<10"
elif test ${A} -lt 100 ; then
echo "A<100"
else
echo "A>=100"
fi
等价于:
#!/bin/bash
read -p "请输入一个整数A: " A
if [ ${A} -lt 1 ] ; then
echo "A<1"
elif [ ${A} -lt 10 ]; then
echo "A<10"
elif [ ${A} -lt 100 ]; then
echo "A<100"
else
echo "A>=100"
fi
test expr 等价于 [ expr ]
注意:在[之后和]之前都要有空格,通常[]作为test的缩写
test能够理解三种主要类型的表达式:
文件测试,字符串比较,数字比较
复合表达式:
test expr1 op1 expr2 op1为-a或-o
等价于[ expr1 ] op2 [ expr2 ] op2为&&,||
具体ref:私房菜P380
注意:test测试时的变量,最好用双引号括起来
case语句:
case word in
pattern1)
list1;;
pattern2)
list2;;
...
*)
listn;;
esac
while循环:
while cmd
do
list
done
如:
#!/bin/bash
x=1
while [ ${x} -lt 10 ]
do
echo ${x}
x=`expr $x + 1`
done
输入重定向和while
#!/bin/bash
while read line
do
case ${line} in
*root*) echo ${line};;
esac
done < /etc/passwd
输出:root:x:0:0:root:/root:/bin/bash
---------------------------------------------------------------
#!/bin/bash
#计算文件行数
if [ -f "${1}" ]; then
i=0
while read line
do
i=`expr ${i} + 1`
done < "${1}"
echo $i
fi
-----------------------------------------------------------
#!/bin/bash
if [ $# -ge 1 ]; then
for FILE in ${@}
do
exec 4<&0 0<"${FILE}" #重定向STDIN指针
while read line
do
echo ${line}
done
exec 0<&4 4<&- #恢复STDIN,并关闭描述符4
done
fi
--------------------------------------------------------------
until循环:
#!/bin/bash
x=1
until [ ${x} -ge 10 ]
do
echo ${x}
x=$(($x+1))
done
for循环:
#!/bin/bash
for i in 0 1 2 3 4 5
do
echo ${i}
done
------------------------------------
#!/bin/bash
for FILE in $HOME/.bash*
do
echo ${FILE}
done
--------------------------------------
select循环:
#test.sh:
#!/bin/bash
select DRINK in tea cofee water juice appe all none
do
case $DRINK in
tea|cofee|water|all)
echo "Go to canteen"
;;
juice|appe)
echo "Available at home"
;;
none)
break
;;
*) echo "ERROR: Invalid selection"
;;
esac
done
执行结果:
#bash test.sh
1) tea
2) cofee
3) water
4) juice
5) appe
6) all
7) none
#? 1
Go to canteen
#? 2
Go to canteen
#? 3
Go to canteen
#? 4
Available at home
#? 5
Available at home
#? 6
Go to canteen
#? 7
您可以更改显示的提示选择循环通过改变变量PS3如下:
$PS3="Please make a selection => " ; export PS3
$./test.sh
1) tea
2) cofee
3) water
4) juice
5) appe
6) all
7) none
Please make a selection => juice
Available at home
Please make a selection => none
$
----------------------------------------------
无限循环:
#!/bin/bash
x=0
while :
do
echo ${x}
x=`expr ${x} + 1`
done
1. if-else流控制:
if list1; then
list2
elif list3; then
list4
elif list5; then
list6
else
list7
fi
shell中返回0表示为真,非0为假
例子:
#!/bin/bash
read -p "请输入一个整数A: " A
if test ${A} -lt 1 ; then
echo "A<1"
elif test ${A} -lt 10 ; then
echo "A<10"
elif test ${A} -lt 100 ; then
echo "A<100"
else
echo "A>=100"
fi
等价于:
#!/bin/bash
read -p "请输入一个整数A: " A
if [ ${A} -lt 1 ] ; then
echo "A<1"
elif [ ${A} -lt 10 ]; then
echo "A<10"
elif [ ${A} -lt 100 ]; then
echo "A<100"
else
echo "A>=100"
fi
test expr 等价于 [ expr ]
注意:在[之后和]之前都要有空格,通常[]作为test的缩写
test能够理解三种主要类型的表达式:
文件测试,字符串比较,数字比较
复合表达式:
test expr1 op1 expr2 op1为-a或-o
等价于[ expr1 ] op2 [ expr2 ] op2为&&,||
具体ref:私房菜P380
注意:test测试时的变量,最好用双引号括起来
case语句:
case word in
pattern1)
list1;;
pattern2)
list2;;
...
*)
listn;;
esac
while循环:
while cmd
do
list
done
如:
#!/bin/bash
x=1
while [ ${x} -lt 10 ]
do
echo ${x}
x=`expr $x + 1`
done
输入重定向和while
#!/bin/bash
while read line
do
case ${line} in
*root*) echo ${line};;
esac
done < /etc/passwd
输出:root:x:0:0:root:/root:/bin/bash
---------------------------------------------------------------
#!/bin/bash
#计算文件行数
if [ -f "${1}" ]; then
i=0
while read line
do
i=`expr ${i} + 1`
done < "${1}"
echo $i
fi
-----------------------------------------------------------
#!/bin/bash
if [ $# -ge 1 ]; then
for FILE in ${@}
do
exec 4<&0 0<"${FILE}" #重定向STDIN指针
while read line
do
echo ${line}
done
exec 0<&4 4<&- #恢复STDIN,并关闭描述符4
done
fi
--------------------------------------------------------------
until循环:
#!/bin/bash
x=1
until [ ${x} -ge 10 ]
do
echo ${x}
x=$(($x+1))
done
for循环:
#!/bin/bash
for i in 0 1 2 3 4 5
do
echo ${i}
done
------------------------------------
#!/bin/bash
for FILE in $HOME/.bash*
do
echo ${FILE}
done
--------------------------------------
select循环:
#test.sh:
#!/bin/bash
select DRINK in tea cofee water juice appe all none
do
case $DRINK in
tea|cofee|water|all)
echo "Go to canteen"
;;
juice|appe)
echo "Available at home"
;;
none)
break
;;
*) echo "ERROR: Invalid selection"
;;
esac
done
执行结果:
#bash test.sh
1) tea
2) cofee
3) water
4) juice
5) appe
6) all
7) none
#? 1
Go to canteen
#? 2
Go to canteen
#? 3
Go to canteen
#? 4
Available at home
#? 5
Available at home
#? 6
Go to canteen
#? 7
您可以更改显示的提示选择循环通过改变变量PS3如下:
$PS3="Please make a selection => " ; export PS3
$./test.sh
1) tea
2) cofee
3) water
4) juice
5) appe
6) all
7) none
Please make a selection => juice
Available at home
Please make a selection => none
$
----------------------------------------------
无限循环:
#!/bin/bash
x=0
while :
do
echo ${x}
x=`expr ${x} + 1`
done