是否可以将命令行参数从bourne脚本中传递到函数中,以便允许getopts处理它们.
我的其余部分很好地打包到函数中,但它开始看起来我必须将参数处理移动到主逻辑中.
以下是它现在的编写方式,但它不起作用:
processArgs()
{
while getopts j:f: arg
do
echo "${arg} -- ${OPTARG}"
case "${arg}" in
j) if [ -z "${filename}" ]; then
job_number=$OPTARG
else
echo "Filename ${filename} already set."
echo "Job number ${OPTARG} will be ignored.
fi;;
f) if [ -z "${job_number}" ]; then
filename=$OPTARG
else
echo "Job number ${job_number} already set."
echo "Filename ${OPTARG} will be ignored."
fi;;
esac
done
}
doStuff1
processArgs
doStuff2
是否有可能以一种可以读取脚本args的方式定义函数?这可以通过其他方式完成吗?我喜欢getopts的功能,但看起来在这种情况下,我将不得不牺牲代码之美来获得它.
我(大概每个人)都不时遇到这个问题,但我自己找不到任何好的解决方法。当getopts寻找一个参数时,即使它是一个选项,它实际上也会接受下一个参数。这是我为阻止该错误所做的事情(代码段):
#!/bin/bash
function optsGet()
{
while getopts ":c:f" opt; do
case $opt in
c ) [[ -z "${OPTARG}" || "${OPTARG}" == -* ]] \
&& { echo -e "ERROR: Invalid argument\n"; exit 1; }
CNAME="${OPTARG}.tEsTsTr"
;;
f ) FORCE=true
;;
\?) echo -e "Invalid option: -$OPTARG\n" >&2;;
: ) echo -e "Missing argument for -$OPTARG\n" >&2; exit 1;;
* ) echo -e "Unimplemented option: -$OPTARG\n" >&2; exit 1;;
esac
done
shift $(($OPTIND - 1))
}
optsGet "${@}"
echo …Run Code Online (Sandbox Code Playgroud) 可以使用getopts拾取全字标志吗?
如下所示:
while getopts ":abc --word" opt; do
case ${opt} in
a) SOMETHING
;;
...
--word) echo "Do something else."
;;
esac
done
Run Code Online (Sandbox Code Playgroud)
试图拿起那些双破折号。
我试图修改要使用的bd脚本getopts.我是bash脚本的新手
我的剧本是
while getopts ":hvis:d:" opt
do
...
done
...
echo $somedirpath
cd "$somedirpath"
Run Code Online (Sandbox Code Playgroud)
这样做很好
$ ./bd -v -i -s search
Run Code Online (Sandbox Code Playgroud)
要么
$ ./bd -is search -d dir
Run Code Online (Sandbox Code Playgroud)
但是当像这样运行时
$ . ./bd -s search
Run Code Online (Sandbox Code Playgroud)
getopts根本没有阅读论据.我while根据参数在循环中设置的所有变量都没有设置,因此脚本不再起作用.请帮忙!
这是我的脚本。我根据本教程对其进行了改编,因此它不会是脚本本身的错误。(原始脚本也有同样的问题。)
#!/bin/bash
while getopts "a:" opt; do
case $opt in
a)
echo "-a was triggered, Parameter: $OPTARG"
;;
esac
done
Run Code Online (Sandbox Code Playgroud)
这是我的输出:
bash-3.2$ source getopt.sh
bash-3.2$ source getopt.sh -a /dev/null
-a was triggered, Parameter: /dev/null
bash-3.2$ source getopt.sh -a /dev/null
bash-3.2$
Run Code Online (Sandbox Code Playgroud)
我已经梳理了互联网,找不到对这种行为的任何解释。
我在他们的网站上关注IBM的例子:
(清单#5)http://www.ibm.com/developerworks/library/l-bash-parameters/index.html
#!/bin/bash
echo "OPTIND starts at $OPTIND"
while getopts ":pq:" optname
do
case "$optname" in
"p")
echo "Option $optname is specified"
;;
"q")
echo "Option $optname has value $OPTARG"
;;
"?")
echo "Unknown option $OPTARG"
;;
":")
echo "No argument value for option $OPTARG"
;;
*)
# Should not occur
echo "Unknown error while processing options"
;;
esac
echo "OPTIND is now $OPTIND"
done
Run Code Online (Sandbox Code Playgroud)
我想要的是有一个名字超过1个字母的选项.即-pppp和-qqqq而不是-p和-q.
我写了我的程序并实现-help给了我一个问题......
我有从我的调用以下脚本.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${subdirs}
else
echo "${currdir} is not a workspace"
stgdir=""
fi
}
# Update local version with staging version
upd() {
__set_dirs
if [[ -n ${stgdir} ]]
then
__overwrite=0
while getopts "o" opt
do
case $opt in
o)
__overwrite=1
;;
\?)
echo "Invalid option: -$OPTARG" >&2
;;
esac
done
echo "Updating ${currdir} with latest from ${stgdir}..."
if [ ${__overwrite} …Run Code Online (Sandbox Code Playgroud) I know how to handle optional arguments using getopts in Bash
#!/bin/bash
while getopts ":a:p:drh" opt; do
case "$opt" in
a) echo $OPTARG;;
esac
done
Run Code Online (Sandbox Code Playgroud)
But if my script expects ./script.sh arg1 [options], how do I tell getopts to skip arg1?
$ ./script.sh -a 1234
1234
$ ./script.sh arg1 -a 1234
$ ./script.sh arg1
$ ./script.sh -a 1234 arg1
1234
Run Code Online (Sandbox Code Playgroud)
As I see I can handle the argument if the argument is put in the last position. What "regex" …
我正在使用标准 getopts 逻辑。但我想知道如何才能使我提供的选项相互排斥。例如
shell.sh -a SID
<accepted>
shell.sh -b SID
<accepted>
shell.sh -ab SID
Message- using ab together is the same as running shell.sh without any options supplying just SID . Help usage < ya da ya >
shell.sh
Please enter SID at the minimum. Usage < ya da ya >
shell.sh SID
<accepted>
Run Code Online (Sandbox Code Playgroud)
我正在尝试使用如下所示的方法来开发这个逻辑
while getopts ":a:b:" opt; do
case $opt in
a ) SID="$OPTARG";;
set var=1
b ) SID="$OPTARG";;
set var=2
\?) echo "Invalid option: -"$OPTARG"" >&2
exit …Run Code Online (Sandbox Code Playgroud) 我有以下代码:
while getopts ":p:t:n:" o; do
case "${o}" in
p)
p=${OPTARG}
numep=$p
mkdir $numep
;;
t)
t=${OPTARG}
tip=$t
if [ $tip == "c" ]; then
touch $numep/$numep.c
touch $numep/$numep.h
elif [ $tip == "c++" ]; then
touch $numep/$numep.cpp
touch $numep/$numep.h
fi
;;
n)
n=${OPTARG}
nrFis=$n
if [ $tip == "c" ]; then
for i in $(seq $n); do
touch $numep/src/$numep$i.c
touch $numep/inc/$numep$i.h
done
elif [ $tip == "c++" ]; then
for i in $(seq $n); do
touch $numep/src/$numep$i.cpp
touch $numep/inc/$numep$i.h …Run Code Online (Sandbox Code Playgroud)