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
5
votes
3 answers

How to ignore invalid arguments with getopts and continue parsing?

I've the following simple code: #!/usr/bin/env bash while getopts :f arg; do case $arg in f) echo Option $arg specified. ;; *) echo Unknown option: $OPTARG. ;; esac done and it works in simple scenarios such as: $ ./test.sh -f Option f…
kenorb
  • 155,785
  • 88
  • 678
  • 743
5
votes
3 answers

getopts - if a flag is empty getopts use the next flag

I'm trying to use getopts for a bash script. This script can have flags and all of those flags are mandatory and need to contain a value. When one of the mandatory flags that supposed to contain a value is empty getopts use the next-in-line flag as…
Gili Lapid
  • 53
  • 2
  • 6
4
votes
4 answers

getopts printing help when no cmd. line argument was matched

I'm trying to use getopts in bash to parse command line arguments, but I couldn't figure out how to implement "default" action, if no argument was matched (or no cmdline argument given). This is silghtly simplified version of what I've tried so…
Tomas Pruzina
  • 8,397
  • 6
  • 26
  • 39
4
votes
2 answers

Using getopt_long (C++) how do I code up a long & short option to both require arguments?

#include #include #define no_argument 0 #define required_argument 1 #define optional_argument 2 int main(int argc, char * argv[]) { std::cout << "Hello" << std::endl; const struct option longopts[] = { …
stackoverflow
  • 18,348
  • 50
  • 129
  • 196
4
votes
1 answer

What is the meaning of "-" in getopts template?

What is the meaning of the following template in getopts? while getopts ':x:y-:' val; I know that it expects two options -x or -y but what is the meaning of the symbol - at the end of the template ?
4
votes
2 answers

How do I separate the first argument from that of getopts?

#!/bin/bash priority=false it=0 dir=/ while getopts "p:i" option do case $option in i) it=$OPTARG;; p) priority=true;; esac done if [[ ${@:$OPTIND} != "" ]] then dir=${@:$OPTIND} fi echo $priority $it $dir If I…
simpatico
  • 10,709
  • 20
  • 81
  • 126
4
votes
1 answer

Running a script with getopts works the first time, but doesn't work the second time I run it

This is my script. I adapted it from this tutorial so it can't be an error in the script itself. (The original script had the same problem.) #!/bin/bash while getopts "a:" opt; do case $opt in a) echo "-a was triggered, Parameter:…
Shoblade X
  • 363
  • 2
  • 9
4
votes
2 answers

bash scripting - how to process a comma separated list as input parameters to a function

I'm trying to figure out how to pass a comma , separated list as inputs and to have my function process the values one at a time. My function: addToWhitelist () { host='127.0.0.1' db='mytest' _mongo=$(which mongo); echo "${_ip}"; …
noober
  • 1,427
  • 3
  • 23
  • 36
4
votes
1 answer

Bash getopts: recognizing negative options (-x- or +x)?

(Similar to this, but in bash.) I have an existing bash script that uses the builtin getopts to recognize -s (flag only - no argument). I find I use that option every time, so I would like to make it default unless -s- or +s is specified on the…
cxw
  • 16,685
  • 2
  • 45
  • 81
4
votes
2 answers

Can getopts parse a subset of a bash script's arguments and leave the rest intact?

I am using getopts to parse arguments in a bash script. I want to do two things: remove processed options from "$@" leave unprocessed options in "$@" consider the command-line $ foo -a val_a -b val_b -c -d -e -f val_f positional_l positional_2…
starfry
  • 9,273
  • 7
  • 66
  • 96
4
votes
1 answer

How to use ${OPTARG} on getopts?

I have the following code: while getopts ":p:t:n:" o; do case "${o}" in p) p=${OPTARG} numep=$p mkdir $numep ;; t) t=${OPTARG} tip=$t if [ $tip == "c" ]; then touch…
Marko
  • 407
  • 1
  • 7
  • 19
4
votes
1 answer

Using getopts within user-defined-function in bourne shell

Is it possible to pass command line arguments into a function from within a bourne script, in order to allow getopts to process them. The rest of my script is nicely packed into functions, but it's starting to look like I'll have to move the…
AgentConundrum
  • 20,288
  • 6
  • 64
  • 99
4
votes
1 answer

Bash script - how to read file line by line while using getopts

I want to do two things in this script: 1) pass a file name to the script 2) pass options to the script example 1: $./test_script.sh file_name_to_be_read pass only file names to script example 2: $./test_script.sh -a -b file_name_to_be_read pass…
user97662
  • 942
  • 1
  • 10
  • 29
4
votes
1 answer

getopts in bash, script was working before and now I'm baffled

So I have a couple of getopts in my bash script. Here's an example of a working one. FOUND= SEARCH= COUNT=0 while getopts "ips:flenkc" OPTION do case $OPTION in i) FOUND=1 let "COUNT++" ;; …
Shrike
  • 43
  • 3
4
votes
2 answers

How to stop getopts from taking blank or null or other options as option value

I (presumably everyone) hit by this problem time to time but couldn't find any good workaround by myself. When getopts looks for an argument, it literally takes the next one, even if it's an option. This is what I did to stop that (code…
MacUsers
  • 2,091
  • 3
  • 35
  • 56
1 2
3
29 30