bash的多个命令行参数依赖对方 [英] bash multiple command line arguments dependent on each other

查看:172
本文介绍了bash的多个命令行参数依赖对方的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在接受下列参数的脚本:
./script [-dph] [-peh]
第一个参数告诉脚本,如果我需要的生产脚本或开发脚本来执行它。第二个参数告知,如果生成的报告的输出应作为电子邮件发送或应该被重定向到一个HTML页面的脚本。

I have a script in which accepts the following arguments: ./script [-dph] [-peh] The first argument tells the script if I need to execute it with production scripts or with development scripts. The second argument tells the script if the output of the report generated should be sent as an email or should be redirected to a html page.

当我使用getopts的,我得到一个选项做一个选择。

When I use getopts, I get an option to do with one option.

while getopts "dph" opt_name
do
    case "$opt_name" in
        h)
            helpText
            exit 1
            ;;
        d)
            #Use Dev env
            ;;
        p)
            #Use Prod env
            ;;
        *)
            echo "Wrong command line argument. Exiting...";
            exit 1
            ;;
    esac
done

我如何在这第二个参数是指基于第一个参数不同的操作两个选项呢?

How do I do it with two options in which the second argument means a different action based on the first argument?

PS:请注意,我知道有用于开发和督促ENV选项是不是最好的解决办法左右,但我的应用程序是在概念阶段的证明,因此,我用的什么我知道建这个

P.S.: Please note that I know that having options for dev and prod env is not the best solution around, but my app is in a proof of concept stage and hence, I am using whatever I know to build this.

推荐答案

这是也许不是最优雅的解决方案,我很乐意了解更好的方法,但它的工作原理和它的作用是显而易见的。

This is maybe not the most elegant solution and I'd be happy to find out about better approaches, but it works and what it does is clear.

在getopts的部分,你解析命令行选项,并将其分配给变量。在第二部分,你控制的CLI选项的依赖,你可以直接在控制结构执行的操作。

In the getopts part you parse the command line options and assign them to variables. In the second part you control the cli option dependencies and you can directly execute the actions in the control structure.

#!/bin/bash

while getopts "DPehp" opt_name 
do
    case "$opt_name" in
        h)
            helpText
            exit 1
            ;;
        D)  
            #Use Dev env
            dev=true
            ;;
        P)  
            #Use Prod env
            prod=true
            ;;
        p)  
            p=true
            ;;
        e)  
            e=true
            ;;
        *)
            echo "Wrong command line argument. Exiting...";
            exit 1
            ;;
    esac 
done

if [[ $dev == true && $prod == true ]] || [[ $dev != true && $prod != true ]]
then
    echo "You must choose between development (-D) or production (-P) scripts"
    exit 
fi

if [[ $dev == true ]] 
then
    if [[ $p == true ]] 
    then
        echo "Your 'p' dev action goes here"
    fi
    if [[ $e == true ]] 
    then
        echo "Your 'e' dev action goes here"
    fi 
elif [[ $prod == true ]] 
then
    if [[ $p == true ]] 
    then 
        echo "Your 'p' prod action goes here"
    fi
    # and so on fi
fi

这篇关于bash的多个命令行参数依赖对方的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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