文件目录属性判断、if特殊用法、case判断、for循环

文件目录属性判断

 [ -f file ]判断是否是普通文件,且存在
 [ -d file ] 判断是否是目录,且存在
 [ -e file ] 判断文件或目录是否存在
 [ -r file ] 判断文件是否可读
 [ -w file ] 判断文件是否可写
 [ -x file ] 判断文件是否可执行
  • [ -f file ]判断是否是普通文件,且存在
[root@garytao-01 shell]# vi filel.sh
[root@garytao-01 shell]# cat filel.sh 
#!/bin/bash
f="/tmp/aminglinux"
if [ -f $f ]
then 
    echo $f exist
else
    touch $f
fi

##第一次执行,会创建该文件
[root@garytao-01 shell]# sh -x filel.sh   
+ f=/tmp/aminglinux
+ '[' -f /tmp/aminglinux ']'
+ touch /tmp/aminglinux

#第二次执行,会提示该文件已存在
[root@garytao-01 shell]# sh -x filel.sh  
+ f=/tmp/aminglinux
+ '[' -f /tmp/aminglinux ']'
+ echo /tmp/aminglinux exist
/tmp/aminglinux exist
  • [ -d file ] 判断是否是目录,且存在
[root@garytao-01 shell]# cp filel.sh filel2.sh
[root@garytao-01 shell]# vi filel2.sh 
[root@garytao-01 shell]# cat filel2.sh 
#!/bin/bash
f="/tmp/aminglinux"
if [ -d $f ]
then 
    echo $f exist
else
    mkdir $f
fi

#没有这个目录,就touch一个目录
[root@garytao-01 shell]# sh -x filel2.sh 
+ f=/tmp/aminglinux
+ '[' -d /tmp/aminglinux ']'
+ mkidr /tmp/aminglinux

[root@xietaolinux3 shell]# sh -x filel2.sh 
+ f=/tmp/aminglinux
+ '[' -d /tmp/aminglinux ']'
+ mkdir /tmp/aminglinux
mkdir: 无法创建目录"/tmp/aminglinux": 文件已存在
  • [ -e file ] 判断文件或目录是否存在

目录和文件都可以touch的,touch的目的是如果这个文件或目录不存在,它会创建这个文件,如果这个文件或目录存在了,在touch就会更改这个文件的三个time

[root@garytao-01 shell]# vi filel2.sh 
[root@garytao-01 shell]# cat filel2.sh 
#!/bin/bash
f="/tmp/aminglinux"
if [ -e $f ]
then 
    echo $f exist
else
    touch $f
fi

[root@garytao-01 shell]# sh -x filel2.sh 
+ f=/tmp/aminglinux
+ '[' -e /tmp/aminglinux ']'
+ echo /tmp/aminglinux exist
/tmp/aminglinux exist
  • [ -r file ] 判断文件是否可读
[root@garytao-01 shell]# vi filel2.sh
[root@garytao-01 shell]# cat filel2.sh 
#!/bin/bash
f="/tmp/aminglinux"
if [ -r $f ]
then 
    echo $f readable
fi

[root@garytao-01 shell]# sh -x filel2.sh 
+ f=/tmp/aminglinux
+ '[' -r /tmp/aminglinux ']'
+ echo /tmp/aminglinux readable
/tmp/aminglinux readable
  • [ -w file ] 判断文件是否可写
[root@garytao-01 shell]# vi filel2.sh
[root@garytao-01 shell]# cat filel2.sh 
#!/bin/bash
f="/tmp/aminglinux"
if [ -w $f ]
then 
    echo $f writeable
fi
[root@garytao-01 shell]# sh -x filel2.sh 
+ f=/tmp/aminglinux
+ '[' -w /tmp/aminglinux ']'
+ echo /tmp/aminglinux writeable
/tmp/aminglinux writeable
root@garytao-01 shell]# ls -l /tmp/aminglinux 
-rw-r--r-- 1 root root 0 Feb  3 14:52 /tmp/aminglinux

判断是否可读可写,实际上是去判断执行shell脚本的当前用户,如果使用其它用户,或许就只是可读。

  • [ -x file ] 判断文件是否可执行
[root@garytao-01 shell]# vi filel2.sh
[root@garytao-01 shell]# cat filel2.sh 
#!/bin/bash
f="/tmp/aminglinux"
if [ -x $f ]
then 
    echo $f exeable
fi

#因为不可执行,所以没有输出
[root@garytao-01 shell]# sh filel2.sh  
常用案例
  • 并且 &&
f="/tmp/yongge"
[ -f $f ] && rm -f $f #前一条命令执行成功才会继续执行之后的命令
等同于下面的表达方式
if [ -f $f ]     
then
      rm -rf $f
