Linux 解析 shell 脚本参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4882349/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 02:42:40  来源:igfitidea点击:

Parsing shell script arguments

linuxbashshellunix

提问by TIMEX

$myscript.sh -host blah -user blah -pass blah

I want to pass arguments into it.

我想将参数传递给它。

I'm used to doing $1, $2, $3....but I want to start naming them

我已经习惯了$1, $2, $3.... 但我想开始命名它们

采纳答案by William Pursell

There are lots of ways to parse arguments in sh. Getopt is good. Here's a simple script that parses things by hand:

有很多方法可以解析 sh 中的参数。Getopt 很好。这是一个手动解析事物的简单脚本:

#!/bin/sh
# WARNING: see discussion and caveats below
# this is extremely fragile and insecure

while echo  | grep -q ^-; do
    # Evaluating a user entered string!
    # Red flags!!!  Don't do this
    eval $( echo  | sed 's/^-//' )=
    shift
    shift
done

echo host = $host
echo user = $user
echo pass = $pass
echo args = $@

A sample run looks like:

示例运行如下所示:

$ ./a.sh -host foo -user me -pass secret some args
host = foo
user = me
pass = secret
args = some args

Note that this is not even remotely robust and massively open to security holes since the script eval's a string constructed by the user. It is merely meant to serve as an example for one possible way to do things. A simpler method is to require the user to pass the data in the environment. In a bourne shell (ie, anything that is not in the csh family):

请注意,由于脚本 eval 是由用户构造的字符串,因此这甚至不是远程健壮性和大规模开放的安全漏洞。它只是作为一种可能的做事方式的例子。更简单的方法是要求用户在环境中传递数据。在 bourne shell 中(即,任何不在 csh 系列中的东西):

$ host=blah user=blah pass=blah myscript.sh

works nicely, and the variables $host, $user, $passwill be available in the script.

工作得很好,和变量$host$user$pass将在脚本可用。

#!/bin/sh
echo host = ${host:?host empty or unset}
echo user = ${user?user not set}
...

回答by James Sumners

回答by Paused until further notice.

Here is a simple way to handle both long and short options:

这是处理多头和空头期权的简单方法:

while [[  == -* ]]; do
    case "" in
      -h|--help|-\?) show_help; exit 0;;
      -v|--verbose) verbose=1; shift;;
      -f) if (($# > 1)); then
            output_file=; shift 2
          else 
            echo "-f requires an argument" 1>&2
            exit 1
          fi ;;
      --) shift; break;;
      -*) echo "invalid option: " 1>&2; show_help; exit 1;;
    esac
done

From How can I handle command-line arguments (options) to my script easily?

如何轻松处理命令行参数(选项)到我的脚本?

回答by Kamil Kie?czewski

I adopt above William Pursell example (with Dennis Williamson advice) for parameters in this format: script -param1=value1 -param2=value2 ...

对于这种格式的参数,我采用了上面的 William Pursell 示例(与 Dennis Williamson 建议):脚本 -param1=value1 -param2=value2 ...

Here is code with one-line arguments parser (save it in file 'script'):

这是带有单行参数解析器的代码(将其保存在文件“脚本”中):

#!/bin/bash

while echo  | grep ^- > /dev/null; do declare $( echo  | sed 's/-//g' | sed 's/=.*//g' | tr -d '2')=$( echo  | sed 's/.*=//g' | tr -d '2'); shift; done

echo host = $host
echo user = $user
echo pass = $pass

You call it like that:

你这样称呼它:

script -host=aaa -user=bbb -pass=ccc

script -host=aaa -user=bbb -pass=ccc

and result is

结果是

host = aaa
user = bbb
pass = ccc

Do someone know shorter code to parse arguments than this above?

有人知道比上面更短的代码来解析参数吗?