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

How to use options in bash for skipping parts of code

I have bash script and I want to be able to skip over (that is - do not execute) some of its commands based on options passed while invoking script. To illustrate what I want to accomplish, I have written script with if statements that basically…
Rafal
  • 864
  • 10
  • 21
0
votes
2 answers

BASH: getopts with default parameters value

I've got another bash-script problem I simply couldn't solve. It's my simplified script showing the problem: while getopts "r:" opt; do case $opt in r) fold=/dev dir=${2:-fold} a=`find $dir -type b | wc -l` echo "$a" …
Smugli
  • 11
  • 1
  • 4
0
votes
2 answers

Bash: Passing parameters using getopts

I know this is a simple question and I have been examining the getopts tutorials and examples, but I am stuck. If you check this link, you will see that with the help of @fedorqui, I have managed to write the code in order to get an element of an…
iso_9001_
  • 2,655
  • 6
  • 31
  • 47
0
votes
1 answer

How to pass parameters to Bash getopts using a custom variable as opposed to ${1}, ${2} etc

I want to specify parameters (options and arguments) in a Bash variable and to pass that variable to getopts for parsing instead of the usual variables ${1}, ${2} etc. I am not sure how to do this. Often in documentation for getopts, the getopts…
d3pd
  • 7,935
  • 24
  • 76
  • 127
0
votes
2 answers

Parsing OPTARG inside case with regular expression and [[ ]]'s

So, I'm setting up a bash script and want to parse arguments to certain flags using getopts. For a minimal example, consider the a script which has a flag, -M, and it takes y or n as an argument. If I use the following code: #!/bin/bash #…
StevieP
  • 1,569
  • 12
  • 23
0
votes
1 answer

How can I generate a list of arguments unacceptable to getopts in Bash?

Let's say I'm running getopts in a Bash script with the option string ":a:b" and I provide the following command line parameters: ./script.sh -a foo bar -b bar zappo The instance of "foo" is an expected argument for the option a and the option b…
d3pd
  • 7,935
  • 24
  • 76
  • 127
0
votes
1 answer

Changing no args default case in getopts - bash

Whenever I use getopts and i don't give any argument for a given flag I get the following message: "option requires an argument -- d" I would like to remove this message and allow the user to retype the options using the read command. Here is my…
0
votes
1 answer

ksh getopts unknown option Error

I am using getopts to get passed options to a ksh script but the options aren't being recognized by getopts. Here is the options part of the usage string I give to getopts #OPTIONS USAGE+="[w:week?print the whole week]" USAGE+="[b:before?print the…
0
votes
1 answer

Using getopts and ${1} together

I'm trying to write a script that can use both ${1} and getopts options simultaneously. I would like it to work using the usage line: ./test_script test -a to print: test -a was triggered! I've tried echo ${1}; while getopts "c:a" opt; do …
user1382685
  • 49
  • 1
  • 5
0
votes
4 answers

Bash Script to function as wc command

I am trying to create a script using getopts that would work as wc. the problem is that I get stuck when I use two switches together. The script: while getopts l:w:c: choice do case $choice in l) wc -l $OPTARG;; …
Bash Noob
  • 53
  • 2
  • 10
0
votes
1 answer

poptGetArgs return null.

I am using poptGetArgs to read multiple values for single option. But it always give null as return value. I posted my code below. please help me to resolve if it has any error. int main(int argc, char **argv) { char filename[ 128 ],…
VNS
  • 158
  • 1
  • 10
0
votes
1 answer

getopts treats an option as argument of a previous one

I've the following script: #!/bin/bash USER="NONE" LOST=0 AVG=0 while getopts ":pmu:" OPTION; do case $OPTION in u) USER=$OPTARG ;; p) LOST=1 ;; m) AVG=1 …
Zagorax
  • 11,440
  • 8
  • 44
  • 56
0
votes
1 answer

Errors using getopts on cygwin

I am trying to use getopts on cygwin in a bash script. The following is the code: #!/bin/bash # Sample program to deal with getopts. echo "Number of input arguments = $#"; i=0; while [ ${i} -lt 10 ]; do i=$[${i}+1]; echo ${i}; done while…
Sriram
  • 10,298
  • 21
  • 83
  • 136
0
votes
4 answers

How to sort a file with a bash script using sort and getopts?

This is my bash script that sorts a file by columns. while getopts "123456" flag do sort -t: -k $flag names.txt done Right now it does exactly what I need, but I need to have the filename be a parameter too. The input right now is ./sortScrpt…
Andrew Tsay
  • 1,893
  • 6
  • 23
  • 35
0
votes
1 answer

How to check for extra parameters in ash script?

I have an ash script where I need to check whether the user has entered anything stupid. The proper use is: script -s [-f filter] [-n number] And I need to detect if user has for example evoked it like: script read…
ThePiachu
  • 8,695
  • 17
  • 65
  • 94