如何编写带有可选输入参数的 bash 脚本?

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

How to write a bash script that takes optional input arguments?

bashargumentsparameter-passing

提问by Abe

I want my script to be able to take an optional input,

我希望我的脚本能够接受一个可选的输入,

e.g. currently my script is

例如目前我的脚本是

#!/bin/bash
somecommand foo

but I would like it to say:

但我想说:

#!/bin/bash
somecommand  [ if  exists, , else, foo ]

回答by Itay Perl

You could use the default-value syntax:

您可以使用默认值语法:

somecommand ${1:-foo}

The above will, as described in Bash Reference Manual - 3.5.3 Shell Parameter Expansion[emphasis mine]:

以上将,如Bash 参考手册 - 3.5.3 Shell Parameter Expansion[强调我的] 中所述:

If parameter is unset or null, the expansion of word is substituted. Otherwise, the value of parameter is substituted.

如果参数未设置或为 null,则替换单词的扩展。否则,替换参数的值。

If you only want to substitute a default value if the parameter is unset (but not if it's null, e.g. not if it's an empty string), use this syntax instead:

如果您只想在未设置参数的情况下替换默认值(但如果它为空,则不是,例如,如果它是空字符串,则不是),请改用以下语法:

somecommand ${1-foo}

Again from Bash Reference Manual - 3.5.3 Shell Parameter Expansion:

再次来自Bash 参考手册 - 3.5.3 Shell 参数扩展

Omitting the colon results in a test only for a parameter that is unset. Put another way, if the colon is included, the operator tests for both parameter's existence and that its value is not null; if the colon is omitted, the operator tests only for existence.

省略冒号会导致仅对未设置的参数进行测试。换句话说,如果包含冒号,则运算符会测试两个参数是否存在,并且其值不为空;如果省略冒号,则运算符仅测试是否存在。

回答by Brad Parks

You can set a default value for a variable like so:

您可以为变量设置默认值,如下所示:

somecommand.sh

一些命令.sh

#!/usr/bin/env bash

ARG1=${1:-foo}
ARG2=${2:-bar}
ARG3=${3:-1}
ARG4=${4:-$(date)}

echo "$ARG1"
echo "$ARG2"
echo "$ARG3"
echo "$ARG4"

Here are some examples of how this works:

以下是有关其工作原理的一些示例:

$ ./somecommand.sh
foo
bar
1
Thu Mar 29 10:03:20 ADT 2018

$ ./somecommand.sh ez
ez
bar
1
Thu Mar 29 10:03:40 ADT 2018

$ ./somecommand.sh able was i
able
was
i
Thu Mar 29 10:03:54 ADT 2018

$ ./somecommand.sh "able was i"
able was i
bar
1
Thu Mar 29 10:04:01 ADT 2018

$ ./somecommand.sh "able was i" super
able was i
super
1
Thu Mar 29 10:04:10 ADT 2018

$ ./somecommand.sh "" "super duper"
foo
super duper
1
Thu Mar 29 10:05:04 ADT 2018

$ ./somecommand.sh "" "super duper" hi you
foo
super duper
hi
you

回答by Irit Katriel

if [ ! -z  ] 
then 
    : #  was given
else
    : #  was not given
fi

回答by Dennis

You can check the number of arguments with $#

您可以检查参数的数量 $#

#!/bin/bash
if [ $# -ge 1 ]
then
    
else
    foo
fi

回答by hagen

please don't forget, if its variable $1 .. $n you need write to a regular variable to use the substitution

请不要忘记,如果它的变量 $1 .. $n 你需要写入一个常规变量来使用替换

#!/bin/bash
NOW=
echo  ${NOW:-$(date +"%Y-%m-%d")}

回答by Jesse Glick

For optional multiplearguments, by analogy with the lscommand which can take one or more files or by default lists everything in the current directory:

对于可选的多个参数,通过类比ls可以接受一个或多个文件的命令,或者默认列出当前目录中的所有内容:

if [ $# -ge 1 ]
then
    files="$@"
else
    files=*
fi
for f in $files
do
    echo "found $f"
done

Does not work correctly for files with spaces in the path, alas. Have not figured out how to make that work yet.

对于路径中有空格的文件不能正常工作,唉。还没有想出如何使这项工作。

回答by mosh

This allows default value for optional 1st arg, and preserves multiple args.

这允许可选的第一个参数的默认值,并保留多个参数。

 > cat mosh.sh
   set -- ${1:-xyz} ${@:2:$#} ; echo $*    
 > mosh.sh
   xyz
 > mosh.sh  1 2 3
   1 2 3 

回答by Garren S

It's possible to use variable substitution to substitute a fixed value or a command (like date) for an argument. The answers so far have focused on fixed values, but this is what I used to make date an optional argument:

可以使用变量替换来替换固定值或命令(如date)来代替参数。到目前为止,答案都集中在固定值上,但这是我用来将日期作为可选参数的原因:

~$ sh co.sh
2017-01-05

~$ sh co.sh 2017-01-04
2017-01-04

~$ cat co.sh

DAY=${1:-$(date +%F -d "yesterday")}
echo $DAY