BASH不以防万一 [英] BASH Not In Case

查看:61
本文介绍了BASH不以防万一的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出一种在情况下执行NOT子句的理智方法.我这样做的原因是为了遇到案件时进行转码,也就是如果我按下了avi,则没有理由再次将其转换为avi,我可以只需将其移开即可(这是我的案例应该的范围).反正我有一些我写出的 proto 代码给出了我要做什么的要旨.

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

预期结果将是这样

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

非常感谢您的帮助.

推荐答案

这与 case 的设计相反,后者仅执行第一个匹配项.如果要在多个匹配项上执行(并且在您的设计中, 3 的内容将要同时在 1 上执行)2 ),那么 case 是错误的构造.使用多个 if 块.

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"

因为大小写仅执行第一个匹配,所以只有一个"did-not-match"处理程序才有意义;这就是 *)掉线的目的.

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.

这篇关于BASH不以防万一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