我正在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 ?
我猜'$PATH_TO_SOMEWHERE'是类似于'<目录>/*'。
在这种情况下,我将代码更改为:
find <directory> -maxdepth 1 -type d -exec ... \;
find <directory> -maxdepth 1 -type f -name "*.txt" -exec ... \;
如果你想对目录和文本文件名做一些更复杂的事情,你可以:
find <directory> -maxdepth 1 -type d | while read dir; do echo $dir; ...; done
find <directory> -maxdepth 1 -type f -name "*.txt" | while read txtfile; do echo $txtfile; ...; done
如果文件名中有空格,你可以:
find <directory> -maxdepth 1 -type d | xargs ...
find <directory> -maxdepth 1 -type f -name "*.txt" | xargs ...
我猜'$PATH_TO_SOMEWHERE'是类似于'<目录>/*'。
在这种情况下,我将代码更改为:
find <directory> -maxdepth 1 -type d -exec ... \;
find <directory> -maxdepth 1 -type f -name "*.txt" -exec ... \;
如果你想对目录和文本文件名做一些更复杂的事情,你可以:
find <directory> -maxdepth 1 -type d | while read dir; do echo $dir; ...; done
find <directory> -maxdepth 1 -type f -name "*.txt" | while read txtfile; do echo $txtfile; ...; done
如果文件名中有空格,你可以:
find <directory> -maxdepth 1 -type d | xargs ...
find <directory> -maxdepth 1 -type f -name "*.txt" | xargs ...