This question already has answers here:
How do I parse command line arguments in Bash?
(40 answers)
Closed 2 years ago.
I have this shell script:
for i in "$#"
do
case $i in
-l=*|--ddloc=*)
DDLOC="${i#*=}"
shift # past argument=value
;;
*)
# unknown option
;;
esac
done
Its working fine as -x=y but i want to be like -x y. What changes will be required here?
for i in "$#"
do
case $i in
-l=*|--ddloc=*)
DDLOC="${i#*=}"
shift # past argument=value
;;
-l|--ddloc)
shift # past argument
DDLOC="$1"
shift # past value
;;
*)
# unknown option
;;
esac
done
Related
This question already has answers here:
Multiple option arguments using getopts (bash)
(4 answers)
Closed 1 year ago.
I am writing a script using getopt to parse parameters. The solution I have so far only accepts one parameter. Is there a way to make this solution accept multiple parameters (eg. both '-f and -l)?
solution in link does not work for me.
Bash getopt accepting multiple parameters
code:
'''
while getopts "f:l:" option; do
case "${option}" in
f) firstdate=${OPTARG}
shift
;;
l) lastdate=${OPTORG}
;;
*)
echo "UsageInfo"
exit 1
;;
esac
shift
done
'''
First, you have a typo: OPTORG should be OPTARG.
More importantly, you don't need the calls to shift. getopts takes care of consuming and skipping over each option and argument.
while getopts "f:l:" option; do
case "${option}" in
f) firstdate=${OPTARG} ;;
l) lastdate=${OPTARG} ;;
*)
echo "UsageInfo"
exit 1
;;
esac
done
This question already has an answer here:
Passing argument containing space in shell script
(1 answer)
Closed 2 years ago.
I'm a bash noob and can't figure out how to use a string containing spaces in a bash argument. Here's the function giving me problems...
handle_arguments() {
ARGUMENTS=($#);
for i in ${ARGUMENTS[#]}
do
INDEX=`expr index "$i" =`;
case $i in
--projectslug*)
PROJECT_SLUG=${i:INDEX};
;;
--sitename*)
SITE_NAME=${i:INDEX};
;;
--build*)
BUILD_TYPE=${i:INDEX};
;;
--dbname*)
DB_NAME=${i:INDEX};
;;
--mysqlun*)
MYSQL_UN=${i:INDEX};
;;
--mysqlpw*)
MYSQL_PW=${i:INDEX};
;;
--dbfilename*)
DB_FILE_NAME=${i:INDEX};
;;
--search*)
DB_SR_SEARCH=${i:INDEX};
;;
--replace*)
DB_SR_REPLACE=${i:INDEX};
;;
*)
echo -e "${RED}$i is not recognized as an argument.${NC}\n";
;;
esac
done;
if [[ -z $DB_NAME ]];
then
DB_NAME=$PROJECT_SLUG;
fi
}
The command I run is...
$ source wpautobuild.sh; wp_auto_build --projectslug=abctest1 --sitename="ABC TEST 1"
I've been troubleshooting this for hours and everything I'm finding is telling me to wrap $# in ARGUMENTS=($#); with double quotes so it's ARGUMENTS=("$#"); but that does not work.
I've also tried adding double quotes to SITE_NAME=${i:INDEX}; to be SITE_NAME="${i:INDEX}"; but also with no luck.
One article suggested escaping spaces in the command like this...
$ source wpautobuild.sh; wp_auto_build --projectslug=abctest1 --sitename="ABC\ TEST\ 1"
...and that did not work either.
Note: The handle_arguments() function is run in the wp_auto_build() function which is being called in the command.
I'm totally stuck and appreciate any help I can get. Thanks in advance.
You need to quote $# for it to preserve spaces. Unquoted $# serves no purpose, as it is identical to unquoted $*.
ARGUMENTS=("$#")
You subsequently need to quote ${ARGUMENTS[#]} for the same reason
for i in "${ARGUMENTS[#]}"
though there is no need for ARGUMENTS at all; you can iterate over "$#" directly.
for i in "$#"
This question already has answers here:
What is $opt variable for command line parameter in bash
(3 answers)
Closed 4 years ago.
Normally, script parameters are read from $1, $2, ...
Sometimes this is combined with shift and a while-loop and case-statement to process multiple parameters.
while [[ $# > 0 ]]; do
case "$1" in
-v|--verbose)
VERBOSE=1
;;
-d|--debug)
VERBOSE=1
DEBUG=1
;;
*) # unknown option
echo 1>&2 -e "${COLORED_ERROR} Unknown command line option '$key'.${ANSI_NOCOLOR}"
exit 1
;;
esac
shift # parsed argument or value
done
Today, I found a code snippet based on a simple for-loop:
#! /bin/bash
for opt; do
echo $opt
done
Execution:
$ ./test.sh foo bar spam
foo
bar
spam
Normally, one would see for i in ...; do.
Why/how can a simplified for-loop access script parameters?
Does it also work with parameters in functions?
From help for:
If in WORDS ...; is not present, then in "$#" is assumed.
Having a bit of trouble here.
I haven't had to do long options ever, so I am trying getopt rather than getopts.
For some reason it keeps stating shift as an unrecognized token.
Any reason why?
Also is this a proper implementation for getopt? Or is there a better method for this?
BASH SCRIPT BELOW:
FLAGS=$(getopt --long "help,user:" --name $PROGNAME -- "$#")
echo $FLAGS
eval set -- "$FLAGS"
while true; do
case $1 in
--help)
usage()
shift
;;
*)
shift
exit 1
;;
esac
shift
done
In Bash you don't call functions with brackets - usage() should instead be usage.
I'm trying to use getopts inside a switch case loop.
if i use only getopts or only the switch case it's work, however when i combine those two the getopts dos not trigger.
i have search a lot but i cat fins any mention for how to combine them, and problem i missing something stupid so for give me ...
here is the code essence.
#!/bin/bash
case $1 in
ver)
echo "vesion"
exit 0
;;
op)
while getopts ":a" opt; do
case $opt in
a)
echo "-a was triggered!" >&2
;;
\?)
echo "Invalid option: -$OPTARG" >&2
;;
esac
done
;;
esac
when i do that
# bash -x test.sh op -a
i get
+ case $1 in
+ getopts :a opt
(and without debug i get nothing)
what is that that i missing to combine these two
Thanks :)
You should add a shift instruction at the beginning of your op) choice, before the call to getopts, to eat the op argument itself. Else, the first argument that getopts will analyze is op and it will silently stop (end of options).