Shell脚本参数 [英] Shell Script parameters

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

问题描述

什么是解析在shell脚本命令参数,然后验证呢?最好的办法

What is the best way to parse parameters in shell script command, and then validate it?

例如庆典someScript.sh -p<&SOME_PATH GT; -O - 的基; some_other_param> -i(用户忘记提供值)

如何分析所有这些参数,并在用户忘记输入一些参数或该参数的值显示一些错误消息,并终止脚本执行?

How to parse all of this parameters and when user forget to input some parameters or value of this parameter show some error message and terminate executing of script?

推荐答案

使用的 getopt的 getopts的

有很多例子在这个网站,但这里是多了一个:

There are lots of examples on this site, but here is one more:

#!/usr/bin/env bash

p_set=false
o_set=false
i_set=false
while getopts p:o:i: OPT; do
    case "${OPT}" in
        p)
            p_set=true
            some_path=${OPTARG}
            ;;
        o)
            o_set=true
            some_other_param=${OPTARG}
            ;;
        i)
            i_set=true
            # Process ${OPTARG} or report error if it's not provided
            ;;
    esac
done

if ! $i_set ; then
    echo "-i must be provided"
fi

这篇关于Shell脚本参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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