bash shell 脚本的可变参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9951655/
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-09-18 01:55:21  来源:igfitidea点击:

Variable arguments to a shell script

bash

提问by Kitcha

I would like to have my script accepting variable arguments. How do I check for them individually?

我想让我的脚本接受可变参数。我如何单独检查它们?

For example

例如

./myscript arg1 arg2 arg3 arg4

 or 

./myscript arg4 arg2 arg3

The arguments can be any number and in any order. I would like to check if arg4 string is present or not irrespective of the argument numbers.

参数可以是任何数字和任何顺序。无论参数编号如何,我都想检查 arg4 字符串是否存在。

How do I do that?

我怎么做?

Thanks,

谢谢,

回答by ruakh

The safest way — the way that handles all possibilities of whitespace in arguments, and so on — is to write an explicit loop:

最安全的方法——处理参数中所有可能的空格的方法,等等——是编写一个显式循环:

arg4_is_an_argument=''
for arg in "$@" ; do
    if [[ "$arg" = 'arg4' ]] ; then
        arg4_is_an_argument=1
    fi
done
if [[ "$arg4_is_an_argument" ]] ; then
    : the argument was present
else
    : the argument was not present
fi

If you're certain your arguments won't contain spaces — or at least, if you're not particularly worried about that case — then you can shorten that to:

如果您确定您的参数不包含空格——或者至少,如果您不是特别担心这种情况——那么您可以将其缩短为:

if [[ " $* " == *' arg4 '* ]] ; fi
    : the argument was almost certainly present
else
    : the argument was not present
fi

回答by chbrown

This is playing fast and loose with the typical interpretation of command line "arguments", but I start most of my bash scripts with the following, as an easy way to add --helpsupport:

这是对命令行“参数”的典型解释的快速和松散,但我使用以下内容开始我的大多数 bash 脚本,作为添加--help支持的简单方法:

if [[ "$@" =~ --help ]]; then
  echo 'So, lemme tell you how to work this here script...'
  exit
fi

The main drawback is that this will also be triggered by arguments like request--help.log, --no--help, etc. (not just --help, which might be a requirement for your solution).

主要缺点是,这也将受到类似的论点被触发request--help.log--no--help等等(不只是--help,这可能是您的解决方案的要求)。

To apply this method in your case, you would write something like:

要在您的情况下应用此方法,您可以编写如下内容:

[[ "$@" =~ arg4 ]] && echo "Ahoy, arg4 sighted!"

Bonus!If your script requires at least one command line argument, you can similarly trigger a help message when no arguments are supplied:

奖金!如果您的脚本至少需要一个命令行参数,则您可以在未提供任何参数时类似地触发帮助消息:

if [[ "${@---help}" =~ --help ]]; then
  echo 'Ok first yer gonna need to find a file...'
  exit 1
fi

which uses the empty-variable-substitution syntax ${VAR-default}to hallucinate a --helpargument if absolutely no arguments were given.

如果绝对没有给出参数,它使用空变量替换语法${VAR-default}来产生幻觉--help

回答by Umae

maybe this can help.

也许这会有所帮助。

#!/bin/bash
# this is myscript.sh

[ `echo $* | grep arg4` ] && echo true || echo false