加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

shell – 检查文件是否存在以及它是否包含特定的字符串

发布时间:2020-12-15 19:46:53 所属栏目:安全 来源:网络整理
导读:我想检查file2.sh是否存在,如果一个特定的单词,诗人是文件的一部分。我使用grep创建变量used_var。 #!/bin/kshfile_name=/home/file2.sh used_var=`grep "poet" $file_name` 如何检查used_var是否具有某些值? 而不是将grep的输出存储在变量中,然后检查变
我想检查file2.sh是否存在,如果一个特定的单词,诗人是文件的一部分。我使用grep创建变量used_var。
#!/bin/ksh

file_name=/home/file2.sh                  
used_var=`grep "poet" $file_name`

如何检查used_var是否具有某些值?

而不是将grep的输出存储在变量中,然后检查变量是否为空,您可以这样做:
if grep -q "poet" $file_name
then
    echo "poet was found in $file_name"
fi

============

以下是一些常用的测试:

-d FILE
          FILE exists and is a directory
   -e FILE
          FILE exists
   -f FILE
          FILE exists and is a regular file
   -h FILE
          FILE exists and is a symbolic link (same as -L)
   -r FILE
          FILE exists and is readable
   -s FILE
          FILE exists and has a size greater than zero
   -w FILE
          FILE exists and is writable
   -x FILE
          FILE exists and is executable
   -z STRING
          the length of STRING is zero

例:

if [ -e "$file_name" ] && [ ! -z "$used_var" ]
then
    echo "$file_name exists and $used_var is not empty"
fi

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读