Questions tagged [getopts]
`getopts` is a shell built-in used to parse command line options, while `getopt` is its non-built-in counterpart.
113
questions
-1
votes
1
answer
60
views
What does colon (:) mean for getopts?
I am trying to parse options for my script with getopts and I decided that it would be best to read up on it in the POSIX standard, as it's usually extremely helpful. The DESCRIPTION section is fairly ...
0
votes
0
answers
31
views
Is there a tool or macro processor capable of compactly expressing BASH CLI interfaces?
I am writing scripts with the following boilerplate CLI pattern:
usage(){
echo "$0 yada yada"
...
echo
}
for arg in "$@"; do
case "$arg" in
'--help|-help') ...
0
votes
1
answer
45
views
Multichar options in Unix sysytems
I have a usecase wherein I need to support multiple character option. Currently I am using single character option for getopts. Multi-character option is desirable.What's the best way of doing this? I ...
-1
votes
1
answer
108
views
How to check for passed options using getopts in a POSIX shell? (Report, count, and discard.)
How to check for passed options using getopts (man page) in a POSIX shell? (Report, count, and discard.)
My idea is this: Many of my scripts take no options (not even -h for help, I am transitioning ...
-2
votes
1
answer
202
views
The workings of getopts OPTIND
I want to understand how OPTIND works by getopts. If I want to skip the first few positional arguments, how should I set up OPTIND exactly ?
And because OPTIND is not reset automatically, I need to ...
1
vote
0
answers
34
views
What the... Variable String [duplicate]
I believe I am running into a scenario where escaping quotes and utilizing variables as strings is causing some issues and after some hours of troubleshooting this isn't seeming to come to a ...
3
votes
1
answer
184
views
getopts for more than 4 parser arguments
I want to parse multiple arguments using getopts in a bash script using the code below.
while getopts b:B:m:M:T flag
do
case "${flag}" in
b) rbmin=${OPTARG};;
B) rbmax=${...
2
votes
1
answer
4k
views
How to mix plain arguments with flagged arguments in bash scripting?
for instance, gcc accepts the input file without any flag and the output file with the -o flag in:
gcc input.c -o output.out
or
gcc -o output.out input.c
I am creating a random password generator bash ...
2
votes
2
answers
1k
views
Can getopts arguments be combined with other input?
I am writing a script that would take a string as input along with other options the user can select by using arguments as indicators. So in other words, something like this:
./script "My input ...
1
vote
2
answers
211
views
identify if present and remove specific argument from shell args
I have to check if particular argument lets say 'java8' is present in my shell arguments to script and if it is present remove it . Also I want it to be stored in some other variable , but want it to ...
0
votes
2
answers
398
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 ...
0
votes
1
answer
1k
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 ...
4
votes
1
answer
2k
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 ...
0
votes
1
answer
1k
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::")?
1
vote
2
answers
9k
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 "$@...
1
vote
1
answer
225
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. ...
0
votes
3
answers
5k
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 ...
-2
votes
2
answers
1k
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 ...
-2
votes
1
answer
704
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.
2
votes
2
answers
3k
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 ...
0
votes
0
answers
40
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 ""
...
3
votes
2
answers
576
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=...
13
votes
1
answer
31k
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 ...
3
votes
1
answer
1k
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
...
0
votes
2
answers
6k
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) ...
4
votes
1
answer
2k
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() {
...
0
votes
0
answers
69
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 ) ...
1
vote
1
answer
2k
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 |...
1
vote
0
answers
340
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 ...
3
votes
2
answers
1k
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 ...
2
votes
2
answers
2k
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.
...
2
votes
2
answers
1k
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 ...
0
votes
2
answers
923
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 ...
3
votes
1
answer
590
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:
-...
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:' ...
0
votes
3
answers
2k
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" = "...
1
vote
1
answer
676
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 ...
1
vote
1
answer
2k
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:
"${...
-1
votes
1
answer
520
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=$(...
4
votes
1
answer
5k
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=${...
1
vote
1
answer
3k
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=$...
0
votes
2
answers
310
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 ...
-1
votes
2
answers
948
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 ...
0
votes
2
answers
4k
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 ...
1
vote
1
answer
2k
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)
...
2
votes
2
answers
9k
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;...
0
votes
1
answer
68
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 ...
0
votes
1
answer
170
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 ...
10
votes
2
answers
3k
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 ...
2
votes
1
answer
24k
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 $...