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

Capturing unrecognised options and unspecified option values with getopts

I have the following bash function that uses getopts to parse positional arguments. Am trying to make options fail and get to print error messages. pfa () { local OPTIND OPTARG local shortopts=":Vuhv:f:sc:" while getopts $shortopts arg; do …
Dilna
  • 405
  • 1
  • 6
0
votes
1 answer

Is it possible to mix getops with optional positional parameters?

I made a script to restart my web server automatically, do certain checks, and then print a message with the time stamp to a log file. Since I call this file from different places (cron jobs, when I update my site, manually, etc.), I wanted to have…
0
votes
1 answer

Pass text file as argument/variable [bash]

I am trying to pass a text file as a specific argument and print its name and content... #!/bin/bash ## set input args while getopts "f" option; do case "${option}" in f) arg3=${OPTARG};; esac done ## script echo…
jgarces
  • 519
  • 5
  • 17
0
votes
1 answer

korn script options not finding getopts correctly

I have a korn script that does this: testing.ksh: m_Ftp -t'echo "No skipping"' -f DEV_OVR MMMM <
Michele
  • 3,617
  • 12
  • 47
  • 81
0
votes
1 answer

Bash command to specify which egrep to search

I'm trying to create two separate options to search through a file, one for phone numbers and one for emails. Nothing seems to happen when I run the file at the moment. #!/bin/sh while getopts ":-e:-p:" option; do case $option in -e) egrep -o …
0
votes
2 answers

bash scripting - using of options and not option

I created a script in bash that checking the strength of a password comibination I want to add an option that the user can input a password from a text file (The user types -f and then the file path and the he gets a password review ) or he types…
0
votes
0 answers

shift and getopts

I am using getopts and have the intention of using an option -v which can take an optional argument. When using getopt isnstead of getopts, people customarily use the shift command to go to the next argument. But I do not see people use shift when…
Nigel
  • 39
  • 4
0
votes
0 answers

How do I use getopts to add to a variable in a file shell

i'm trying to use getopts to make a command: actu -c [credits to add] to add a value to a variable that is on a .txt file but I don't understand what i'm doing wrong: #!/bin/bash set -e set -u set -o pipefail while getopts 'c:' OPTION; do case…
henryke9
  • 23
  • 3
0
votes
1 answer

How to pass multiple flag and multiple arguments in getopts in shell script?

Answer given here doesn't here any of my questions: bash getopts with multiple and mandatory options I am new to shell script and trying to write a shell script with getopts meanwhile I couldn't find answers to the below questions. Can somebody…
pk786
  • 2,140
  • 3
  • 18
  • 24
0
votes
1 answer

Parsing long command-line arguments not working with getopt

I want to parse long command-line arguments for a large script as part of my current project. I have never tried getopt before but want to try the first time to make the script look tidy. Before trying to push getopt on that large project script, I…
vjwilson
  • 754
  • 2
  • 14
  • 30
0
votes
1 answer

Unable to workaround on getopt options

I am trying to play around with getopt options. I want both short options and long options. For my following test script, it does not produce my required output. #!/bin/bash options=$(getopt -o d:f:t: -l domain -l from -l to -- "$@") [ $? -eq 0 ]…
vjwilson
  • 754
  • 2
  • 14
  • 30
0
votes
0 answers

Bash scripting using getopts not accepting multiple arguments

I'm trying to build a script in bash and building a getopts loop. I have two options(x and h) that don't have any arguments and the other two have it. But my while loop fails by taking only one option and doesn't take any other arguments, how can I…
bat_singer
  • 11
  • 2
0
votes
0 answers

Variable set by bash flag cannot be used by another command

I wrote the following bash script to print the top and bottom rows of a CSV file as a table. #!/usr/bin/env bash # Default argument …
Dan
  • 11,370
  • 4
  • 43
  • 68
0
votes
1 answer

Bash 4.2.46 getopts concatenated short options and resetting OPTIND during processing leads to an infinite loop: How can this be remedied in Bash?

I'm using the following Bash version in a up-to-date CentOS 7 VM: GNU bash, version 4.2.46(2)-release (x86_64-redhat-linux-gnu) The following code performs as expected (take note of -x -y): set -- -x -y; OPTIND=1; while getopts xy opt; do echo $opt;…
Steve Amerige
  • 1,309
  • 1
  • 12
  • 28
0
votes
1 answer

Bash getopts option with another

My current Bash script looks like in the following. So far it's working except I have no idea how to make it so the two options -f and -o work together: the -o FILE option to display the most relevant information in a file passed as an argument (if…
eya
  • 3
  • 3