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

UNIX - getopts for multiple switches

The below while loop exists within a shell of mine called test.sh I want to run the following command ksh -x test.sh -c -e file1 file2 I want the while loop to perform both the c) case first then e) case within the loop, however at the moment it is…
DaveMac001
  • 155
  • 3
  • 13
0
votes
1 answer

Getopts same flag two times

Hi i have a basic question but i wasn't able to find a good answer I have this Code: while getopts :weco:r:u:hP: ARG; do case $ARG in . . . h) #set option "h" - show help; help ;; P) …
Nico
  • 323
  • 4
  • 14
0
votes
0 answers

How can I get an option to always be executed last?

I'm using getopts to deal with parameters. I need to have an option that has to be always executed last. Let's say i have option a,b,c,d,e, I need e to always be last. How would I do that?
Max
  • 59
  • 13
0
votes
1 answer

How getopts is setting a bash variable

I would like to re-implement my own getopts (in python if possible). My problem is that I don't understand how this is possible: # The call of the following script ./script -h #!/bin/bash getopts 'h' TEST # output nothing echo $TEST # output h env…
onda47
  • 615
  • 2
  • 6
  • 21
0
votes
1 answer

How to use getopts in bash script?

I'm trying to use getopts like this: #!/bin/bash while getopts "i" option do case "${option}" in i) INT=${OPTARG};; esac done echo "$INT" But it prints $INT only if i use getopts "i:". If I understand correctly, the colons in the optstring…
kusayu
  • 93
  • 6
0
votes
0 answers

getopts is not working in bash script

Test1.sh script have the below line in it. source $HOME/set-env -l test_$START_TIME Test2 script while getopts ":l:" opt; do case $opt in l) echo "file name: $OPTARG" FILE_NAME=$OPTARG ;; \?) echo "Invalid…
0
votes
1 answer

Getopts more options trigger one event

im currently updating my first script i wrote to print room schedules I want to use getopts to parse my options now my question: My Options are to print the schdule for all rooms are: -w for the next day -e to print the schedule for monday on…
Nico
  • 323
  • 4
  • 14
0
votes
2 answers

getopts for mandatory options for running the script

I have 8 options which are mandatory to run the script. However I am unable to pass option values after the first argument. Could you please let me know what I am doing wrong here. #!/usr/bin/env bash usage() { echo "Usage: $0 -f…
user44552
  • 153
  • 1
  • 10
0
votes
1 answer

Why doesn't getopts in bash yield : (per docs) when an option is missing its associated argument?

I'm relatively new to this field, and I'm running my shell script named "statsrandomrun.sh" with following code snippets: #!/bin/bash while getopts "m:s:xh" opt; do case $opt in m) MU=$OPTARG; mflag=true; ;; s) SIGMA=$OPTARG;…
0
votes
1 answer

getopts options with same starting alphabet

I am using getopts to pass parameters to a shell script. For example, I want to make operations on two sections in the script, that both start with the same letter. I also dont want to choose any other letter. For example I have two projects :…
infoclogged
  • 3,641
  • 5
  • 32
  • 53
0
votes
0 answers

bash getopts, why is my script not parsing additional input parameter?

I'm trying to create a bash script which reads a set of optional input parsing parameters. I tried to follow this example on GitHub.com. I tried to add another integer parameter to read, which I called x (or xxx-size). I thought it was sufficient to…
DavideChicco.it
  • 3,318
  • 13
  • 56
  • 84
0
votes
1 answer

bash getopts ignores options unless they are specified in a certain way

Bash getopts should allow for options and arguments not to depend on position, as stated in tutorials and docs. The following snippet of code could receive two options: a debug -d option which requires an argument a force -f option, without…
rgulia
  • 523
  • 7
  • 11
0
votes
1 answer

How to specify the arguments' order with getopts function?

test-fun(){ OPTIND=1 while getopts "waz" arg do case $arg in w) echo "ok w" ;; a) echo "ok a" ;; z) …
user7988893
0
votes
1 answer

Why does a match never use the second match arm?

I am trying to make reimplementation of first 2 Final Fantasy games using original data from various platforms. I want to get 2 program arguments using the getopts crate and handle both of them by using match but it just executes the first match…
user160995
  • 129
  • 2
0
votes
1 answer

(Bash Script) How to use the same directory for multiple options?

I am confused how to read in multiple options using getopts and how to use one directory as the source for both options. Let me be more specific... If I were to say: ./rpsm.sh -u -g /some/directory or ./rpsm.sh -ug /some/directory How would I…