繁体   English   中英

我如何将bash转换为sh

[英]How I can convert bash into sh

我想使用正则表达式,它可以在bash中使用,但是在sh中

str=9009
for i in *v[0-9][0-9][0-9][0-9].txt; do echo "$i";      
       if ! [[ "$i" =~ $str ]]; then rm "$i" ; fi    done

像这样的文件名:mari_v9009.txt femme_v9009.txt mari_v9010.txt femme_v9010.txt,mari.txt,femme.txt

所以我想删除这些文件:mari_v9010.txt femme_v9010.txt

这可能是您要寻找的:

case $i in
    *"$str"* ) # do nothing
        ;;
    * ) rm "$i"
        ;;
esac

从您的评论看来,您实际上是在寻求帮助,而不是if语句中的正if表达式。 尝试这个:

str=9009
for i in ./*v[0-9][0-9][0-9][0-9].txt; do
    echo "$i" >&2
    if [ -e "$i" ]; then
        case $i in
            *"$str"* )
                echo "match for $str in $i" > &2
                ;;
            * ) echo "no match for $str in $i" >&2
                ;;
        esac
    else
         echo "no file name matching $i in directory" >&2
    fi
done

在测试后将echo更改为您想要的任何值。

还请考虑不要执行上述任何操作,而是执行以下操作:

find . -maxdepth 1 -name '*v[0-9][0-9][0-9][0-9].txt' ! -name "*$str*" -exec echo rm {} \;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM