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

Using getopts to grep a file and export it to a file: why is another parameter mentioned?

I use getopts to obtain a MAC address and grep that MAC address through log files. It looks like this: #!/bin/bash while getopts ":m:hx:" opt; do case $opt in m) cat /var/log/vmlog/Verimatrix.log | grep $OPTARG | grep VCAS080455 …
Nicolas de Fontenay
  • 2,170
  • 5
  • 26
  • 51
0
votes
1 answer

Variable override in bash from command line

I've the following bash script. It calculates ax^2 + bx + c. Asks for a,b and c as you can see and gets x as a command line argument. echo "Enter a value for a: " read a echo "Enter a value for b: " read b echo "Enter a value for c: " read c echo…
Can
  • 4,516
  • 6
  • 28
  • 50
0
votes
1 answer

getopts: optional $OPT_ARG

I am looking to create a script that can run both -u and -u gtest_filter= Currently I have while getopts u: opt 2> /dev/null; do case $opt in u) UNIT_TEST=1; UNIT_TEST_OPTION=$OPTARG ;; ?) usage >&2; exit 1 ;; esac done But makes it mandatory to…
user1607549
  • 1,499
  • 3
  • 13
  • 17
0
votes
1 answer

Is this the correct way to write opts

Is this the correct way to get and set with OPTS? I want to be able to take in a lot of options like ./runthis.sh --option1 setuff --option2 setmorestuff while : do case $1 in -h | --help | -\?) usage …
amanada.williams
  • 467
  • 2
  • 4
  • 9
0
votes
1 answer

working with options in bash code

Possible Duplicate: Using getopts in bash shell script to get long and short command line options I'm trying to figure out how to make use of flag like -e/--email -h/--help for example. UPDATE: Current example: while getopts ":ec:h"…
Amanada Smith
  • 1,893
  • 9
  • 28
  • 42
0
votes
1 answer

How can I get getopts source?

My embedded device uses busybox and it only has getopt. I wanted to port getopts to my embedded device and was looking for getopts source to do that. Where can I find getopts source?
Ankur Agarwal
  • 23,692
  • 41
  • 137
  • 208
-1
votes
2 answers

Ensuring a single colon at start of string

I am using getopts to parse options. I want to ensure that the optstring has only a single colon at the start, and if this does not exist I want to introduce it. Here is an example of the initialisation of optstring local optstring="sn:z:"
Dilna
  • 405
  • 1
  • 6
-1
votes
1 answer

Simple calling a function within a function is not working/ have I missed somethig

APPLICATION_VERSION="1.0" APPLICATION_HELP="SOME HELP TEXT" function help(){ echo "h${APPLICATION_HELP}" exit 0 } help function versionapp(){ echo "v${APPLICATION_VERSION}" exit 0 } versionapp function check(){ while…
JnooriRS
  • 103
  • 11
-1
votes
1 answer

Bash script argument not being parsed

I wrote a bash script to help me with the build process of couple web apps. There are different build modes so i tried implementing this using flags (getopts) dev=false; clean_dest=false; all=false; while getopts…
fmi21
  • 485
  • 3
  • 15
-1
votes
1 answer

Bash getopts command in Golang

How do I pass a list as a flag variable in Golang? For example in Bash you can pass a list as getopts to the script. Something like that: ./myScript.sh -n list_of_names.txt //some file with about 50 names and later loop over the…
Stead
  • 19
  • 3
-1
votes
1 answer

How to getopts after $1 in bash

I want to run the script as ./script speed -a some_value -b some_value also ./script accuracy -a some_value -b some_value What I tried is while [ -n "$1" ]; do case "$1" in speed) for i in "${@:2}" do while getopts…
KATHAN PATEL
  • 103
  • 6
-1
votes
1 answer

How do I pass arguments to my functions in a zsh/bash script?

I am trying to combine these two functions in a script. The idea is to pass the flags -E to encrypt, and -D to decrypt. So far the flags are working. I get the different usages for encrypt, decrypt and help. Problem: The functions aren't getting the…
-1
votes
2 answers

Getopts default case bash script

I want to use getopts but the default case doesn't work. The code that I tried is: while getopts "sdp" arg; do case "$arg" in s) echo "1" ;; p) echo "2" ;; d) echo "3" ;; *) echo "default" ;; esac when I run the process:…
omer blechman
  • 277
  • 2
  • 16
-1
votes
1 answer

Default Flag Option Get Ops

I would like get ops to default to the -8 flag if someone runs the -9 and does not put anything for the OPTARG after the flag OR can i check to see if OPTARG is null and replace the empty value with a base value. ft1 and ftx are just format…
Calvin
  • 29
  • 4
-1
votes
1 answer

How can I accept long arguments using getopts in Bash?

I'm trying to have my getops function run with multiple flags and arguments but instead of short (-f style) flag, I want to accept a long one (--flag style). For example: if [ $# -lt 1 ]; then usage >&2 exit 1 else while $1 "hf:" opt; do …
John Doe
  • 159
  • 1
  • 10
1 2 3
29
30