Questions tagged [getopts]

`getopts` is a shell built-in used to parse command line options, while `getopt` is its non-built-in counterpart.

Filter by
Sorted by
Tagged with
0 votes
2 answers
27 views

Use getopt to directly retrieve option value

I am not very experienced with getopt, but in all codes I have seen, it is only being used to reorder the program arguments and then use switch/case to decode which options and arguments are being ...
user avatar
0 votes
1 answer
102 views

Bash script with subcommand and long options only

How can I use getopt or getopts with subcommands and long options, not with short options? I know how to implement short and long options with getopts. Solutions that I've found so far are using ...
user avatar
4 votes
1 answer
504 views

How to use getopts in bash

I am new to bash scripting and I am trying to write a script with getopts so that when script.sh -sp is is invoked the URL and column and row count is printed out. And when script.sh -r option is ...
user avatar
  • 55
0 votes
1 answer
255 views

getopts with double colons for optional values

Does getopts not accept optional arguments for an option using double colons (e.g. using shortopts="t::")?
user avatar
  • 13
-1 votes
2 answers
2k views

Handling long-options with getopts

I am parsing options with getopts but would like to handle long-options as well. print-args () { title="$1" ; shift printf "\n%s\n" "${title}: \$@:" for arg in "$@...
user avatar
  • 53
1 vote
1 answer
38 views

Ensuring only 1 mandatory parameter is passed to script

I am writing a shell script and new to getopts for parameter parsing. What I have are 1 optional and 2 mandatory parameters and what I want to do is ensure that only one mandatory parameter is passed. ...
user avatar
  • 13
0 votes
3 answers
1k views

Detecting getopts `--` (double dash) to issue a message

I am using the customary way of using getopts through a variable named arg. I can capture the option names as follows. Is it possible to detect the moment getopts reaches "--" so that I ...
user avatar
  • 21
-2 votes
2 answers
195 views

Setting variables using getopts [duplicate]

I would like to use getopts for parsing arguments to bash functions. while getopts ${shortopts} arg; do But am encountering problems in setting variable values. Calling myfunc -v8 or myfunc -v 8 ...
user avatar
  • 59
-2 votes
1 answer
306 views

getopt long and short options

Does getopt require a short option for every defined long option? In other words, can I have a long option without being required to define an equivalent short option.
user avatar
  • 319
3 votes
2 answers
1k views

bash script with optional input arguments using getopt

I have copied code from tutorialspoint's getopt article and got the following script to work (sort of): ##argument_script.sh VARS=`getopt -o i::o:: --long input::,output:: -- "$@"` eval set ...
user avatar
0 votes
0 answers
33 views

Shell script getopts weird behaviour args not parsing

I'm trying a simple shell script to get the arguments passed by the user, but cannot seem to find my error. Sometimes it works, but mostly not (weird). helpFunc() { echo "" ...
user avatar
3 votes
2 answers
301 views

shell getopt with help argument [closed]

I'm preparing a script with getopt. I would like to add the help section. So if they use --help or -h it should execute the function (just print the instructions) and return it. Sample code: log_type=...
user avatar
8 votes
1 answer
13k views

Bash getopts, mandatory arguments

I am working with a pretty simple bash script, but I am facing a problem which I can't resolve: I have myscript.sh with three parameters, -u, -h, and -p. Both -u and -h are mandatory, needed for the ...
user avatar
3 votes
1 answer
650 views

Using command-line parameters as destination for cp and mv in bash script

I am attempting to do copy a file (or rename a file) by running the script with flags/parameters to give both the source and the destination file name: #!/bin/bash/ while getopts s:d flag do ...
user avatar
0 votes
2 answers
4k views

Processing optional arguments with getopts in bash

I have a bash script which processes an input file with optional arguments. The script looks like this #!/bin/bash while getopts a:b:i: option do case "${option}" in a) arg1=${OPTARG};; b) ...
user avatar
  • 131
4 votes
1 answer
894 views

getopts gets no arguments when function is called inside while loop

I am trying to call a function in a while loop passing some arguments. However, getopts can only get the arguments for the first call. Here's a minimal example: function add_all_external_services() { ...
user avatar
  • 213
0 votes
0 answers
30 views

when don't I give the argument to option, option is not working

example code: function purge() { delete_dir; case $purge_arg in --static) delete_static_dir; ;; esac } while getopts ":nrp:h" opt; do case ${opt} in h ) ...
user avatar
  • 184
1 vote
1 answer
1k views

getopt - push default value if the argument is not provided

