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

Is there a way in bash script to have an option to give an argument but it shouldn't a must?

I have a scenario where i would like to assign an option a default value but a user can decide to give it another argument: Here is an example check_param() { for arg in "$@"; do shift case "$arg" in …
Technas
  • 17
  • 5
0
votes
2 answers

Bash: Calling script with multiple optional flags - it always picks the first one and ignores others

I have a bash method: SUITE='' FILTER='' COVERAGE='' COVERAGE_REPORT_PATH="tests/Report" while getopts :s:f:c: flag # do not remove first colon as it makes flags optional do case "${flag}" in s)…
Matt Komarnicki
  • 5,198
  • 7
  • 40
  • 92
0
votes
1 answer

Changing variable in function dependant on getopts flag bash

I have a bash script that finds connected devices and logs the serial port output to a file. I am using getopts to choose the method of logging. Two of the functions are the same except for one line. I would like to change it to a single function…
0
votes
1 answer

Not able to connect to sqlplus from linux by passing credentials as parameter using getopts

I am trying to connect sqlplus by passing credentials as a parameter using getopts method- Code: while getopts ":o:s:t::i::p::f::" opt; do case "$opt" in o) uname=$OPTARG ;; s) sname=$OPTARG ;; t) password=$OPTARG ;; i) ip=$OPTARG…
saurabh704
  • 63
  • 6
0
votes
1 answer

Why would a script have multiple questions marks in the OptionString of getopts

I have a script that has the following line: while getopts "b:?1:?2:?g:r:n:t:?o:?" opt; do how is this any different than: while getopts "b:1:2:g:r:n:t:o:" opt; do
Brian Wiley
  • 485
  • 2
  • 11
  • 21
0
votes
1 answer

Ways to provide list of parameters to options in shell scripts?

Basically I am making a script where I would like to give a list of parameters to some option, say -l, eg: usr@domain:$./somescript -l hi bye cry nigh The list specified to -l would be then stored into a specific array and iterated over. So far, I…
Zage12
  • 5
  • 4
0
votes
0 answers

is there a way to restrict getopts to work on single option only?

I am writing a bash script with getopts which contains multiple case statements. My requirement is to be able to decide when getopts can take multiple options and when not. while getopts "hla:d:t:k:w:e:f:i:m:u:L:D:" options do case $options in …
0
votes
0 answers

Mishandling of input using "getopts" and "case"

ISSUE: I need to place a users cmd-line argument ( in this case a directory path ) into an associative array using declare -a DIR. The directory path will be stored in the array for later use in the script. However, at the moment that when the…
dex
  • 11
  • 6
0
votes
0 answers

Argument getting triggered even if it is optional and mentioned in getopts

I have written a script myscript which does an action on particular type of files. However there is an option for displaying the filename with -d. If I've not mentioned $ myscript -d also it is saying, invalid file path (because that file should be…
Mtoklitz113
  • 3,828
  • 3
  • 21
  • 40
0
votes
1 answer

How to show ALL the invalid options entered when getopts is used?

I have written a script myscript: while getopts "t:" opt; do case ${opt} in t ) target=$OPTARG ;; * ) echo "Invalid option: $OPTARG requires an argument" exit 1; ;; esac done shift $((OPTIND -1)) If I…
Mtoklitz113
  • 3,828
  • 3
  • 21
  • 40
0
votes
1 answer

The getopts command not taking in multiple options that require an argument

I'm having problems with getopts command. When I want to use an option that requires the SAME argument, it stops running the program after the first option that requires an argument (-i for example). So when I want it to take the options -id or…
0
votes
0 answers

getopts bash query for argument check

I have a getops script as below.. I am facing an issue with this script.. The usage is Usage: ./getoptstest.sh [-f] [ -i INSTANCES ] If I pass,./getoptstest.sh -f -i 1.2.3.4 ,it works fine with the result, Result f good i good 1.2.3.4 …
Rad4
  • 1,936
  • 8
  • 30
  • 50
0
votes
2 answers

Why is OPTIND messing my positional params?

I have this function: sgrep () { local OPTIND; if getopts i o; then grep --color=auto -P -in "$1" "$2"; shift $((OPTIND-1)); else grep --color=auto -P -n "$1" "$2"; fi | sed -E -n 's/^([0-9]+).*/\1/p' |…
Herdsman
  • 799
  • 1
  • 7
  • 24
0
votes
0 answers

shell getopts - assign a default value if the argument is not provided

Im trying to implement cron job, I learned the getopts from here. My requirement is if I give the argument while triggering then take that value else assign a default value. My script ALPHA=unset BETA=unset CHARLIE=unset DELTA=unset usage() { …
TheDataGuy
  • 2,712
  • 6
  • 37
  • 89
0
votes
1 answer

Bash Scripting- Switches

#!/bin/bash while getopts "p:" opt; do case ${opt} in p )#print the argument value echo "I like $OPTARG programming" ;; \? ) What if i want to print an error if I don't give '-p'
Nik
  • 1