码迷,mamicode.com
首页 > 系统相关 > 详细

2018-4-18 17周1次课 shell逻辑判断、文件目录属性判断、if、case

时间:2018-04-19 00:32:50      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:shell

20.5 shell脚本中的逻辑判断



·格式1:if 条件; then 语句; fi

例:a=5

if [ $a -gt 3 ]; then echo ok; fi

技术分享图片

[root@localhost shell]# sh if1.sh
ok


·格式2:if 条件; then 语句; else 语句; fi

例:a=5

if [ $a -gt 3 ]; then echo ok; else echo nook; fi

技术分享图片

[root@localhost shell]# sh if1.sh
ok

技术分享图片

[root@localhost shell]# sh if1.sh
not ok
[root@localhost shell]# sh -x if1.sh
+ a=2
+ '[' 2 -gt 3 ']'
+ echo not ok
not ok


·格式3:if …; then … ;elif …; then …; else …; fi

第一个条件,怎样;第二个条件,怎样;。。。否则,其他条件

技术分享图片

[root@localhost shell]# sh -x if1.sh
+ a=3
+ '[' 3 -gt 4 ']'
+ '[' 3 -lt 6 ']'
+ echo '<6 && >1'
<6 && >1


技术分享图片

[root@localhost shell]# sh -x if1.sh
+ a=5
+ '[' 5 -lt 4 ']'
+ '[' 5 -gt 6 ']'
+ echo not ok
not ok


·逻辑判断表达式:

if [ $a -gt $b ]; -gt (>); 大于

if [ $a -lt 5 ]; -lt(<); 小于

if [ $b -eq 10 ]等   -eq(==); 等于

-le(<=);-ge(>=); -ne(!=) 注意到处都是空格

·如果非要用 > < ,可以用(( ))

[root@localhost shell]# if (($a>1));then echo ok;fi

ok

·可以使用 && || 结合多个条件

·if [ $a -gt 5 ] && [ $a -lt 10 ]; then 并且

·if [ $b -gt 5 ] || [ $b -lt 3 ]; then 或者





20.6 文件目录属性判断


·[ -f file ] 判断是否是普通文件,且存在

·[ -d file ] 判断是否是目录,且存在

·[ -e file ] 判断文件或目录是否存在

·[ -r file ] 判断文件是否可读

·[ -w file ] 判断文件是否可写

·[ -x file ] 判断文件是否可执行

·[ -f file ] 判断是否是普通文件,且存在


技术分享图片

[root@localhost ~]# sh -x file1.sh
+ f=/tmp/alex
+ '[' -f /tmp/alex ']'                ##是否存在且是文件
+ touch /tmp/alex##不存在则创建
[root@localhost ~]# sh -x file1.sh
+ f=/tmp/alex
+ '[’ -f /tmp/alex ']'                ##是否存在且是文件
+ echo /tmp/alex exist                ##显示存在
/tmp/alex exist


·[ -d file ]判断是否是目录,且存在

技术分享图片

[root@localhost ~]# sh -x file2.sh
+ f=/tmp/alex
+ '[' -d /tmp/alex ']'
+ touch /tmp/alex
[root@localhost ~]# sh -x file2.sh
+ f=/tmp/alex
+ '[' -d /tmp/alex ']'
+ touch /tmp/alex


·[ -e file ]判断文件或目录是否存在

技术分享图片

[root@localhost ~]# sh -x file2.sh
+ f=/tmp/alex
+ '[' -e /tmp/alex ']'
+ echo /tmp/alex exist
/tmp/alex exist


·[ -r file ]判断文件是否可读

技术分享图片

[root@localhost shell]# sh file2.sh
/tmp/alex readable


·[ -w file ]判断文件是否可写

技术分享图片

[root@localhost shell]# sh file2.sh
/tmp/alex writeable


·[ -x file ]判断文件是否可执行

技术分享图片

[root@localhost ~]# sh file2.sh
[root@localhost ~]# ll /tmp/alex
-rw-r--r-- 1 root root 0 4月  18 21:21 /tmp/alex
#!/bin/bash
f="/tmp/alex"
if [ -f $f ]
then
rm -f $f
fi

可以不用if判断,写成

#!/bin/bash
f="/tmp/alex"
[ -f $f ] && rm -f $f


#!/bin/bash
f="/tmp/alex"
if [ -f $f ]
then
rm -f $f
else
touch $f
fi

原脚本可取反写为

#!/bin/bash
f="/tmp/alex"
if [ ! -f $f ]
then
touch $f
fi

可以不用if判断,写成

#!/bin/bash
f="/tmp/alex"
[ -f $f ] || touch $f




20.7 if特殊用法


·if [ -z "$a" ]  这个表示当变量a的值为空时会怎么样(变量要加引号,文件不用引号)

技术分享图片

可优化为

技术分享图片

