Questions tagged [getopts]

getopts is a Bourne/POSIX shell builtin for parsing command-line options, available in ash, bash, dash, ksh, zsh, ... on Linux and other Unix systems.

getopts is a shell builtin for parsing command-line options of the form -a where a is a letter or other character. It is available in all Bourne-style shells (Bourne, ash, bash, dash, ksh, zsh, ...) and defined by the POSIX standard.

Using getopts usually involves using a loop calling getopts OPTSTRING varname, in which each call to getopts will parse the next option, set varname with the option name and return success.

Example

#!/bin/sh

while getopts a:b flag
do
    case $flag in
        a) echo "a flag used, with value $OPTARG" ;;
        b) echo "b flag used" ;;
        ?) echo "Usage: PROGRAM [-a ARG1] [-b]"; exit 1 ;;
    esac
done
shift $((OPTIND - 1)) # remove parsed args from the arglist
echo remaining args: $*

Documentation

444 questions
0
votes
1 answer

bash getopts options for different functions

how can I make this work? I want to use different functions for my command, my problem is how can I pass arguments to the add.sh function? find.sh works fine but the first two commands says no arg for -v / -a option. What am I doing wrong? while…
0
votes
0 answers

How to transform if elif to getopts

i've been working on transforming if elif statement to getopts for sometime but i can't seems to get it. I would like to run getopts statement without inserting any options. if [ $? eq 0 ]; then echo "username exist" exit 1 elif grep $uid …
0
votes
1 answer

Iterate twice on bash scripts arguments using getoptex

I'm writing a bash script that needs long arguments, like --argument and not only letters like -a. There is a function called getoptex which can be found here that can handle this. The thing is, my project is very specific as it is modular. A single…
Antonin Décimo
  • 499
  • 3
  • 17
0
votes
1 answer

How to determine if the correct arguments were passed in getopts?

I have a bash script that contains the following: while getopts ":a:1:2:3:4" arg; do case "$arg" in a) a=$OPTARG ;; 1) one=$OPTARG ;; 2) two=$OPTARG ;; 3) three=$OPTARG ;; 4) four=$OPTARG ;; There are two…
Lucas Alanis
  • 1,208
  • 3
  • 15
  • 30
0
votes
1 answer

Korn shell getopts syntax error

I am trying to write a Korn shell script that uses getopts to take command line options. The example given in the book I am using gives the description of using getopts with this: #!/bin/ksh a="not selected" b="not selected" b_arg="not…
Chris
  • 78
  • 1
  • 9
0
votes
1 answer

Using getopts in bash to get optional input argument

I am using getopts to process the input arguments. I have problem in reading optional argument value. When I invoke the script with arguments test.sh -t test -r server -p password -v 1 $OPTARG is not returning the value of the optional argument…
Tech Tech
  • 141
  • 2
  • 10
0
votes
2 answers

BASH - getopts not working properly

I'm currently having problems with my script. Basically, what I want to happen is when I execute ./apache_new_vhost.sh -a -d google.com, it will create a file and directories and if I use the -r option, it should delete. The script was able to use…
devxvda1
  • 442
  • 2
  • 8
  • 18
0
votes
1 answer

How to quote bash flag arguments to pass through one getopts call and be interpreted by a second?

Script nerf calls script herd, which calls script er. nerf uses a flag on herd that explicitly takes arguments needing to be passed to er. This was not a problem before nerf existed - when herd was just called from the command line, we could…
0
votes
2 answers

Bash interprets too long arguments for an option

I'm writing a bash script, that can take three options: c, p, and m; c can't have arguments, but p and m must have arguments. In my script I have these lines of code: while getopts ":cp:m:" opt do case $opt in c ) capitals=1 ;; …
Mathiasss
  • 11
  • 1
0
votes
1 answer

bash: getopts: parse two options with arguments

I am trying to parse two options which both need an argument. #!/bin/bash while getopts "a:b:" opt; do case $opt in a) echo "a has the argument $OPTARG." shift ;; b) echo "b has the argument $OPTARG." …
Monkey Supersonic
  • 1,165
  • 1
  • 10
  • 19
0
votes
1 answer

simple getopts c problems

So I have a pretty simple program but for some reason I can't get the options right. If the -h option is present I just want to print the usage statement and exit. If no options are present I want it to just run normally. If any other options are…
Greg
  • 737
  • 4
  • 13
  • 21
0
votes
1 answer

bash scripts hyphen operator and square brackets

Please could someone tell me what is the hyphen operator in below scripts? [ "${MYDATA_OPT-}" ] && set "$MYDATA_OPT" "$@" data=${MYDATA_VER-1} Is there some quick documentation of these operators? I am also trying to understand below scripts shift…
NewQueries
  • 4,841
  • 11
  • 33
  • 39
0
votes
0 answers

Bash script and getopts outputting saved variables

I'm trying to put together a script to monitor files and im having a hard tiem with getopts. If I run more than once the shell values dont changes... There's a ton of clean up but I've been stuck on this one issue for a couple hours so…
crownedzero
  • 496
  • 1
  • 7
  • 18
0
votes
2 answers

Access second argument in getopts option using bash

Already asked question related this few days ago here But this time the condition different,Having following bash script using getopts #!/bin/bash ipaddr="" sysip="" msgType="" sshTimeout="" bulbIndex="" bulbstate="" while getopts ":ht:d:A:r:s:m:"…
Jayesh Bhoi
  • 24,694
  • 15
  • 58
  • 73
0
votes
1 answer

RHEL6 getopts doesn't seem to be working

I have a new RHEL6 machine and I'm trying to run a script to generate some output. The script uses getopts which I've never used in the past. This should have worked on other machines but is my first time trying it. Below is the beginning of the…
djdick
  • 189
  • 1
  • 2
  • 12