Dynamic case statement in bash

前端 未结 5 1360
执念已碎
执念已碎 2020-12-20 01:25

I\'m trying to figure out how to create a dynamic case statement in a bash script.

For example, let\'s say I have the output of an awk statement with the following c

5条回答
  •  清酒与你
    2020-12-20 01:58

    You can create a dynamic case statement in bash by doing the following:

    1) ensure the list is PIPE (|) seperated. IE. red|green|blue

    2) wrap your case statement in an eval

    For example:

    valid="red|green|blue"
    
    eval "case \"$choice\" in
        $valid)
            echo do something good here
            ;;
        *)
            echo invalid colour
            ;;
    esac"
    

    This works for simple variable processing, I can not guarantee this will work in all cases.

提交回复
热议问题