Linux 标准实用程序清单包括getopts,但不包括getopt。类似于Posix实用程序的开放群列表。
同时,维基百科的标准Unix命令列表包括getopt,但不包括getopts。类似地,(基于基于Debian的Ubuntu )也包括getopt,但不包括getopts (而且它是GNU增强版。
balter@spectre:~$ which getopt
/usr/bin/getopt
balter@spectre:~$ getopt -V
getopt from util-linux 2.27.1
balter@spectre:~$ which getopts
balter@spe
我有一个脚本myscript.sh
#!/bin/sh
echo $1 $2
就像..。
./myscript.sh foo bar
这给了我一个输出..。
foo bar
但是如何使这个脚本包含自定义命令选项呢?例如..。
./myscript.sh -a foo and corespond to $1
和
./myscript.sh -b bar and corespond to $2
我正在尝试使用bash中的getopts处理命令行参数。其中一个需求是处理任意数量的选项参数(不使用引号)。
第一个示例(仅获取第一个参数)
madcap:~/projects$ ./getoptz.sh -s a b c
-s was triggered
Argument: a
第二个示例(我希望它的行为像这样,但不需要引用参数“
madcap:~/projects$ ./getoptz.sh -s "a b c"
-s was triggered
Argument: a b c
有没有办法做到这一点?
下面是我现在拥有的代码:
#!/bin/bash
while getop
我在最新的CentOS 7 VM中使用以下Bash版本:
GNU bash, version 4.2.46(2)-release (x86_64-redhat-linux-gnu)
以下代码按预期执行(注意到-x -y):
set -- -x -y; OPTIND=1; while getopts xy opt; do echo $opt; OPTIND=$OPTIND; done
x
y
但是,当我将两个短选项组合到-xy时,会出现无限循环:
set -- -xy; OPTIND=1; while getopts xy opt; do echo $opt; OPTIND=$OPTIND; d
我使用getopts将参数传递给shell脚本。
例如,我想在脚本中的两个部分上进行操作,这两个部分都以相同的字母开头。我也不想选择任何其他的字母。例如,我有两个项目: project 1和project2,它们都以字母p开头。
有没有办法,我可以提供完整的字符串,如--project1等。或者有任何其他解决方案?
while getopts "project1 project2" opt;
# This ofcourse would not work. So, whats the solution?
是否有getopts的替代方案
如果有人澄清了在这个简单的解析器代码中使用shift的必要性,我会很高兴:
while getopts ":hp:" option
do
case "${option}" in
p)
some_parameter=${OPTARG}
;;
h)
print_usage_and_exit 0
;;
*)
print_usage_and_exit 1
;;
es
我一直在尝试使用bash脚本进行一些练习,但我在使用$1变量来引用脚本的第一个参数时遇到了困难。它是一个简单的脚本,它以文件为参数并打印文件名。这是我的剧本:
#!/bin/bash
function practice() {
echo "${1}"
}
while getopts "h:" opt; do
case "$opt" in
h) practice
;;
esac
done
exit 0
我尝试了以下命令:
./practice.sh -h somefile.txt
由于某种原因,它返回一个空行。有什么
假设我使用选项字符串":a:b“在Bash脚本中运行getopts,并提供了以下命令行参数:
./script.sh -a foo bar -b bar zappo
"foo“的实例是选项a的预期参数,而选项b没有预期参数。但是,"bar“的实例和"zappo”的实例对于getopts都是不可接受的。在运行getopts之后,如何回显一个包含所有不可接受参数列表的变量?我怎样才能生成"bar bar zappo“这个列表?谢谢!
为了方便起见,下面是一些代码:
#!/bin.bash
option_string=":a:b"
unexpe
我试图制作一个可执行文件来搜索特定的文本。我希望检索数据文件中包含传递它的值的任何行。所以我创建了一个名为.addr_book的数据文件,并创建了一个名为lookup.ksh的可执行文件。
lookuo.ksh:
#!/bin/ksh
while getopts ":a:" option;
do
case $option in
a)
echo searching for $OPTARG
cat .addr_book | grep -o '$OPTARG.*'
;;
*)
echo "invalid option -$
我刚开始使用bash,并在其中完成了我的第一步,我想学习如何在脚本中本地存储参数供以后使用,并编写了以下脚本
#!/bin/bash
while getopts ":d" opt;do
case "${opt}" in
d)
d =$OPTARG
echo "-d was triggered ! storing ${d}" >&2
;;
esac
done
这是我的输出:
$ -d was triggered! storing
我希望这些投入都能发挥作用。也就是说,-n选项本身是可选的--我已经知道如何做了--但是它的顶部可能有一个可选的参数。如果不提供参数,则将应用回退值。
command -n 100
command -n
我只能使前者的输入类型工作或后者,但不能两者兼而有之。
HAS_NICE_THINGS=0
NICE_THINGS=50 # default value.
while getopts n: option; do
#while getopts n option; do # NICE_THINGS would always be that default value.
#whi
我有一个以getopts开头的脚本,如下所示:
USAGE() { echo -e "Usage: bash $0 [-w ] [-o ] [-c ] [-t ] \n" 1>&2; exit 1; }
if (($# == 0))
then
USAGE
fi
while getopts ":w:o:c:t:h" opt
do
case $opt in
w ) BIGWIGS=$OPTARG
;;
o ) OUTDIR=$OPTARG
;;
c )
我有一系列的BASH脚本。
我正在使用getopts来解析cmd行中的参数(尽管对替代项是开放的)。
这些脚本有一系列常见的选项,称为“选项集”、“队列”、“核心”等。
然后,每个脚本都有一系列额外的选项,即设置B1、B2、B3。
我想要的是脚本
"1 to be able to take options A+B1"
"2 to be able to take options A+B2"
"3 to be able to take options A+B2"
但我希望能够将选项A的代码存储在一个中心位置(库/函数),并且必须在每个脚本中写出
我的程序接受命令行参数。
test.pl --help
我想在代码中启用"help“选项。如何在代码中打印用法?
#!/usr/bin/perl
use strict;
use warnings;
use Cwd 'chdir';
use Getopt::Long;
my ($application,$version, $location,$artifactid,$pomversion,$groupid);
GetOptions('application=s' => \$application,
'v
我有以下脚本(示例):
#!/bin/bash
while getopts a: opt; do
case "$opt" in
a) val="$OPTARG";;
?) echo "use the flag \"-a\""
exit 2;;
esac
done
echo "a specified with: ${val}"
当我现在用test.sh -a "here is a string"调用这个脚本时,输出是:a spe
我有一个脚本,它应该作为这两个脚本中的任何一个运行:
script.sh -t TYPE
script.sh -t TYPE -f FILE
如果在没有-t标志的情况下运行它,我希望它出错并退出。
如果它是使用-t标志运行的,我希望获取该值并将其存储在一个名为"$TYPE“的变量中,并打印”$TYPE“。
如果它使用-f标志运行,我希望它获取该值并将其存储在一个名为"$FILE“的变量中,然后打印"$TYPE和$FILE”
从这里和互联网上的信息和教程来看,这是我能得到的最接近的信息。有人能帮我把第二个条件放到这个现有的代码中吗?
while get
我有几个剧本可以互相联系。然而,当我经过
来自buid和run-node.sh的片段
OPTIND=1 # Reset getopts in case it was changed in a previous run
while getopts "hn:c:f:s:" opt; do
case "$opt" in
h)
usage
exit 1
;;
n)
container_name=$OPTARG
;