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

I am running a korn script (ksh) on a Cent Os 7 bash shell. The "getopts" is not working as expected. What am I doing wrong?

The problem script testcmd.ksh #!/usr/bin/ksh while getopts "d: m s z a b " opt; do echo $opt case $opt in d ) echo "d" ;; m ) echo "mail";; s ) echo "snmp";; z ) echo "force:";; a ) echo "fs";; …
CornerH
  • 11
  • 4
0
votes
0 answers

Bash script terminates after loop

I am experimenting with getopts in bash. I am adding the following snippet as an example. script-setup.sh set -e ... package_version="v1.1.0" grafana_username="" grafana_password="" subcommand=$1; shift while [ ! -z "$subcommand" ]; do case…
srajappa
  • 484
  • 8
  • 19
0
votes
1 answer

Handle Bash getopts option with one of other options

My current Bash script looks like in the following. So far it's working except the option -g. I'd like this option to be optional but it cannot be used without any of -c or -n. So what I mean is: -g shall be completely optional If, however, it is…
Robert Strauch
  • 12,055
  • 24
  • 120
  • 192
0
votes
1 answer

How do I get argparse to show one help page for a python script with two names?

My current working python script uses getopts, as it is a rewrite of a perl script that used Getopt::Long. I'd like to migrate to argparse, but failed so far as the options do not fit nicely into the way argparse works. I then had the bright idea…
0
votes
0 answers

Passing long and short CLI options with & without space

I wish to pass long and short CLI options. I have tried below for short options: #!/bin/bash while getopts d:t:r: option do case "${option}" in d) c_date=${OPTARG};; t) c_type=${OPTARG};; r) c_date_range=${OPTARG};; esac done shift $((OPTIND…
S R
  • 657
  • 3
  • 10
  • 21
0
votes
1 answer

Optional GETOPTs argument leading to empty variable/argument not passed to variable

I have the following bash script: while getopts g:s:l:o:t:f:rbh option do case "${option}" in g) genome=${OPTARG};; s) species=${OPTARG};; l) RepSub=${OPTARG};; o)…
toby_b
  • 1
  • 3
0
votes
3 answers

how to call a function with 2 arguments which under the option of "getopts"

New in Linux bash script. Here I tried to create some files with getopts. For example I'd like to create 3 files called xyzfile, in command line ./createfiles -n xyzfile 3should be given (2 arguments after the option -n). The result should be 3…
cchi
  • 71
  • 6
0
votes
1 answer

Problem in passing command line arguments using getopts

I am trying to pass commandline arguments to my workflow script using getopts but the code throws an error Following is the snippet of code for passing command line arguments # take strings as arguments. while getopts "TDNUW:" opt; do case "$opt"…
DevanDev
  • 161
  • 1
  • 2
  • 17
0
votes
0 answers

getopts not parsing args properly

Here is my short script: #!/bin/bash user="" secret="" pkgname="" while getopts ":p:s:u:" opt; do case ${opt} in p ) pkgname=$OPTARG echo "p=$pkgname" ;; s ) secret=$OPTARG echo "s=$secret" …
aaa
  • 415
  • 6
  • 15
0
votes
1 answer

How can I take multiple arguments in bash using getopts?

I am using getopts for the first time. And I am trying to accept 2 arguments: startyear and endyear, based on which the script will continue doing a lot of calculations. But I am not able to make this work. I am getting blanks for echo variables.…
maximusdooku
  • 5,242
  • 10
  • 54
  • 94
0
votes
1 answer

It is possible to mix options and arguments?

Is it possible to mix options (with getopts) and arguments ($1....$10)?
Alex
  • 1
  • 1
0
votes
2 answers

How to pass the command via getopts argument and execute it?

I'm trying to make some application but it's too difficult to describe it here so I simplify my problem into simpler ones. I just want to make a script that takes two arguments: time and command. It parses arguments with getopts, waits some time and…
J Kluseczka
  • 1,150
  • 9
  • 14
0
votes
0 answers

getopts bash script accepting unrecognized flags

I cannot seem to figure out why my simple bash script is parsing not declared flags. Here is how it looks: #!/bin/bash while getopts "hi" OPTIONS do case $OPTIONS in h) echo "usage:" …
mailman
  • 195
  • 1
  • 1
  • 8
0
votes
0 answers

How do I port getopts logic from bash to cshell?

Background In bash I have a working getopts interface as depicted below: while getopts "a:b:c:d:" OPTION ; do case "$OPTION" in a) JET="$OPTARG" ;; b) FUEL="$OPTARG" ;; c) CAN="$OPTARG" ;; d) MELT="$OPTARG" ;; …
isakbob
  • 1,439
  • 2
  • 17
  • 39
0
votes
0 answers

Bash getopt check parameter for every single user in system

Ive done a script which works as follows bash users.sh -u "user" [-f | -d | -g] -f , shows the number of files of the specified user directory. -d, shows the number of folders of the specified user directory. -g , shows the groups to which the…