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
0 answers

How can I use getopts in bash and have it work with an extra flag?

I want to mock the verbose command in rm this is what I currently have while getopts ":ehrm" opt; do . case ${opt} in . e ) empty . ;; there is more, but it's just the other options such as h,r,and m. what I want to do is allow the user to do…
fsdff
  • 611
  • 4
  • 10
0
votes
1 answer

How to access multiple options for a command line flag in bash

I want to access multiple command line inputs for flags, but I can't get it to work. The input order is out of my control, with the format being (# are numbers, not comments) ./program.sh -a -b # # ./program.sh -b # # -a ./program.sh -b # # -a has…
Frogglet
  • 3
  • 3
0
votes
1 answer

Trying to save an argument as a variable in bash script

I am trying to save the argument after "-ip" as a variable in the bash script: if [ $# == 0 ]; then ARGS="" else for var in "$@" do ARGS="$ARGS $var" if [ $var == "-ip" ]; then …
Gerald
  • 83
  • 8
0
votes
1 answer

bash getopts loop not iterating

I have a simple loop i use in a lot of my bash scripts, yet this particular one doesn't seem to work. #!/bin/bash function main { echo here while getopts "Ah" cli_opt; do case ${cli_opt} in A) echo "op A" …
vesperto
  • 804
  • 1
  • 6
  • 26
0
votes
1 answer

calling different functions with getopts using bash

I am trying to work out how i can have multiple functions in one script and choose the function with an argument. what seems to be the issue is that if i choose a function the optarg does'nt seem to be run with the script. in this example I would…
m4lvo
  • 3
  • 1
0
votes
2 answers

Getopts: how to manager properly optional arguments?

I'm struggling with the following code. #!/bin/bash test-one () { if [[ ! -z $1 ]] ; then echo "You are in function test-one with arg $1" else echo "You are in function test-one with no args" fi } while getopts ":a:b:" opt; do …
EBAH
  • 99
  • 9
0
votes
1 answer

Detecting no option with getopt in C (in Linux)

I want to write a simple C program using terminal in Linux. I don't know how to check if no option was provided during program executing: ./program.a Here's my script: #include #include #include int main(int argc,…
BloodyMary
  • 173
  • 13
0
votes
1 answer

Simple argument parse in bash

I am new to bash. I will kindly ask for help from someone who can help me with this problem. I have created a script that makes a backup. Next step would be to make this script run at some certain timestamp in the future. I know I can do this with…
xbuffer
  • 1
  • 1
0
votes
1 answer

Order of getopts when no argument is passed to option

My problem is that when I use the snippets below the script chokes up the order when I do not give an argument to option. If I do include arguments all is well and I can enter options in any order. How can I ensure that the different options (-s and…
Andy Thompson
  • 284
  • 1
  • 3
  • 21
0
votes
1 answer

Can a shell script flag have optional arguments if parsing with getopts?

I have a script that I want to run in three ways: Without a flag -- ./script.sh With a flag but no parameter -- ./script.sh -u With a flag that takes a parameter -- ./script.sh -u username Is there a way to do this? After reading some guides…
Wimateeka
  • 2,474
  • 2
  • 16
  • 32
0
votes
0 answers

getopts malfunction in bash script

i have the following code: while getopts s:h:w:c:t:p option do case "${option}" in s) CUSTOM_COMMSTR=${OPTARG};; h) CUSTOM_HOSTADDRESS=${OPTARG};; w) CUSTOM_WARNING=${OPTARG};; …
RoyMWell
  • 199
  • 1
  • 9
0
votes
1 answer

get the values stored in OPTARG variable inside a shell script

I have a script that takes 3 parameters as input and then continues with the script.I am using getopts to check for parameters passed but i am not able to get the value of the passed parameters inside my script. Can anyone examine this code and…
noob_coder
  • 749
  • 4
  • 15
  • 35
0
votes
1 answer

Parsing options that take arguments in getopts

I have the following program foo, that can take one of three optional flags, f, g, or s: usage() { echo "Use this correctly" } while getopts "fgs" opt; do case $opt in f) echo f foo="$OPTARG" echo $foo …
Mike S
  • 1,451
  • 1
  • 16
  • 34
0
votes
1 answer

How to declare an option which doesn't allow any argument using getopts

I'm trying to understand how we can configure options for a ksh script using getopts and I have found below details on declaring options using getopts: Use : for options that require arguments. Use # for options that require numeric arguments. Use…
Krishna
  • 471
  • 2
  • 7
0
votes
2 answers

Combining multiple options into one single option (Getopts)

Due to my lack of thorough understanding using getopts, the title is definitely vague :0. I am currently writing a bash script and I would like to add an option that outputs the other options within the case statement in getopts. For the sake of…