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

Bash script not reading from file

Purpose This script is meant to copy one's public ssh identity file to many hosts that are listed in a file Problem The script is not properly reading IP's from the file. I believe this is because it is not reading the lines of the file. When I echo…
akjones
  • 88
  • 5
0
votes
2 answers

How to add help usage in Python script for getopt

Consider: import getopt options, remainder = getopt.getopt(sys.argv[1:], 'd:a', ['directory=', 'algorithm', 'version=', …
0
votes
1 answer

Error: File not found with name : -a while using getopts

I am Having trouble using getopts where i am trying to parsing options but getting error: Error: File not found with name : -a Code: while getopts ":a :b :c :d :e :f :g" opt; do case $opt in a) FILETOPARSE=$OPTARG echo…
Asnau Nauticas
  • 197
  • 1
  • 4
  • 20
0
votes
0 answers

How to prevent the terminal from echoing my getopts arguments?

I'm writing a script that includes getopts, and it works swimmingly: #!/bin/bash # Define help function for options function help(){ echo echo -e $YELLOW'--->COMMAND LINE OPTIONS:'$ENDCOLOR echo echo "Usage example:"; echo…
user2096457
0
votes
1 answer

Python 2.7.11 getopt doesn't read the argument

In Python 2.7.13 We have the following python code to take the command line argument: import sys import csv import os import sys, getopt import pandas as pd print('Python version ' + sys.version) print('Pandas version ' + pd.__version__) def…
Chubaka
  • 2,933
  • 7
  • 43
  • 58
0
votes
1 answer

Use optional argument before options on command line in a bash script

I have a script myscript that should do the following: Run on its own: myscript Run with an argument: myscript myarg Run with options: myscript -a 1 -b 2 -c 3 ... Run with an argument and options: myscript myarg -a 1 -b 2 -c 3 ... I cannot get 4.…
snurden
  • 173
  • 5
0
votes
1 answer

How can I make an option require another option?

I'm making a script that accepts options and two of them are -s and -e. The -s requires a start date and the -e requires and end date, but I want to make it so one can't be used without the other. In other words, if I use the -s option the -e option…
mackdiogo
  • 3
  • 1
  • 5
0
votes
1 answer

Pass commandline args into another script

I have couple of scripts which call into each other. However when I pass Snippet from buid-and-run-node.sh OPTIND=1 # Reset getopts in case it was changed in a previous run while getopts "hn:c:f:s:" opt; do case "$opt" in h) …
user3626708
  • 727
  • 2
  • 8
  • 24
0
votes
1 answer

How to make an argument optional in getopts bash?

I would like to make one of the optional characters (-t) which should not accept any argument in getopts bash. This is where i got so far while getopts ":hb:q:o:v:t" opt; do case $opt in b) Blasting_list=$OPTARG ;; l) …
upendra
  • 2,141
  • 9
  • 39
  • 64
0
votes
2 answers

bash - how to iterate a list/array through a parameterized bash script

I have a bash script where I pass the following arguments (its using getopts) like so: ./test.sh -o c03 -d mydb -t tblA -n c13 -r us-east-1 The execution works however, I need to alter this where -t (for table) needs to be a list of tables (tblA,…
noober
  • 1,427
  • 3
  • 23
  • 36
0
votes
0 answers

Shell: getopts commandline argument with conditional argument, how to do it?

I want to parse one argument with getopts, which requires two other parsed arguments to be set. So something like this, if -l is set to yes: sh name.sh -l yes -a "bla" -b "blubb" Then arguments -a and -b need to be set to. Otherwise these arguments…
Momina
  • 41
  • 1
  • 8
0
votes
0 answers

Pass all variables from one getops shellscript to another?

Assuming we have a getopts shell library, "command_line.sh": while getopts “c:” argv do case $argv in c) CLIENT_ID=$OPTARG ;; esac done echo "CLIENT_ID is ${CLIENT_ID}" How can we pass the above…
Chubaka
  • 2,933
  • 7
  • 43
  • 58
0
votes
0 answers

Variable inside Variable

Hey I have the following problem: FILE=/etc/squid3/conf.d/$CUSTOMER.conf Everytime i'm doing that: grep -o $replaced_ip $FILE grep: /etc/squid3/conf.d/.conf: Data not found I've startet the script together with the $CUSTOMER variable, so it is…
tso
  • 187
  • 4
  • 13
0
votes
1 answer

Bash script - last case in getopts not getting read

I have the following bash script #!/bin/bash id="" alias="" password="" outputDirectory="" extension="" function ParseArgs() { while getopts "t:a:p:f:r:o:e" arg do case "$arg"…
0
votes
0 answers

Cannot convert string '=' to double (m_Pos = 0) error in bash

I want to give a numerical value as input argument for a bash script which is as below #!/bin/bash while getopts ":hl:q:s:e:k:g:v" opt; do case $opt in l) lincRNAfasta=$OPTARG ;; q) query_species=$OPTARG ;; s) …
upendra
  • 2,141
  • 9
  • 39
  • 64