Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
迭代文件并排除具有特定名称模式Shell的文件_Shell_Posix - Fatal编程技术网

迭代文件并排除具有特定名称模式Shell的文件

迭代文件并排除具有特定名称模式Shell的文件,shell,posix,Shell,Posix,如何迭代当前目录中的文件并排除某些具有特定名称模式的文件?解决方案必须与POSIX兼容 假设要排除的文件遵循以下模式:test[0-9].txt和work-.*(使用regex) 到目前为止,我掌握的代码是: for file in * do if test "$file" != "test[0-9].txt" -o "$file" != "work-.*" then echo

如何迭代当前目录中的文件并排除某些具有特定名称模式的文件?解决方案必须与POSIX兼容

假设要排除的文件遵循以下模式:test[0-9].txt和work-.*(使用regex)

到目前为止,我掌握的代码是:

for file in * 
do 
    if test "$file" != "test[0-9].txt" -o "$file" != "work-.*"
    then 
        echo "$file"
    fi 
done 
此时,输出是工作目录中的所有文件。我很确定测试中的模式匹配是不正确的,但我如何修复它?

我想您需要:

if ! [[ "$file" =~ "test[0-9].txt" ]] -a ! [[ "$file" =~ "work-.*" ]]
我想你想要:

if ! [[ "$file" =~ "test[0-9].txt" ]] -a ! [[ "$file" =~ "work-.*" ]]

[[
是针对bash的,对于posixshell,我想可以为您进行glob样式匹配:

for file in *
do
    case $file in
        test[0-9].txt | work-*) ;;
        *) echo "$file";;
    esac
done

[[
是针对bash的,对于posixshell,我想可以为您进行glob样式匹配:

for file in *
do
    case $file in
        test[0-9].txt | work-*) ;;
        *) echo "$file";;
    esac
done

可以
test
进行模式匹配吗?@georgexsh我不确定,是否有其他方法排除文件?可以
test
进行模式匹配吗?@georgexsh我不确定,是否有其他方法排除文件?它的意思是
/test.sh:11:./test.sh:[[:找不到
并且在该错误行之后仍然显示不需要的文件。它的意思是
/test.sh:11:./test.sh:[:找不到
并且在该错误行之后仍然显示不需要的文件。