【问题标题】:If one getopts flag is active, how do I make another flag behave a certain way?如果一个 getopts 标志处于活动状态,我如何使另一个标志以某种方式运行?
【发布时间】:2015-10-09 22:29:28
【问题描述】:

如果一个 getopts 标志设置为 true,我如何让另一个标志根据它来行动?

option_r=false
option_v=false

while getopts 'r:v' option
do
 case "$option" in

 r) option_r=true
    *Just to set the flag to true*
    ;;

 v) option_v=true
    if *option -r is set to true*
    then
    cp -r directory1 directory2

    if *option -r is set to false*
    then
    cp directory1 directory2 (copy normally)
    ;;

我希望我正确地解释了自己。

基本上,如果一个标志打开,我希望它反映在另一个选项上。如果选项 -r 处于活动状态,则递归复制,否则,正常复制。

提前致谢!

【问题讨论】:

    标签: android bash shell sh


    【解决方案1】:

    我会分两步完成:首先获取参数,然后应用逻辑。这是一个例子:

    option_r=false
    option_v=false
    while getopts "rv" opt; do
        case $opt in
            r)  
                option_r=true
                ;;  
            v)  
                option_v=true
                ;;  
        esac
    done
    
    if [ "$option_v" = true ] ; then
        if [ "$option_r" = true ] ; then
            cp -r directory1 directory2
        else
            cp directory1 directory2
        fi  
    fi
    

    【讨论】:

    • 这是必要的,因为getopts 不关心选项的顺序。您必须同时满足command -r -vcommand -v -r
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-09
    • 1970-01-01
    • 2012-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-08
    相关资源
    最近更新 更多