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 behaving strangely in python

I am using getops for args management and for some reason my -s variable is not working. My code is below, as well as the output I am getting try: opts, args = getopt.getopt(sys.argv[1:], "hadsp:v", ["help", "all", "display", "single=",…
user2455869
  • 151
  • 1
  • 13
0
votes
0 answers

Python - Amateurish code

First of all, my code does work as expected at least to some extend. The intention of the program is to extract data from a DBMS in order to create a CSV-dump and transfer it to a FTP-server. That does work. My to issues: The functions of the class…
royskatt
  • 1,190
  • 2
  • 15
  • 35
0
votes
1 answer

Arguments to change variable values in Bash script

i have this script in bash: #!/bin/bash dir="/home/dortiz/Prueba" for i in $dir/* do cat $i | awk '{print $1" " $2" " $3" " $4"\n " $5}' | \ awk '/gi/{print ">" $0; getline; print}' | \ awk '$3>20.00 {print $0; getline;…
Dani
  • 91
  • 1
  • 1
  • 6
0
votes
1 answer

getopts is not sending my options to standard out

I'm beginning to experiment with getopts but am running into a few errors. When I enter an invalid option such as -A the program output is not what it needs to be. #!/bin/bash function usage() { echo "Usage: $0 -h [database host] -d [test…
user3299633
  • 2,971
  • 3
  • 24
  • 38
0
votes
1 answer

unable to echo value after exporting variable using getopts

I'm trying to figure out in my script, why my variable dbname isn't being set when using getopts after explicitly doing an export on that variable. I tried do an echo, echo $dbname and its the only variable that doesn't have a value. Here's my code…
noober
  • 1,427
  • 3
  • 23
  • 36
0
votes
0 answers

GNU getopt optarg null

In order to understand how to use getopt I used the example from gnus manual example. I reduced it to the case I was interested, as stated below: #include #include #include #include int main (int argc, char…
famfop
  • 185
  • 1
  • 1
  • 9
0
votes
2 answers

using a combination of positional parameters and 1 named parameter

I am still "baking" my shell with these features so my question may not sound like "totally ready" ..its more of a "whats the best way to design this" as against something like "I ran into this error or this wont work..now what do I do ", so bear…
user1874594
  • 2,277
  • 1
  • 25
  • 49
0
votes
1 answer

Using Floating Points In Bash Script

I have this Bash script that has a basic if...else operator but the script doesn't seem to recognise the values correctly, and just continues the script, when it should stop and show an error. I believe it's because I am using a float number, and…
Chris Mellor
  • 347
  • 5
  • 15
0
votes
1 answer

Why is this simple bash script not working? getopts

I don't get why this won't work as it's really simple. #!/bin/bash type='A' while getopts "t:" OPTION do case $OPTION in t) echo "The value of -t is $OPTARG" type=$OPTARG exit ;; \?) echo "Used for the…
woutwoot
  • 152
  • 1
  • 12
0
votes
1 answer

Bash: getopts through case

I'm trying to get a getopts option through a case. The way my script is setup now, I have a start, stop, status, help options and I want to add some -x options to that to control some variables. If I don't use the start/stop/status then the -x…
0
votes
2 answers

bash getopts not recognizing the second argument

This is my entire script in its simplest form. #!/bin/bash src="" targ=${PWD} while getopts "s:t:" opt; do case $opt in s) src=$OPTARG ;; t) targ=$OPTARG ;; esac shift $((OPTIND-1)) done echo "Source:…
kshenoy
  • 1,726
  • 17
  • 31
0
votes
1 answer

How to throw error to only use one option flag when using getopts in bash script?

I have been searching but i have a question in regards to bash scripting and getopts. I am trying to build a fool proof script using getops. What would be the best approach so when the user using the option that it will throw an error if i add more…
0
votes
1 answer

How to exit getopt if two specific arguments are passed?

What I am trying to do is that if two specific arguments are passed at once then script would exit and HELP function is executed. while getopts ":H:D:S:h:" arg; do case "${arg}" in H) HOUR=${OPTARG};; D) DAY=${OPTARG};; h) HELP;; \?) #unrecognized…
madbitloman
  • 816
  • 2
  • 13
  • 21
0
votes
1 answer

getopts is not capturing arguments (file rename script)

Based on Google's Style Guide, I made the following script. The script should take files like a.txt and b.txt and change them to PREFIX_1_prefix_a.txt and PREFIX_1_prefix_b.txt. However, when I enter: $ sh rename_enum.sh -n 100 -P X -p Y I expect…
reynoldsnlp
  • 1,072
  • 1
  • 18
  • 45
0
votes
1 answer

Bash with getopts/flags and multiple functions

I have problem with getopts. When i write ./nameofscript.sh -n name - it should make a new repository but it says to me : -- n is unknown option and in new line: Adding user with name: blank. Why does it happen ? #!/bin/bash …
adamos
  • 69
  • 8