[root@localhost shell]# sh -x file3.sh
++ wc -l /tmp/jojo
wc: /tmp/jojo: 没有那个文件或目录
+ n=
+ '[' -z '' ']'
+ echo not exist
not exist

可以再优化为

技术分享图片

[root@localhost shell]# sh -x file3.sh
++ wc -l /tmp/jojo
wc: /tmp/jojo: 没有那个文件或目录
+ n=
+ '[' -f ']'
+ echo ' not exist.'
not exist.
+ exit


·if [ -n "$a" ] 表示当变量a的值不为空

[root@localhost shell]# echo $b
[root@localhost shell]# if [  -n 01.sh ];then echo ok;fi
ok
[root@localhost shell]# if [  -n "$b" ];then echo "$b";else echo "b is null";fi
b is null


·if grep -q '123' 1.txt; then  表示如果1.txt中含有'123'的行时会怎么样

[root@localhost shell]# if grep -w "user1" /etc/passwd;then echo "user1 exist";fi
user1:x:1011:1011::/home/user1:/bin/bash
user1 exist
[root@localhost shell]# if grep -wq "user1" /etc/passwd;then echo "user1 exist";fi
user1 exist                ##grep -q 静默输出
[root@localhost shell]# if ! grep -wq "user2" /etc/passwd;then useradd user2;fi


·if [ ! -e file ]; then 表示文件不存在时会怎么样


·if (($a<1)); then …等同于 if [ $a -lt 1 ]; then…

·[ ] 中不能使用<,>,==,!=,>=,<=这样的符号





20.8/20.9 case判断


·格式: case  变量名 in

                      value1)

                          command

                          ;;

                     value2)

                          command

                          ;;

                      *)

                        commond

                          ;;

                      esac


::表示一个判断结束,进入下一个判断


·在case程序中,可以在条件中使用|,表示或的意思, 比如    

2|3)

     command

    ;;

· *)除以上所有之外的


·shell脚本案例

#!/bin/bash
read -p "Please input a number: " n        ##从标准输入读取输入并赋值给变量 n
if [ -z "$n" ]
then
echo "Please input a number."
exit 1##1是退出时的提示符
fi

n1=`echo $n|sed 's/[0-9]//g'`        ##从变量n中将数字替换为空,如果n1不为空,则不是纯数字,那么现实输入一个数字并退出
if [ -n "$n1" ]
then
echo "Please input a number."
exit 1
fi

if [ $n -lt 60 ] && [ $n -ge 0 ]            ##纯数字往下接着判断
then
tag=1                                        ##标记tag
elif [ $n -ge 60 ] && [ $n -lt 80 ]
then
tag=2
elif [ $n -ge 80 ]  && [ $n -lt 90 ]
then
tag=3
elif [ $n -ge 90 ] && [ $n -le 100 ]
then
tag=4
else
tag=0
fi
case $tag in
1)
echo "不及格"
;;
2)
echo "及格"
;;
3|4)
echo "优秀"
;;
*)
echo "The number range is 0-100."
;;
esac


·执行查看结果:

[root@localhost shell]# sh -x case.sh
+ read -p 'Please input a number: ' n
Please input a number: 77
+ '[' -z 77 ']'
++ echo 77
++ sed 's/[0-9]//g'
+ n1=
+ '[' -n '' ']'
+ '[' 77 -lt 60 ']'
+ '[' 77 -ge 60 ']'
+ '[' 77 -lt 80 ']'
+ tag=2
+ case $tag in
+ echo $'\345\217\212\346\240\274'
及格
[root@localhost shell]# sh -x case.sh
+ read -p 'Please input a number: ' n
Please input a number: 8sdafasdf                ##输入一个非纯数字
+ '[' -z 8sdafasdf ']'
++ echo 8sdafasdf
++ sed 's/[0-9]//g'
+ n1=sdafasdf
+ '[' -n sdafasdf ']'
+ echo 'Please input a number.'
Please input a number.
+ exit 1
[root@localhost shell]# sh -x case.sh
+ read -p 'Please input a number: ' n
Please input a number: 123                    ##输入一个大于100的数字
+ '[' -z 123 ']'
++ echo 123
++ sed 's/[0-9]//g'
+ n1=
+ '[' -n '' ']'
+ '[' 123 -lt 60 ']'
+ '[' 123 -ge 60 ']'
+ '[' 123 -lt 80 ']'
+ '[' 123 -ge 80 ']'
+ '[' 123 -lt 90 ']'
+ '[' 123 -ge 90 ']'
+ '[' 123 -le 100 ']'
+ tag=0
+ case $tag in
+ echo 'The number range is 0-100.'
The number range is 0-100.


2018-4-18 17周1次课 shell逻辑判断、文件目录属性判断、if、case

标签:shell

原文地址:http://blog.51cto.com/11530642/2105142

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!