ll文件显示为?????_关于shell编程中的文件测试简单的操作实例

谈一谈关于shell编程中的文件测试

Shell编程有时处理一个对象时,需要我们对对象进行测试。

只有符合要求的才采取下一步操作,这样做的好处可以避免程序出错。

这个测试的对象可以是文件、字符串、数字等等。

下面我们来简单的看一下对于文件的测试。

1、 文件测试

格式:

[ 操作符 文件目录 ]

常用操作符:

-d 测试是否为一个目录

-e 测试一个文件是否存在

-s 测试文件是否存在且长度不为0

-f 测试是否为一个普通文件

-r 测试文件是否存在且对于当前用户可读

-w 测试文件是否存在且对于当前用户可写

-x 测试文件是否存在且对于当前用户可执行

-L 测试文件是否存在且为链接文件

案例:

(1) 、-f参数使用

//简单的使用&& 和 ||

[ -f /etc/passwd ] && echo "I am file" || echo "not file"

I am file

[ -f /etc ] && echo "I am file" || echo "not file"

not file

1497939e52b87407e428902cb0b34df7.png

(2) 、-d参数使用

[odysee@kingdom shellFiles]$ [ -d /etc ] && echo "I am dir" || echo "not dir"

I am dir

ffe6ba6ace84de79cd920e19b7b1601d.png

(3) 、-e参数使用

[odysee@kingdom shellFiles]$ [ -e /etc/passwd ] && echo "yes" || echo "no"

yes

[odysee@kingdom shellFiles]$ [ -e /etc/passwds ] && echo "yes" || echo "no"

no

76a4552ed66d8fea1ddbaa844751995c.png

(4) 、-r -w -x 使用

[ -r /etc/passwd ] && echo "yes" || echo "no"

yes

[ -w /etc/passwd ] && echo "yes" || echo "no"

no

[ -x /etc/passwd ] && echo "yes" || echo "no"

no

//查看文件权限,很明显只有r权限

ll /etc/passwd

-rw-r--r-- 1 root root 1490 Jan 25 09:53 /etc/passwd

42a0e002277294606d0d07b300bc2127.png

(5) 、-L参数使用

[odysee@kingdom shellFiles]$ which python2

/usr/bin/python2

[odysee@kingdom shellFiles]$ ll /usr/bin/python2

lrwxrwxrwx 1 root root 6 Aug 24 2017 /usr/bin/python2 -> python

[odysee@kingdom shellFiles]$ [ -L /usr/bin/python2 ] && echo "yes" || echo "no"

yes

6097ff50ac192e9602917372e4ee08fd.png

补充:

!:求反

//对-f /etc/passwd求反,即把true变为false

//false则打印||

[ ! -f /etc/passwd ] && echo "I am not file" || echo "file"

file

//对-f /etc求反,即把false变为true

//true则打印&&

[ ! -f /etc ] && echo "I am not file" || echo "file"

I am not file

3ebe98569e73d78dca4a9213a025ea50.png

测试完成

欢迎大家给予宝贵的意见或者建议。

欢迎大家补充或者共享一些其他的方法。

感谢支持。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值