如何迭代当前目录中的文件并排除某些具有特定名称模式的文件?该解决方案必须与POSIX兼容。
假设要排除的文件遵循以下模式: test0-9.txt和work-.* (使用regex)。
到目前为止,我的代码如下:
for file in *
do
if test "$file" != "test[0-9].txt" -o "$file" != "work-.*"
then
echo "$file"
fi
done
目前,输出是工作目录中的所有文件。我很确定测试中的模式匹配是不正确的,但是我如何修复它呢?
发布于 2021-04-01 14:42:02
[[
是用于bash的,对于POSIX shell,我猜case
可以为您完成glob风格的匹配:
for file in *
do
case $file in
test[0-9].txt | work-*) ;;
*) echo "$file";;
esac
done
发布于 2021-04-01 13:47:54
我认为你想要:
if ! [[ "$file" =~ "test[0-9].txt" ]] -a ! [[ "$file" =~ "work-.*" ]]
https://stackoverflow.com/questions/66898797
复制相似问题