Im following this guide to using getopt. #!/bin/bash ALPHA=test BETA=test CHARLIE=test DELTA=test usage() { echo "Usage: alphabet [ -a | --alpha ] [ -b | --beta ] [ -c |...
user avatar
1 vote
0 answers
272 views

getopt - how to avoid read next flag as argument?

When I use getopt and pass the required flags with empty argument, it reads the next flag as an argument. Need assistance to add some sort of check to distinguish between flag containing dash (-) and ...
user avatar
  • 49
2 votes
2 answers
973 views

Parsing script arguments after getopts

I have a script where I've implemented switches using getopts. However, I'm having trouble referencing the next argument. My script is for backporting a backup of our website on a local development ...
user avatar
  • 13.3k
1 vote
2 answers
1k views

Proper way to code dry run option without having to repeat myself?

I'm coding a script that goes and searches for files on a remote server and transfers them back to my local computer. I want to be able to do a dry run first, so I know which files I'm bringing back. ...
user avatar
  • 191
2 votes
2 answers
634 views

How to correctly identify the order of parameters?

This is my assignment. The task is to output n longest lines from the input file(s). If there is no argument for n, the the default of value n is 5. If there is no files in the parameter, the standard ...
user avatar
  • 75
0 votes
2 answers
556 views

Set shell variable then invoke subprocess fails

I was playing with getopts, which has a shell variable OPTERR that influences its behavior. I wanted to change the value of OPTERR and invoke getopts on the same line, to affect the behavior of ...
user avatar
  • 121
3 votes
1 answer
356 views

Unexpected behavior from getopts

Considering: #!/bin/sh while getopts ":h" o; do case "$o" in h ) "Usage: sh $(basename "$0") -h Displays help message sh $(basename "$0") arg Outputs ... where: -...
user avatar
  • 481
1 vote
1 answer
1k views

Bash getopts take more positional parameters after the first argument

So I don't know how to make getopts so that it takes every positional parameter after the argument. What I mean by this, is the following: this is my example code: #!/bin/bash while getopts 'a:b:' ...
user avatar
0 votes
3 answers
1k views

Call a function with an argument inside a function that needs another argument

