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

shell getopts parameters collection issue

I have below code in my shell script. show_help() { cat <
Shailesh Sutar
  • 370
  • 1
  • 6
  • 22
0
votes
1 answer

getopt multiple line statements while parsing arguments

Having a bit of trouble here. I haven't had to do long options ever, so I am trying getopt rather than getopts. For some reason it keeps stating shift as an unrecognized token. Any reason why? Also is this a proper implementation for getopt? Or…
Kyle Calica-St
  • 2,629
  • 4
  • 26
  • 56
0
votes
1 answer

Prevent the usage of two flags simultaneously when running script

My script.sh usage: ./work.sh [options] parm_1 parm_2 parm_3 parm_4 the options: -y year -n number -1 (flag, no parameters) -2 (flag, no parameters) i would like a mutual exclusion between -1 and -2 flags, so that if -1 is specified, -2 cannot be…
DamienzOnly
  • 88
  • 1
  • 10
0
votes
2 answers

Getopts in sourced Bash function works interactively, but not in test script?

I have a Bash function library and one function is proving problematic for testing. prunner is a function that is meant to provide some of the functionality of GNU Parallel, and avoid the scoping issues of trying to use other Bash functions in Perl.…
MrDrMcCoy
  • 351
  • 4
  • 18
0
votes
1 answer

getopts not working when called with an argument

This is my first attempt using getopts, and so far it hasn't been working for me. The code in my script is: while getopts "s:" opt; do case $opt in s) subj=$OPTARG;; \?) echo "Incorrect usage";; esac done echo "" echo $subj When I try to…
djl
  • 267
  • 1
  • 3
  • 13
0
votes
1 answer

error parsing getopt parameters

When I try to run the script as below: ./myscript.sh --certtype ca --password xyz --commonname xyz I get the following error: + local BIN_PATH CertType Password Commonname + BIN_PATH=keytool ++ getopt -u -o t:p:c -l certtype:password:commonname --…
F. K.
  • 694
  • 3
  • 9
  • 23
0
votes
1 answer

getopts throwing invalid argument error in linux shell script

I am trying to run a shell code with getopts arguments passing at run time. But below script is throwing "Invalid Argument" error. strt_tim=`date` while getopts dir:day:size: arg; do case "$arg" in dir) dirnm="$OPTARG";; day) dy="$OPTARG";; siz)…
Anku g
  • 101
  • 8
0
votes
1 answer

How to get value of argument in with getopts

while getopts ':s:e:ci:z:n:d:m:' opt; do echo -- $opt echo -- $OPTORG done For The above snippet when executed with bash a.sh -c -m lsdjfe -s "all ab" -d all Results into -- c -- -- m -- -- s -- -- d -- Where as i was expecting -- c -- -- m --…
codeofnode
  • 18,169
  • 29
  • 85
  • 142
0
votes
2 answers

Flags that result in function calls and passing arguments

I would like to update a script that is currently like this: $ example.sh a b Here is the code within example.sh for var in "$@" do $var done Where it takes in arguments and those arguments are looped over and executed (assuming those…
ThomasReggi
  • 55,053
  • 85
  • 237
  • 424
0
votes
1 answer

getopts in C, command line arguments

What does in getopts the "abc:d:" mean? How does this work? example: while ((option = getopt(argc, argv,"abc:d::")) != -1) I am writing a program in C and it has to use command line arguments like: compress -t [1..5] -[c,d] I can't get it right.
0
votes
1 answer

|+ operators in getops of ksh

What is difference when we use |+ operator in getopts of ksh ? Examples of code are while getopts d: o do case "$o" in d|+d) seplist="$OPTARG";; esac done echo $seplist and while getopts d: o do case "$o" in d) …
Tsuyoshi
  • 27
  • 1
  • 5
0
votes
1 answer

bash - getopts only parses the first argument if operands are required

Once a bash program is executed while processing options in getops, the loop exits. As a short example, I have the following bash script: #!/usr/bin/env bash while getopts ":a:l:" opt; do case ${opt} in a) ls -a $2 ;; l) …
homersimpson
  • 4,124
  • 4
  • 29
  • 39
0
votes
2 answers

Bash script to test for only presence of flag

I have a bash script that I need to take in a user name with a flag, and then I want to be able to look for -r or -w to indicate whether this should be a read or write. Currently I am using get opts, but this requires that an actual argument be…
William Ross
  • 3,568
  • 7
  • 42
  • 73
0
votes
0 answers

using getopts to have 1 option require 4 options

I have been trying to read a lot about getops for bash because I have a project that requires the use of it for a script. The issue is I cannot find what I want to call nested options (if its even called that) For the project I am required to have…
ZerkerEOD
  • 25
  • 6
0
votes
0 answers

Grouping together getopts style args in bash

I'd like to be able to store a bunch of args in bash that I want to use in a bunch of different curl commands. However, my attempts so far haven't worked in the case where the arguments have spaces in them. I made a simple bash script to replicate…
Julian Grinblat
  • 161
  • 1
  • 12