如何检查文件名的bash脚本扩展? [英] How to check the extension of a filename in a bash script?
本文介绍了如何检查文件名的bash脚本扩展?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我写在bash每晚构建脚本。
一切都很好,很正常,除了一个小障碍:
I am writing a nightly build script in bash.
Everything is fine and dandy except for one little snag:
#!/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文件。
My problem is determining the file extension and then acting accordingly. I know the issue is in the if-statement, testing for a txt file.
我怎么能确定文件是否有一个.txt后缀?
How can I determine if a file has a .txt suffix?
推荐答案
我想你想说是$最后四个字符的文件等于 .TXT
? 如果是这样,你可以使用以下内容:
I think you want to say "Are the last four characters of $file equal to .txt
?" If so, you can use the following:
if [ ${file: -4} == ".txt" ]
注意之间的空间文件:
和 -4
是必需的,因为': - '修饰手段不同的东西。
Note that the space between file:
and -4
is required, as the ':-' modifier means something different.
这篇关于如何检查文件名的bash脚本扩展?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文