I don't know how else to word this so I'll do my best to explain. I have a function main() { if [[ "$1" = "-f" ]] then ($sendFirstEmail) "$1" elif [[ "$1" = "...
user avatar
1 vote
1 answer
407 views

getopts in function that is called more than once in a script, getopts doesn't detect any opts after 1st function call [duplicate]

I have read the getopts man page and am still not sure about this use case. getopts is not detecting any available options the second time a function is called in the same script. As you can see ...
user avatar
1 vote
1 answer
760 views

How do I pass long-style command line options to a bash script using getopt?

I have this script that I have cobbled together from bits of lore I've gleaned from googling: #!/usr/bin/env bash usage() { declare -r script_name=$(basename "$0") echo """ Usage: "${...
user avatar
-1 votes
1 answer
413 views

Bash Script Wind Chill using getopt and bc

I am now getting the correct output, however it is not scaling to only 3 places. Anyone know how to do that? function windchill { to=32 v=10 vel=`echo 'e(l('$v')*0.16)' | bc -l` w=$(...
user avatar
3 votes
1 answer
3k views

getopts does not seem to work

I am trying to run the following script using getopts to parse the options but it does not seem to work: #!/bin/bash set -x echo $@ while getopts "rf" opt do case "${opt}" in r) ropt=${...
user avatar
  • 359
0 votes
1 answer
2k views

shift in getopts loop - clarification needed

I would be glad if someone clarified the need to use shift in this simple parser code: while getopts ":hp:" option do case "${option}" in p) some_parameter=$...
user avatar
0 votes
2 answers
265 views

Questions about understanding a template of using bash's getopts

A stackoverflow post has a template for handling command line arguments. Does the test [ $# == 0 ] mean that a bash script shouldn't be run without any argument? As a template, I think that scripts ...
user avatar
  • 90.7k
1 vote
2 answers
787 views

In a shell script, process options like -a -b but leave --abc alone?

So I have a shell script that does some preparation and then runs a utility. The preparation can be affected by switches like -a or -n. The contents of the command line that are not such switches are ...
user avatar
0 votes
2 answers
3k views

GETOPTS parse empty and nonempty args

I am trying to make script that has two switches -h and -d, -d having a mandatory number argument. After it there will be undetermined number of paths to file. So far, I have this, but the code seems ...
user avatar
  • 103
1 vote
1 answer
1k views

Trying to get getopts to call a function [duplicate]

I'm trying to get it to call a function. Here is my code #!/bin/bash while getopts ":a:b:" opt; do case $opt in a) my_function "%e" ;; b) ...
user avatar
1 vote
2 answers
7k views

Handling unused getopts argument (are options not mandatory?)

I have a script that starts with getopts and looks as follows: USAGE() { echo -e "Usage: bash $0 [-w <in-dir>] [-o <out-dir>] [-c <template1>] [-t <template2>] \n" 1>&2;...
user avatar
  • 199
0 votes
1 answer
53 views

Submitting HPC jobs within an HPC job

I have a large script which relies on input arguments (with getopts). One of these arguments is a directory containing files (all named *bam) This script has 2 parts: Part1: based on input *bam ...
user avatar
  • 199
0 votes
1 answer
107 views

Pass params for getopt from a script that does not use getopt

I have a script (let’s call it scriptC) that uses getopt to parse short and long options and works fine. This script is being called like this: scriptA runs scriptB which calls scriptC with the ...
user avatar
  • 1,195
7 votes
2 answers
2k views

Which is the more standard package, getopt or getopts (with an "s")?

The Linux foundation list of standard utilities includes getopts but not getopt. Similar for the Open Group list of Posix utilities. Meanwhile, Wikipedia's list of standard Unix Commands includes ...
user avatar
  • 201
2 votes
1 answer
21k views

how to properly parse shell script flags and arguments using getopts

I'm using this : for example ./imgSorter.sh -d directory -f format the scripts' content is : #!/bin/bash while getopts ":d:f:" opt; do case $opt in d) echo "-d was triggered with $...
user avatar
  • 123
3 votes
1 answer
258 views

bash function arguments strange behaviour

I have the following bash function: lscf() { while getopts f:d: opt ; do case $opt in f) file="$OPTARG" ;; d) days="$OPTARG" ;; esac done echo file is $file echo days is $...
user avatar
  • 400
3 votes
1 answer
527 views

What is the behavior of `getopts` when it meets `--`?

By convention -- signals that there is no more options after it. It seems to me that when using getopts with case clause, -) pattern subclause doesn't match --. So what is the behavior of getopts ...
user avatar
  • 90.7k
11 votes
2 answers
8k views

What is the purpose of the very first character of the option-string of getopts being a : (colon)?

In option string when using getopts, from http://wiki.bash-hackers.org/howto/getopts_tutorial If the very first character of the option-string is a : (colon), which would normally be nonsense ...
user avatar
  • 90.7k
0 votes
1 answer
727 views

When using `getopts` with `case`: `*)` as the last pattern subclause, or `\?)` and `:)` as the last two pattern subclauses?

When using getopts with case clause, is a *) pattern subclause as the last pattern subclause equivalent to the union of \?) and :) pattern subclauses as the last two pattern subclauses? Specifically, ...
user avatar
  • 90.7k
4 votes
2 answers
449 views

bash script: incorrect argument value being set

Problem I have a script that accepts a few different (optional) command line arguments. For one particular argument, I'm getting the value "less" appear but I don't know why. Bash Code while ...
user avatar
  • 615
1 vote
2 answers
440 views

Is `getopts` only useful when all options are provided upfront?

I'm trying to see whether I can use getopts for my Bash script. However, I'm not sure what's wrong: #! /bin/bash while getopts "a:b" opt ; do case $opt in a) A_OPTION="option ...
user avatar
  • 1,291
0 votes
1 answer
235 views

Ksh built-in getopts not executing the correct case block for an option

when i run below code with option p and no parameters, it is going to other options where as it should execute the print statement of case p ). what needs to be fixed Also, i want the code to execute ...
user avatar
2 votes
1 answer
1k views

getopts does not match the second argument

I'm trying to get a script to: set a variable with -q option show help for -h option, and fail for other options -*, but allow positional arguments Here is the getopts snippet I'm using: while ...
user avatar
1 vote
2 answers
65 views

Bash: Help honing a custom function

I'm learning Bash, and I've written a basic function: wsgc () { # Wipe the global variable value for `getopts`. OPTIND=1; echo "git add -A"; while getopts m:p option; do ...
user avatar
3 votes
1 answer
8k views

bash --- parse flags and an expected (non-optional) argument

$ bash file.sh expected_arg -f optional_arg I've seen posts that address using $1 to grab the first argument and posts that address using getopts for parsing flag arguments. I haven't seen a post ...
user avatar