fi
  • 或者 ||
f="/tmp/yongge"
[ -f $f ] || touch $f  
#如果前面的文件不存在,就去创建这个文件,前面命令不成功时,执行后面的命令
if [ ! -f $f ]       
# “!”表示取反,如果这条命令不成功,就往下执行,touch这个文件
then
      touch $f
fi

if特殊用法

if [ -z "$a" ] 这个表示当变量a的值为空时会怎么样
if [ -n "$a" ] 表示当变量a的值不为空
if grep -q '123' 1.txt; then 表示如果1.txt中含有'123'的行时会怎么样
if [ ! -e file ]; then 表示文件不存在时会怎么样
if (($a<1)); then …等同于 if [ $a -lt 1 ]; then…
[ ] 中不能使用<,>,==,!=,>=,<=这样的符号
if 特殊用法实战
if -z或者if -n 都不能作用在文件上,只能作用在变量上。
if [ -z "$a" ] 这个表示当变量a的值为空时会怎么样
-z 表示为空
!-z=-n
!-n=-z

无论使用-z或使用-n去判断一个变量是否为空,都需要加双影号。
  • if [ -z "$a" ]  这个表示当变量a的值为空时会怎么样
[root@garytao-01 shell]# vi if4.sh
[root@xietaolinux3 shell]# cat if4.sh 
#!/bin/bash
n= `wc -l /tmp/lalal`
if [ $n -gt 100 ]
then
   echo alsdfaf
fi
[root@garytao-01 shell]# sh -x if4.sh 
++ wc -l /tmp/lalal
wc: /tmp/lalal: 没有那个文件或目录
+ n=
+ '[' -gt 100 ']'
if4.sh: 第 3 行:[: -gt: 期待一元表达式


[root@garytao-01 shell]# vi if4.sh
[root@garytao-01 shell]# cat if4.sh 
#!/bin/bash
n=`wc -l /tmp/lalal`
if [ -z "$n" ]
then
    echo error
    exit
elif [ $n -gt 100 ]
then
    echo aladafaf
fi
[root@garytao-01 shell]# sh -x if4.sh 
++ wc -l /tmp/lalal
wc: /tmp/lalal: 没有那个文件或目录
+ n=
+ '[' -z '' ']'
+ echo error
error
+ exit

[root@garytao-01 shell]# vi if4.sh
[root@garytao-01 shell]# cat if4.sh 
#!/bin/bash
if [ ! -f /tmp/lalal ]
then
    echo "/tmp/lalal not exist."
    exit
fi
n=`wc -l /tmp/lalal`
if [ -z "$n" ]
then
    echo error
    exit
elif [ $n -gt 100 ]
then
    echo alsdflljk
fi

[root@garytao-01 shell]# sh -x if4.sh 
+ '[' '!' -f /tmp/lalal ']'
+ echo '/tmp/lalal not exist.'
/tmp/lalal not exist.
+ exit
[root@garytao-01 shell]# sh if4.sh 
/tmp/lalal not exist.
  • if [ -n "$a" ] 表示当变量a的值不为空

if [ -n "$a" ] 表示当变量a的值不为空,或者说这个文件内容不为空,那这样不仅可以判断一个变量,也可以判断一个文件。

-n 判断变量的时候需要用""双引号引起来,若是文件的时候,则不需要用双引号引起来。

[root@garytao-01 shell]# ls
01.sh  filel2.sh  filel.sh  for1.sh  if1.sh  if2.sh  if3.sh  if4.sh
[root@garytao-01 shell]# if [ -n 01.sh ]; then echo ok; fi
ok
[root@garytao-01 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@garytao-01 shell]# grep -w 'user1' /etc/passwd
user1:x:1002:1002::/home/user1:/bin/bash

#会把过滤的内容输出来
[root@garytao-01 shell]# if grep -w 'user1' /etc/passwd; then echo "user1 exist"; fi
user1:x:1002:1002::/home/user1:/bin/bash
user1 exist

#加-q,只做过滤,不把过滤内容输出
[root@garytao-01 shell]# if grep -wq 'user1' /etc/passwd; then echo "user1 exist"; fi
user1 exist

#若是想创建一个用户,直接取反即可 !取反
[root@garytao-01 shell]# if ! grep -wq 'user1' /etc/passwd; then useradd user1; fi

case判断

  • 可以查看vi /etc/init.d/network文件的case判断例子

  • case判断脚本格式

  • shell脚本案例

脚本目的是 输入一个数字,然后用脚本去判断这个数字的范围

[root@garytao-01 shell]# vi case.sh
[root@garytao-01 shell]# cat case.sh 

#!/bin/bash
#判断是否输入有数值,空直接结束整个文本
read -p "Please input a number: " n
#read 让用户输出一些字符串;赋值给最后一个变量;这里的赋值是“n”
if [ -z "$n" ]  #当用户没有输入的时候,-z表示变量n为空
then
    echo "Please input a number."
    exit 1
fi
#n1将输入的数值清空数字,检查变量是否为空,如果不为空,就证明输入有其他的字符,告知用户,请输入一个数字
n1=`echo $n|sed 's/[0-9]//g'`    #确定,n变量是否为数字
if [ -n "$n1" ]
then
    echo "Please input a number."
    exit 1
#elif [ $n -lt 0 ] || [ $n -gt 100 ]
#then
#    echo "The number range is 0-100."
#    exit 1
fi

if [ $n -lt 60 ] && [ $n -ge 0 ]
then
    tag=1
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 "not ok"
        ;;
    2)
        echo "ok"
        ;;
    3)
        echo "ook"
        ;;
    4)
        echo "oook"
        ;;
    *)
        echo "The number range is 0-100."
        ;; 
