SHELL学习——判断
2012-12-18 22:55
141 查看
简单的if结构
if expression
then
command
command
……
fi
测试条件后没有“;”,则then语句要换行。if 和then处于同一行,必须用“;”来终止if语句
if expression ; then
command
……
fi
举例说明:
#!/bin/bash
echo "Please input a integer: "
read integer1
if [ "$integer1" -lt 15 ] ; then
echo "The integer which you input is lower
than 15."
fi
exit命令
exit status
# status用0~255之间的数字表示,exit不能在终端运行。
举例说明:
[root@localhost tmp]# cat 2.sh
#!/bin/bash
echo "Please input a string: "
read str1
if [ -z "$str1" ]
then
echo
"What you input is null!"
exit 1
fi
[root@localhost tmp]# ./2.sh
Please input a string:
hello
[root@localhost tmp]# echo $?
0
[root@localhost tmp]# ./2.sh
Please input a string:
What you input is null!
[root@localhost tmp]# echo $?
1
[root@localhost tmp]#
if/else结构
if expression
then
command
……
else
command
……
fi
举例说明:(例一)
[root@localhost tmp]# cat 3.sh
#!/bin/bash
file=/tmp/3.sh
if [ ! -e "$file" ]
then
echo
"file $file do not exist ."
exit
else
echo
"file $file exist ."
fi
[root@localhost tmp]# ./3.sh
file /tmp/3.sh exist .
[root@localhost tmp]#
举例说明:(例二)
#!/bin/bash
echo "Please input the file which you want to
delete:"
read file
if rm -f "$file"
then
echo
"Delete the file $file sucessfully!"
else
echo
"Delete the file $file failed!"
fi
if/else语句嵌套
if expression1
then
if
expression2
then
command
else
command
fi
else
if
expression3
then
command
else
command
fi
fi
举例说明:
#!/bin/bash
echo "Please input a file_name:"
read word
if [ "$word" ]
then
echo
"What you input is not null!"
if [ -e
"$word" ]
then
echo "The file $word is existence !"
else
echo "The file $word is not existence !"
fi
else
echo
"what you input is null!"
fi
if/elif/else结构
if expression1
then
command
elif expression2
then
command
elif expressionN
command
else
command
fi
举例说明
#!/bin/bash
echo "Please Input a integer(0-100): "
read score
if [ "$score" -lt 0 -o "$score"
-gt 100 ]
then
echo
"The score what you input is not integer or the score is not in
(0-100)."
elif [ "$score" -ge 90 ]
then
echo
"The grade is A!"
elif [ "$score" -ge 80 ]
then
echo
"The grade is B!"
elif [ "$score" -ge 70 ]
then
echo
"The grade is C!"
elif [ "$score" -ge 60 ]
then
echo
"The grade is D!"
else
echo
"The grade is E!"
fi
case结构
case variable in
value1)
command
……
command;;
Value2)
command
command;;
……
valueN)
command
……
command;;
*)
command
command;;
esac
举例说明:
#!/bin/bash
echo "Please Input a month(0-12): "
read month
case "$month" in
1)
echo
"The month is January!";;
2)
echo
"The month is February!";;
3)
echo
"The month is March!";;
4)
echo
"The month is April!";;
5)
echo
"The month is May!";;
6)
echo
"The month is June!";;
7)
echo
"The month is July!";;
8)
echo
"The month is August!";;
9)
echo
"the month is September!";;
10)
echo
"The month is October!";;
11)
echo
"The month is November!";;
12)
echo
"The month is December!";;
*)
echo
"The month is not in (0-12)";;
esac
echo "The 'case' command ends!"
if expression
then
command
command
……
fi
测试条件后没有“;”,则then语句要换行。if 和then处于同一行,必须用“;”来终止if语句
if expression ; then
command
……
fi
举例说明:
#!/bin/bash
echo "Please input a integer: "
read integer1
if [ "$integer1" -lt 15 ] ; then
echo "The integer which you input is lower
than 15."
fi
exit命令
exit status
# status用0~255之间的数字表示,exit不能在终端运行。
举例说明:
[root@localhost tmp]# cat 2.sh
#!/bin/bash
echo "Please input a string: "
read str1
if [ -z "$str1" ]
then
echo
"What you input is null!"
exit 1
fi
[root@localhost tmp]# ./2.sh
Please input a string:
hello
[root@localhost tmp]# echo $?
0
[root@localhost tmp]# ./2.sh
Please input a string:
What you input is null!
[root@localhost tmp]# echo $?
1
[root@localhost tmp]#
if/else结构
if expression
then
command
……
else
command
……
fi
举例说明:(例一)
[root@localhost tmp]# cat 3.sh
#!/bin/bash
file=/tmp/3.sh
if [ ! -e "$file" ]
then
echo
"file $file do not exist ."
exit
else
echo
"file $file exist ."
fi
[root@localhost tmp]# ./3.sh
file /tmp/3.sh exist .
[root@localhost tmp]#
举例说明:(例二)
#!/bin/bash
echo "Please input the file which you want to
delete:"
read file
if rm -f "$file"
then
echo
"Delete the file $file sucessfully!"
else
echo
"Delete the file $file failed!"
fi
if/else语句嵌套
if expression1
then
if
expression2
then
command
else
command
fi
else
if
expression3
then
command
else
command
fi
fi
举例说明:
#!/bin/bash
echo "Please input a file_name:"
read word
if [ "$word" ]
then
echo
"What you input is not null!"
if [ -e
"$word" ]
then
echo "The file $word is existence !"
else
echo "The file $word is not existence !"
fi
else
echo
"what you input is null!"
fi
if/elif/else结构
if expression1
then
command
elif expression2
then
command
elif expressionN
command
else
command
fi
举例说明
#!/bin/bash
echo "Please Input a integer(0-100): "
read score
if [ "$score" -lt 0 -o "$score"
-gt 100 ]
then
echo
"The score what you input is not integer or the score is not in
(0-100)."
elif [ "$score" -ge 90 ]
then
echo
"The grade is A!"
elif [ "$score" -ge 80 ]
then
echo
"The grade is B!"
elif [ "$score" -ge 70 ]
then
echo
"The grade is C!"
elif [ "$score" -ge 60 ]
then
echo
"The grade is D!"
else
echo
"The grade is E!"
fi
case结构
case variable in
value1)
command
……
command;;
Value2)
command
command;;
……
valueN)
command
……
command;;
*)
command
command;;
esac
举例说明:
#!/bin/bash
echo "Please Input a month(0-12): "
read month
case "$month" in
1)
echo
"The month is January!";;
2)
echo
"The month is February!";;
3)
echo
"The month is March!";;
4)
echo
"The month is April!";;
5)
echo
"The month is May!";;
6)
echo
"The month is June!";;
7)
echo
"The month is July!";;
8)
echo
"The month is August!";;
9)
echo
"the month is September!";;
10)
echo
"The month is October!";;
11)
echo
"The month is November!";;
12)
echo
"The month is December!";;
*)
echo
"The month is not in (0-12)";;
esac
echo "The 'case' command ends!"
相关文章推荐
- shell 脚本学习之判断条件
- shell学习之条件判断test
- shell学习-if判断
- Shell学习if判断字符串
- shell判断当前输入的名称是否为目录(shell学习笔记三)
- Shell 学习(四、条件测试(test,[]) 和 条件判断(if..then..fi))
- shell学习笔记之六(测试和判断)
- Linux学习(九)-----shell脚本学习 条件判断和循环
- SHELL学习笔记----IF条件判断,判断条件
- SHELL学习笔记----IF条件判断,判断条件
- 第七章 shell学习之退出、测试、判断及操作
- SHELL学习笔记----IF条件判断,判断条件
- 【shell学习】常用条件判断-字符,数字,文件
- SHELL学习笔记----IF条件判断,判断条件
- SHELL学习笔记----IF条件判断,判断条件
- shell 脚本学习之判断条件
- SHELL学习笔记----IF条件判断,判断条件
- SHELL学习笔记----IF条件判断,判断条件
- shell判断操作符-------shell学习(11)
- SHELL学习笔记----IF条件判断,判断条件