case 语句
语句格式
case "$variable" in
模式1)
命令1
...
;;
模式2)
命令2
...
;;
esac
模式部分可使用通配符
* 任意字符
? 任意单个字符
[..] 类或范围中任意字符
简单的case语句
示例代码如下:
#!/bin/sh
#test12
#简单case语句
echo -n "enter a number from 1 to 5 :"
read ANS
case $ANS in
1) echo "you select 1"
;;
2) echo "you select 2"
;;
3) echo "you select 3"
;;
4) echo "you select 4"
;;
5) echo "you select 5"
;;
*) echo "`basename $0`: this is not between 1 and 5" >&2
exit 1
;;
esac
执行结果:
[retacn@localhost tmp]$ ./test12
enter a number from 1 to 5 :0
test12: this is not between 1 and 5
[retacn@localhost tmp]$ ./test12
enter a number from 1 to 5 :1
you select 1
对匹配模式使用(|)或运算符
==================================================================
循环
for
语法格式为:
for 变量名 in 列表
do
命令1
命令2
done
简单的for 循环
示例代码如下:
#!/bin/sh
#test13
#简单for循环
for loop in 1 2 3 4 5
do
echo $loop
done
执行结果:
[retacn@localhost tmp]$ ./test13
1
2
3
4
5
打印当前目录下所有文件
示例代码如下:
#!/bin/sh
#test14
#打印当前目录下所有文件
for loop in `ls`
do
echo $loop
done
执行结果如下:
[retacn@localhost tmp]$ ./test14
helloworld
helloworld.c
myfile1
temp
test
test10
test12
test13
test14
test2
test3
test4
test5
test6
test7
test8
test9
test.txt
在for循环中使用参数
不使用列表选项查看特定参数$@或$*,从命令行中读取参数
示例代码如下:
#!/bin/sh
#test15
#从命令行读取参数
for params
do
echo "you supplied $params as a command line option"
done
echo $params
done
执行结果:
[retacn@localhost tmp]$ ./test15 myfile1 myfile2
you supplied myfile1 as a command line option
you supplied myfile2 as a command line option
until循环
循环执行命令直到条件为真时结束
语法格式如下:
util 条件
命令
done
简单的until循环
监视磁盘空间
示例代码如下:
#!/bin/sh
#test16
#监视磁盘空间
LOOK_OUT=`df | grep /logs | awk '{print $5}' | sed 's/%//g'`
echo $LOOK_OUT
until [ "$LOOK_OUT" -gt "90" ]
do
echo "filessystem ..log is nearly full"
done
while循环
语法格式如下
while 命令
do
命令1
done
或
while [condition] ; do
简单的while循环
示例代码如下
#!/bin/sh
#test17
#简单的while循环
COUNT=0
while [ $COUNT -lt 5 ]
do
COUNT=`expr $COUNT + 1`
echo $COUNT
done
执行结果
[retacn@localhost tmp]$ ./test17
1
2
3
4
5
循环从文件中读取数据
示例代码如下:
#!/bin/sh
#test18
#从文件中读取数据
while read LINE
do
echo $LINE
dUNT < name.txt
执行结果:
[retacn@localhost tmp]$ ./test18
yue
zhen
hua
break和continue
break跳出循环
continue跳出当前循环
菜单
示例代码如下:
#!/bin/sh
#test19
#菜单
MYDDATE=`date +%d/%m/%Y`
THIS_HOST=`hostname -s`
USER=`whoami`
#无限循环
while :
do
#清屏
tput clear
cat <<MAYDAY
----------------------------------------------------------------
User:$USER Host:$THIS_HOST Date:$MYDATE
----------------------------------------------------------------
1:List files in current directory
2:Use the vi editor
3:See who is on the system
H:Help screen
Q:Exit Menu
-----------------------------------------------------------------
MAYDAY
echo -e -n "\t Your Choice [1,2,3,h,q] >"
read CHOICE
case $CHOICE in
1) ls
;;
2) vi
;;
3) who
;;
Q|q) exit 0
;;
H|h)
cat <<MAYDAY
this is the help screen!
MAYDAY
;;
*) echo -e "\t\007unknown user response"
esac
echo -e -n "\tHit the return key to continue"
read DUMMY
done
执行结果:
----------------------------------------------------------------
User:root Host:localhost Date:
----------------------------------------------------------------
1:List files in current directory
2:Use the vi editor
3:See who is on the system
H:Help screen
Q:Exit Menu
-----------------------------------------------------------------
Your Choice [1,2,3,H,Q] >q
语句格式
case "$variable" in
模式1)
命令1
...
;;
模式2)
命令2
...
;;
esac
模式部分可使用通配符
* 任意字符
? 任意单个字符
[..] 类或范围中任意字符
简单的case语句
示例代码如下:
#!/bin/sh
#test12
#简单case语句
echo -n "enter a number from 1 to 5 :"
read ANS
case $ANS in
1) echo "you select 1"
;;
2) echo "you select 2"
;;
3) echo "you select 3"
;;
4) echo "you select 4"
;;
5) echo "you select 5"
;;
*) echo "`basename $0`: this is not between 1 and 5" >&2
exit 1
;;
esac
执行结果:
[retacn@localhost tmp]$ ./test12
enter a number from 1 to 5 :0
test12: this is not between 1 and 5
[retacn@localhost tmp]$ ./test12
enter a number from 1 to 5 :1
you select 1
对匹配模式使用(|)或运算符
==================================================================
循环
for
语法格式为:
for 变量名 in 列表
do
命令1
命令2
done
简单的for 循环
示例代码如下:
#!/bin/sh
#test13
#简单for循环
for loop in 1 2 3 4 5
do
echo $loop
done
执行结果:
[retacn@localhost tmp]$ ./test13
1
2
3
4
5
打印当前目录下所有文件
示例代码如下:
#!/bin/sh
#test14
#打印当前目录下所有文件
for loop in `ls`
do
echo $loop
done
执行结果如下:
[retacn@localhost tmp]$ ./test14
helloworld
helloworld.c
myfile1
temp
test
test10
test12
test13
test14
test2
test3
test4
test5
test6
test7
test8
test9
test.txt
在for循环中使用参数
不使用列表选项查看特定参数$@或$*,从命令行中读取参数
示例代码如下:
#!/bin/sh
#test15
#从命令行读取参数
for params
do
echo "you supplied $params as a command line option"
done
echo $params
done
执行结果:
[retacn@localhost tmp]$ ./test15 myfile1 myfile2
you supplied myfile1 as a command line option
you supplied myfile2 as a command line option
until循环
循环执行命令直到条件为真时结束
语法格式如下:
util 条件
命令
done
简单的until循环
监视磁盘空间
示例代码如下:
#!/bin/sh
#test16
#监视磁盘空间
LOOK_OUT=`df | grep /logs | awk '{print $5}' | sed 's/%//g'`
echo $LOOK_OUT
until [ "$LOOK_OUT" -gt "90" ]
do
echo "filessystem ..log is nearly full"
done
while循环
语法格式如下
while 命令
do
命令1
done
或
while [condition] ; do
简单的while循环
示例代码如下
#!/bin/sh
#test17
#简单的while循环
COUNT=0
while [ $COUNT -lt 5 ]
do
COUNT=`expr $COUNT + 1`
echo $COUNT
done
执行结果
[retacn@localhost tmp]$ ./test17
1
2
3
4
5
循环从文件中读取数据
示例代码如下:
#!/bin/sh
#test18
#从文件中读取数据
while read LINE
do
echo $LINE
dUNT < name.txt
执行结果:
[retacn@localhost tmp]$ ./test18
yue
zhen
hua
break和continue
break跳出循环
continue跳出当前循环
菜单
示例代码如下:
#!/bin/sh
#test19
#菜单
MYDDATE=`date +%d/%m/%Y`
THIS_HOST=`hostname -s`
USER=`whoami`
#无限循环
while :
do
#清屏
tput clear
cat <<MAYDAY
----------------------------------------------------------------
User:$USER Host:$THIS_HOST Date:$MYDATE
----------------------------------------------------------------
1:List files in current directory
2:Use the vi editor
3:See who is on the system
H:Help screen
Q:Exit Menu
-----------------------------------------------------------------
MAYDAY
echo -e -n "\t Your Choice [1,2,3,h,q] >"
read CHOICE
case $CHOICE in
1) ls
;;
2) vi
;;
3) who
;;
Q|q) exit 0
;;
H|h)
cat <<MAYDAY
this is the help screen!
MAYDAY
;;
*) echo -e "\t\007unknown user response"
esac
echo -e -n "\tHit the return key to continue"
read DUMMY
done
执行结果:
----------------------------------------------------------------
User:root Host:localhost Date:
----------------------------------------------------------------
1:List files in current directory
2:Use the vi editor
3:See who is on the system
H:Help screen
Q:Exit Menu
-----------------------------------------------------------------
Your Choice [1,2,3,H,Q] >q