免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3084 | 回复: 6
打印 上一主题 下一主题

[文本处理] Bash Shell_文件属性判断 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2019-04-22 09:52 |只看该作者 |倒序浏览
新手一个,参照<<Unix Shell by Example>>Example 8.19学习shell编程。
执行sh FILEtesting后没有任何显示,请大家帮忙看看,非常感谢!

#!/bin/sh
#Scriptname: FILEtesting
FILE=/etc/resolv.conf
if [ -d $FILE ]
then
    echo "$FILE is a directory"
elif [ -f $FILE ]
then
    if [ -r $FILE -a -w $FILE -a -x $FILE ]
    then
        echo "You have read, write, and execute permission on $FILE"
    fi
else
    echo "$FILE is neither a FILE or a directory"
fi

论坛徽章:
0
2 [报告]
发表于 2019-04-22 10:09 |只看该作者
本帖最后由 jzsjm1002 于 2019-04-22 10:22 编辑
  1. [root@vh120 mnt]# ll /etc/resolv.conf
  2. -rw-r--r--. 1 root root 95 Apr 12 11:15 /etc/resolv.conf

  3. resolv.conf并不是可执行文件

  4. elif [ -f $FILE ]
  5. 判断为真 就不会往下面的else走了


  6. #!/bin/bash
  7. #Scriptname: FILEtesting
  8. FILE=/etc/resolv.conf

  9. if [ -d $FILE ];then
  10.   echo "$FILE is a directory"
  11. elif [ -f $FILE ];then

  12.   if [ -r $FILE -a -w $FILE -a -x $FILE ];then
  13.     echo "You have read, write, and execute permission on $FILE"
  14.   else
  15.     echo "$FILE is neither a FILE or a directory"
  16.   fi

  17. fi

  18. 改成这样就可以了
复制代码

论坛徽章:
0
3 [报告]
发表于 2019-04-22 10:57 |只看该作者
感谢jzsjm1002的指点,之前理解有误,我以为如果不是目录,就会输出显示"该文件既不是文件,也不是目录"

代码重新修改了一下
#!/bin/sh
#Scriptname: FILEtesting
FILE=./testing
if [ -d $FILE ]
then
    echo "$FILE is a directory"
exit
fi
if [ -f $FILE ]
then
    if [ -r $FILE -a -w $FILE -a -x $FILE ]
    then
        echo "You have read, write, and execute permission on $FILE"
    else
        echo "Sorry, you don't have permissions!"
    fi
else
    echo "$FILE is neither a file or a directory"
fi

论坛徽章:
0
4 [报告]
发表于 2019-04-22 15:22 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
0
5 [报告]
发表于 2019-04-23 10:04 |只看该作者
我的最终目的是NetDevOps,目前已经学了CentOS系统管理部分,sed/awk语法部分,awk编程简单了解了一下,没有打算深入。现在开始学习bash shell,深入学习,大概持续一到两个月。下一个阶段开始系统学习Python,大概三到四个月。最后是Ansible。有什么建议?

PowerShell看起来似乎跟这个关联不大?

论坛徽章:
0
6 [报告]
发表于 2019-04-23 15:51 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
33
ChinaUnix元老
日期:2015-02-02 08:55:39CU十四周年纪念徽章
日期:2019-08-20 08:30:3720周年集字徽章-周	
日期:2020-10-28 14:13:3020周年集字徽章-20	
日期:2020-10-28 14:04:3019周年集字徽章-CU
日期:2019-09-08 23:26:2519周年集字徽章-19
日期:2019-08-27 13:31:262016科比退役纪念章
日期:2022-04-24 14:33:24
7 [报告]
发表于 2019-05-17 08:36 |只看该作者
回复 5# hqmb


现在开始学习bash shell,深入学习,大概持续一到两个月。下一个阶段开始系统学习Python,大概三到四个月。最后是Ansible。有什么建议?

这条路子很正,请坚持。
要学习的东西有很多,不用被其他花里胡哨的东西分散了基础。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP