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

如何检查bash脚本中文件名的扩展名?

发布时间:2020-12-15 16:52:16 所属栏目:安全 来源:网络整理
导读:我在bash写一个每夜构建脚本。 一切都很好,除了一个小钩子: #!/bin/bashfor file in "$PATH_TO_SOMEWHERE"; do if [ -d $file ] then # do something directory-ish else if [ "$file" == "*.txt" ] # this is the snag then # do something txt-ish fi fi
我在bash写一个每夜构建脚本。
一切都很好,除了一个小钩子:
#!/bin/bash

for file in "$PATH_TO_SOMEWHERE"; do
      if [ -d $file ]
      then
              # do something directory-ish
      else
              if [ "$file" == "*.txt" ]       #  this is the snag
              then
                     # do something txt-ish
              fi
      fi
done;

我的问题是确定文件扩展名,然后相应地执行。我知道问题是在if语句,测试txt文件。

如何确定文件是否有.txt后缀?

我想你想说“是$文件的最后四个字符等于.txt吗?如果是,您可以使用以下方法:
if [ ${file: -4} == ".txt" ]

请注意,文件:和-4之间的空格是必需的,因为’: – ‘修饰符意味着不同。

(编辑:李大同)

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

    推荐文章
      热点阅读