esac


[root@garytao-01 shell]# sh case.sh 
Please input a number: 101
The number range is 0-100.

[root@garytao-01 shell]# sh -x case.sh 
+ read -p 'Please input a number: ' n
Please input a number: 101
+ '[' -z 101 ']'
++ echo 101
++ sed 's/[0-9]//g'
+ n1=
+ '[' -n '' ']'
+ '[' 101 -lt 60 ']'
+ '[' 101 -ge 60 ']'
+ '[' 101 -lt 80 ']'
+ '[' 101 -ge 80 ']'
+ '[' 101 -lt 90 ']'
+ '[' 101 -ge 90 ']'
+ '[' 101 -le 100 ']'
+ tag=0
+ case $tag in
+ echo 'The number range is 0-100.'
The number range is 0-100.

for循环

  • for循环案例1
  1. 1+2+3..+100的和
  2. sum 第一次作为变量的时候,是0;当进入for循环里面的时候,每运算一次,sum变量就会改变一次,直至$i 结束;最后输出结果 $sum
  3. 在做加减法的时候,[ ] 方括号里面不需要加空格
#打印1到100
[root@garytao-01 shell]# vi for1.sh
[root@garytao-01 shell]# cat for1.sh 
#!/bin/bash
for i in `seq 1 100 `
#seq  1到100个数字
do 
   echo $i
done
[root@garytao-01 shell]# sh for1.sh 
1
2
3
4
5
6
7
8
9
10
11
...

#打印数字叠加的值
[root@garytao-01 shell]# vi for1.sh
[root@garytao-01 shell]# cat for1.sh 
#!/bin/bash
sum=0
for i in `seq 1 100 `
do 
   sum=$[$sum+$i]  #核心语句
done
echo $sum
[root@garytao-01 shell]# sh for1.sh 
5050

#每次循环相加的值
[root@garytao-01 shell]# vi for1.sh
[root@garytao-01 shell]# cat for1.sh 
#!/bin/bash
sum=0
for i in `seq 1 100`
do 
   echo "$sum + $i"
   sum=$[$sum+$i]
   echo $sum
done
echo $sum
[root@garytao-01 shell]# sh for1.sh 
0 + 1
1
1 + 2
3
3 + 3
6
....
4950 + 100
5050

  • for循环案例2

文件列表循环 列出所有 etc目录下的子目录

[root@garytao-01 shell]# vi for2.sh
[root@garytao-01 shell]# cat for2.sh 
#!/bin/bash
cd /etc/
for a in ls /etc/
do
    if [ -d $a ]
    then
        echo $a
        ls $a
    fi
done

[root@garytao-01 shell]# sh for2.sh
/etc/
adjtime			 exports.d	     ld.so.conf.d	       pki		 shadow-
aliases			 favicon.png	     lftp.conf		       plymouth		 shells
aliases.db		 filesystems	     libaudit.conf	       pm		 skel
alternatives		 firewalld	     libnl		       polkit-1		 ssh
anacrontab		 fonts		     libuser.conf	       popt.d		 ssl
asound.conf		 fstab		     locale.conf	       postfix		 statetab
audisp			 fuse.conf	     localtime		       ppp		 statetab.d
audit			 gcrypt		     login.defs		       prelink.conf.d	 subgid
bash_completion.d	 GeoIP.conf	     logrotate.conf	       printcap		 subuid

  • for循环会把命令空格及回车当作是一个分隔符,这个在写脚本的时候需要注意,如下示例

转载于:https://my.oschina.net/u/3708153/blog/2993153

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值