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

Getopts, bash and multiple methods

I have got few methods (for example: addUser, addProject, addRepository). In all of them i am using curl option and they have specified local(another) variables. In first function I can have: addUser{ username user password email} Second method…
adamos
  • 69
  • 8
0
votes
1 answer

Getopts loop not working

I am working on a bash script, and I decided to use getopts to get the options, but the loop I used doesn't work! Could someone please help me? while getopts "u:p:k:s:t:c:l:" flag do echo $flag case "$flag" in k) APIKEY="$OPTARG" …
Danogentili
  • 664
  • 7
  • 13
0
votes
1 answer

using getopts did not get the input value

I am running the below script, but it looks like the $filename or $srvname did not get the input value. say for eg: ./test.sh -n abcd.net gives the output echo 'Filename or node name must be defined.' it means that, the $srvname did not get the…
user3834663
  • 513
  • 2
  • 7
  • 17
0
votes
2 answers

Bash - optional argument required but not passed for use in getopts

How do I flag an error in a bash script which requires an argument for getopt, but the user didn't pass it? e.g. the script below requires an argument for option "t": #!/bin/bash while getopts "ht:" OPTION do case $OPTION in h) …
user2511788
  • 139
  • 1
  • 1
  • 8
0
votes
1 answer

bash getopts validate options

I have following code in bash script: # argument validation and usage help usage() { cat << EOF usage: $0 options File Integrity Monitoring Script: OPTIONS: -b input file for [backup] -r input file for [diff report] -l list…
Satish
  • 16,544
  • 29
  • 93
  • 149
0
votes
1 answer

Getopts perl order

I am having some problems with getopts in perl. I use: getopts("abc:ds:", \%options); and I'm cheking the options with if (defined $options{a}) where $options is the hash the options are written into. After the options the user has to enter a…
Darius
  • 135
  • 3
  • 13
0
votes
1 answer

Separating arguments with getopts

So, I am trying to write a script to 1.) Create a new directory with the name the user has specified 2.) Download files from a remote location into the new directory 3.) Append a port to a file in the new directory What I have now is OPTIND=1 while…
Dhs92
  • 3
  • 2
0
votes
0 answers

bash getopts making user input as error proof as possible

I have the following code in one of my scripts. I am trying to make the user usage as error proof as possible. while getopts ":c:d:t:T:a:" opt; do case ${opt} in c ) currency=$OPTARG …
sunnyp101
  • 41
  • 3
0
votes
1 answer

Checking for lack of flags in bash

If I were to do something like this to process arguments in bash, how would I check if there were no arguments? It doesn't seem to go to the *) case, but I'd still like to put a usage statement in there somewhere. while getopts 'ias' flag; do case…
user3475234
  • 1,503
  • 3
  • 22
  • 40
0
votes
2 answers

Bash + print default character after user prompt

-I made a bash function like this: find_stuff () { list=`find $SEARCHPATH -type f -user $USERID -mmin -$SOMETIME -not -path '*/\.*' -exec ls -1rt {} \+` if [ -n "$list" ]; then for i in $list; do echo "Found item: "$i"";…
user.py
  • 89
  • 2
  • 5
0
votes
1 answer

Script with getopts doesn't assign value to variable

The variable MAANDINT doesn't get a value assigned. Please help me to find a solution #!/bin/bash if [ $# -eq 0 ] then echo Je moet een maand ingeven! exit 1 elif [ $# -eq 1 ] then JAAR=$(date +'%Y') else JAAR=$2 fi while getopts…
Reconize
  • 5
  • 3
0
votes
2 answers

getopts second flag is not required?

Having issues making sure that both -v and -o are both required elements but -h is not in my getopts. how would I fix this? usage() { echo "Usage: $0 [-v <5.x|6.x>] [-o ]" 1>&2; exit 1; } if ( ! getopts ":v:o:h" opt); then echo "Usage:…
ehime
  • 8,025
  • 14
  • 51
  • 110
0
votes
1 answer

Why is the 3rd argument not parsed in getopts bash

I am trying to write a simple script to do something depending on the input arguments. Here is what I have written #!/bin/bash usage() { echo "Usage: $0 [-l ] [-p ] [-d ]" 1>&2; exit 1; } while getopts ":l:p:d" o; do …
Sagar Sakre
  • 2,336
  • 22
  • 31
0
votes
1 answer

bash can a sourced script tell if a getops has been passed to parent

I am wondering if & how a sourced bash script can tell if a getops variable was passed to a parent, such as, .parent.sh -a MyVarForChild_a in the parent.sh I call . child.sh and I would like to check if the -a MyVar was passed. I would prefer NOT…
art vanderlay
  • 2,341
  • 4
  • 35
  • 64
0
votes
1 answer

getops 1 option to receive 2 arguments

I've searched around found out that getops receives zero or one argument only, But I really need to make this work, I need to make my script run like this: ./script.sh -a string integer What it does is write the string and integer into a text…