BASH Not In Case

夙愿已清 提交于 2020-12-26 06:39:39

问题


I am trying to figure out a sane way to do a NOT clause in a case. The reason I am doing this is for transcoding when a case is met, aka if I hit an avi, there's no reason to turn it into an avi again, I can just move it out of the way (which is what the range at the base of my case should do). Anyway, I have some proto code that I wrote out that kind of gives the gist of what I am trying to do.

#!/bin/bash
for i in $(seq 1 3); do 

    echo "trying: $i"

    case $i in
        ! 1)    echo "1" ;;     # echo 1 if we aren't 1
        ! 2)    echo "2" ;;     # echo 2 if we aren't 2
        ! 3)    echo "3" ;;     # echo 3 if we aren't 3
        [1-3]*) echo "! $i" ;;  # echo 1-3 if we are 1-3
    esac

    echo -e "\n"

done

expected results would be something like this

2 3 ! 1
1 3 ! 2
1 2 ! 3

Help is appreciated, thanks.


回答1:


This is contrary to the design of case, which executes only the first match. If you want to execute on multiple matches (and in your design, something which is 3 would want to execute on both 1 and 2), then case is the wrong construct. Use multiple if blocks.

[[ $i = 1 ]] || echo "1"
[[ $i = 2 ]] || echo "2"
[[ $i = 3 ]] || echo "3"
[[ $i = [1-3]* ]] && echo "! $i"

Because case only executes the first match, it only makes sense to have a single "did-not-match" handler; this is what the *) fallthrough is for.




回答2:


You can do this with the extglob extension.

$ shopt -s extglob
$ case foo in !(bar)) echo hi;; esac
hi
$ case foo in !(foo)) echo hi;; esac
$


来源:https://stackoverflow.com/questions/21919182/bash-not-in-case

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!