我正在编写一个bash脚本,它依赖于通过参数提供的许多值,并提供可选(布尔)标志。 基于this page上的示例,我使用-h选项将脚本扩展为以下内容: human=false
while getopts u:a:f:h: flag
do
case "${flag}" in
u) username=${OPTARG};;
a) age=${OPTARG};;
f) fullname=${OPTARG};;
h) human=true;;
esac
done
if $human; then
所以我有一个关于在bash中获取选项的问题。我想要获取参数的值,如果它们存在,但如果它们不存在,则使用默认值。所以脚本应该接受一个目录和一个整数,但是如果没有指定它们,那么$PWD和3应该是默认值。以下是内容
while getopts "hd:l:" opt; do
case $opt in
d ) directory=$OPTARG;;
l ) depth=$OPTARG;;
h ) usage
exit 0;;
\? ) usage
exit 1;;
esac
我正在尝试扩展源代码脚本中的getopts,如下所示。如果传入a、b、c、d以外的选项,则需要打印"invalid option“并退出。 script_a.sh: #!/bin/bash
script_a_getopts() {
while getopts 'a:b:' OPT; do
case "$OPT" in
a)
a=$OPTARG
;;
b)
b=$OPTARG
;;
*)
我想在我的.bash_profile中定义的函数中使用getopts。我的想法是,我想把一些标志传递给这个函数来改变它的行为。
代码如下:
function t() {
echo $*
getopts "a:" OPTION
echo $OPTION
echo $OPTARG
}
当我像这样调用它时:
t -a bc
我得到以下输出:
-a bc
?
怎么了?我希望在不手动移动和解析的情况下获得值bc。如何在函数中正确使用getopts?
编辑:更正了我的代码片段以尝试$OPTARG,但无济于事
编辑#2:好吧,原来代码没问题,我的shell不知
我有一个脚本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
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
我有以下bash脚本:
#!/bin/bash
function usage() {
printf "\n"
echo "Updates a Lambda with env vars stored in a Secrets Manager secret. Complete drop n' swap."
printf "\n"
echo "Syntax: bash $0 -a <ARN> -s <secretName> [-h]"
echo "options:"
假设我使用选项字符串":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
我正在尝试使用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
我正在尝试编写一个简单的bash脚本,执行如下
$ ./export.sh -n <my-file-name> -a <my-api-key>
我使用它作为在构建时在Go项目中传递一些参数的方法。
脚本的一个非常简单的版本是:
#!/bin/bash
while getopts n:a option
do
case "${option}"
in
n) FILENAME=${OPTARG};;
a) APIKEY=${OPTARG};;
esac
done
if [ -z "$FILENAME" ]
then
FILENAME=d
我编写了这个小bash脚本,用于通过读取文件中的行在特定目录中创建文件。但是问题是mkdir没有创建dir,不知道为什么,它工作了,我在脚本之外尝试了一下。下面是我的脚本...
#!/bin/bash -x
source credentials.sh
OPTARG=""
while getopts :i:x:n name
do
case $name in
x) inputfile="$OPTARG" ;;
i) outputPath="$OPTARGS" ;;
n) dirName=
由于我对getopts缺乏透彻的理解,所以标题肯定是模糊的:0。我目前正在编写一个bash脚本,我想添加一个选项来输出getopts中case语句中的其他选项。为了便于扩展,我缩短了程序。
#!/bin/bash
while getopts :abc opt
do
case $opt in
a)
echo "Hello"
;;
b)
echo "Goodbye"
c)
:ab #****I WANT -
我试图在bash中使用getopts来解析命令行参数,但是如果没有匹配参数(或者没有给定cmdline参数),我不知道如何实现“默认”操作。
这是我到目前为止尝试过的愚蠢的简化版本:
while getopts v:t:r:s:c: name;
do
case $name in
v) VALIDATE=1;;
t) TEST=1;;
r) REPORT=1;;
s) SYNC=1;;
c) CLEAR=1;;
*) print_help; exit 2;;
\?) p
#!/bin/bash
#if present -a flag then print this echo
#echo "A";
#if -b is present then print b
#echo "B"
#and if -c 10 present how can I read this value '10' ?
上面是我想要的脚本外观
我希望能够像这样开始
myscript.sh -a -b -c 10
或
myscript.sh
或
myscript.sh -a -c 10
或
myscript.sh -c 10
诸若此类
我正在编写bash脚本来调用curl命令:
#!/bin/bash
while n flag
do
case "${flag}" in
n) name=${OPTARG};;
esac
done
curl -XPOST http://localhost:2468/api/webhook -H 'Content-Type: application/json' --data '{"name":"'"$name"'"}'
但是,这不是解析变量并发送空字符串
我在OSX上,我需要一个处理多个参数的bash脚本。基于这个网站上的一些答案,我编写了以下bash脚本(为了隐私起见,删除了一些字符串):
#!/bin/bash
# A POSIX variable
OPTIND=1 # Reset in case getopts has been used previously in the shell.
# Default parameter value:
PORT=xxx
PUBKEY=xxx
HOST_IP=xxx
# show_help function
show_help() {
echo "Usage: $
我有三个单独的bash脚本,它们根据另一个脚本中的特定条件发送电子邮件。我想要做的是将所有这些都合并到一个脚本中,并使它们成为函数,并在主脚本中使用这些函数,但是使用标志来调用正确的函数。所以我有这个密码
main()
{
while getopts "f:s:k:" opt; do
case ${opt} in
f) sendFirstEmail
;;
s) sendSecondEmail
;;
k) sendKillEmail
当我试图像这样执行bash脚本时:
bin/provision-pulsar-commands -t development -n provisioning
输出打印:
租户=
命名空间=
这是我的脚本(供应-脉冲星-命令):
#!/bin/bash
function display_usage {
echo "You may override the namespace and tenant for all components for testing, if desired."
echo "usage: bin/provision-pul
我的代码是这样的
#!/bin/bash
for i in $1 $2 $3 $4; do
if [[("$i" == '-f')]] ; then #I'm searching if the user input the -f switch
if [ -f "$((i+1))" ]; then # I'm trying to increment the position of $i to get the input that follows the -f switch and check the existence of
我一直在尝试使用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
由于某种原因,它返回一个空行。有什么
我有从我的.bash_profile调用的以下脚本
# Set directories based on current path
__set_dirs() {
currdir=`pwd`
if [[ ${currdir} =~ "\/path\/to\/main\/(.*)\/working\/([a-z]+)(/?.*)" ]]
then
ws=${BASH_REMATCH[1]}
subdirs=${BASH_REMATCH[3]}
stgdir=${ts}/${ws}/STAGING${subdir
在bash脚本中,我希望从配置文件加载设置,并使用命令行选项覆盖各个设置。如果在配置文件和命令行中都指定了设置,则应优先使用命令行设置。
如何确保在其他getopts块之前加载配置文件?下面是我得到的信息:
#!/bin/bash
# ...
while getopts “c:l:o:b:dehruwx” OPTION
do
case $OPTION in
c)
echo "load"
CONFIG_FILE=$OPTARG
# load_config is a function that sourc
我想做一个像makefile一样工作的bash脚本。它将有像-archive、-clean、-backup等选项。唯一的要求是它必须有-o参数,以便它指定一个名称。我现在遇到的问题是,我不知道如何从参数中取出.c文件。
例如,如果我输入./compile.sh -o name -backup hello_world.c print.c
我该如何编译它呢?
这是我到目前为止拥有的代码。
#!/usr/local/bin/bash
if [ $1 != '-o' ]; then
echo "ERROR -o wasn't present as first argu
我不擅长Bash脚本编写,并试图学习更多。让我用代码介绍我的问题:
#!/bin/bash
version_num=
isWindows=
MSVC_VER=
VERBOSE=
while getopts “hw:v:m:V” OPTION
do
case $OPTION in
h)
usage
exit 1
;;
w)
isWindows=$OPTARG
;;
v)
vers
我有一系列的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的代码存储在一个中心位置(库/函数),并且必须在每个脚本中写出
我在最新的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
我试图用以下用法制作一个脚本:
bash script.sh [-a] databases ...
然后,脚本使用来自“数据库”的参数填充数组$databases。
databases=("$@")
现在,我正在尝试实现选项-a,如果设置该选项,则应该声明att=1
while getopts "a" o; do
case "${o}" in
a)
att=1
;;
esac
done
现在我面临的问题是,在执行脚本时,-a也会被放入数组中。
我不能将read用于数组