ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I got a bash script and I need to add two flags for that. I have already achieved the same with one flag using shift command but how do I do the same thing for 2 flags.
I got a bash script and I need to add two flags for that. I have already achieved the same with one flag using shift command but how do I do the same thing for 2 flags.
Yes, it will help to see your script to better understand what you want to do. Have you considered using getopts? It's intended for parsing command line options. Takes a little understanding but very sweet when you've got it.
for opt in $@
case $opt in
-i) input_file=$2 ; shift 2 ;; # reading $2 grabs the *next* fragment
-o) output_file=$2 ; shift 2 ;; # shift 2 to get past both the -o and the next
esac
Or, if you want to use syntax like -i=input_file
Code:
for opt in $@
case $opt in
-i=) input_file=${opt:3} ; shift 1 ;;
-o=) output_file=${opt:3} ; shift 1 ;;
esac
There you are, this is kinda i wanna achieve it. But what's happening here is while using conventional method I was not able to shift parameter which comes after $4. which should shift to $2.
Here is the thing but its not complete at least you will get a fair idea about it.
if [ "$1" = "-i" ];then
"$3"="-0"
shift 2
fi
if [ "$4" = "-o" ];then
shift 1
fi
RUN #----- RUN is my function
done
I am doing something wrong here but not able to figure that out what that is.
When you shift, the option locations change. If you have 4 arguments $1, $2, $3 and $4, when you shift 1, then $2 becomes 1$, $3 becomes 2 etc.
And this is correct syntax for anything:
"$3"="-0"
Put some echo statements in there so you can see what is happening:
Code:
#!/bin/bash
for opt in $@
case $opt in
-i=) input_file=${opt:3} ; echo $1 ; shift 1 ; echo $1 ;;
-o=) output_file=${opt:3} ; echo $1 ; shift 1 echo $1 ; ;;
esac
done
Why roll your own when there's a tool for the job. Something like this should work (not tested)
Code:
lf=$'\n'
while getopts i:o: opt 2>/dev/null
do
case $opt in
i)
in_fn="$OPTARG"
;;
o)
out_fn="$OPTARG"
;;
* )
emsg="${lf}Invalid option '$opt'"
esac
done
# Test for extra arguments
# ~~~~~~~~~~~~~~~~~~~~~~~~
shift $(( $OPTIND-1 ))
if [[ $* != '' ]]; then
emsg="${lf}Invalid extra argument(s) '$*'"
fi
# Test for mandatory options not set
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if [[ $in_fn = '' ]]; then
emsg="${lf}Mandatory option -i not given"
fi
if [[ $out_fn = '' ]]; then
emsg="${lf}Mandatory option -o not given"
fi
# Report any errors
# ~~~~~~~~~~~~~~~~~
if [[ $emsg != '' ]]; then
echo "$emsg" >&2
usage # a function that prints the usage, can usefully be called with option -h
\exit 1
fi
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.