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

Can't locate getopts.pl with perlbrew

My system have Perl 5.10.1 by default.I am able to execute the perl script successfully with it. I installed Perl 5.16.3 and 5.30.1 using perlbrew. When I use these versions with perlbrew and execute ...
0
votes
0answers
26 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 ...
1
vote
2answers
86 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 ...
0
votes
2answers
71 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
2answers
65 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
2answers
105 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
1answer
131 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
1answer
65 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
3answers
96 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
1answer
81 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 ...
0
votes
1answer
239 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
1answer
180 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=$(...
3
votes
1answer
1k 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=${...
0
votes
1answer
352 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=${OPTARG} ...
0
votes
2answers
170 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
vote
2answers
484 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
2answers
1k 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
1answer
367 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) ...
1
vote
2answers
2k 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
1answer
26 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
1answer
60 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 ...
7
votes
2answers
1k 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 ...
1
vote
1answer
7k 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 $...
2
votes
1answer
119 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 $...
3
votes
1answer
390 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 ...
2
votes
2answers
2k 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 ...
0
votes
1answer
173 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, ...
4
votes
2answers
215 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 ...
1
vote
2answers
325 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 ...
0
votes
1answer
193 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 ...
2
votes
1answer
656 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 ...
1
vote
2answers
55 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 ...
1
vote
1answer
6k 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 ...
4
votes
2answers
2k views

Bash: Why is used eval and shift in a script that parses command line arguments?

As I was looking this answer https://stackoverflow.com/a/11065196/4706711 in order to figure out on how to use parameters like --something or -s some questions rised regarding the answer's script : #!...
0
votes
1answer
2k views

Does getopts read in command line arguments in some order?

Bash manual says getopts optstring name [args] When the end of options is encountered, getopts exits with a return value greater than zero. OPTIND is set to the index of the first non-...
0
votes
1answer
352 views

What happens to getopts when the end of options is encountered

Bash manual says getopts optstring name [args] When the end of options is encountered, getopts exits with a return value greater than zero. OPTIND is set to the index of the first non-...
0
votes
1answer
277 views

Trying to use if else of getopts to pass an argument from the terminal and capture that information in a log

I am trying to pass an argument from a command string in the terminal. I am modifying a script to detect that command and allow the user to see what the script is doing. This is what I am trying to ...
0
votes
2answers
527 views

How to output piped stdout in bash script from getopts?

I have the following snippet: #!/bin/bash OPTIND=1 while getopts ":m:t" params; do case "${params}" in m) bar=$OPTARG ;; t) foo=$OPTARG ;; \?) ...
4
votes
2answers
677 views

bash case && extra OPTARG

I ran into an interesting scenario last night and, so far, my google foo has been unable to find a work around. I have a script that supports a number of arguments. A user (damn those users) didn't ...
0
votes
1answer
637 views

In a bash script, how may I use “tr -d” to delete a user entered char?

#!/bin/bash while getopts ":r" opt; do case $opt in r) [ -f "$1" ] && input="$1" || input="-" read $userinp cat $input | tr -d "$userinp" ;; esac done That is my ...
0
votes
1answer
167 views

getopts Unix input

so in my code below I'm like trying to get my code to read the input that the user enters in my code as seen below #./MyProject -a -b OR -b -a However I keep getting a Syntax error and its not ...
-3
votes
1answer
161 views

What's wrong with getopts or grep here

#!/bin/bash #return codes: STATE_OK=0 STATE_WARNING=1 STATE_CRITICAL=2 STATE_UNKNOWN=3 if [ $# -lt 4 ] then echo "UNKNOWN: Please, Pass Enough Parameters" ...
1
vote
1answer
54 views

parsing getopts

I want to escape the first string SOMETEXT in the getopts args. But I'm only able to do it in the first example. How can I make it work on the second example? while getopts p: opt do case $opt in ...
1
vote
2answers
2k views

How can options be parsed in a Bash script, leaving unrecognized options after the “--”?

I'm looking for a way to do option parsing in a Bash script (allowing for both short and long arguments as getopt does) that stops parsing at the first unrecognized argument, places a -- before that ...
3
votes
1answer
7k views

How to parse Command Line Arguments with arbitrary string

I'm trying to create a script that has an option that will contain arbitrary text (including spaces) surrounded by quotes and this is proving difficult to search for and implement. Basically the ...
0
votes
3answers
1k views

using single dash with long name [closed]

I know that a single dash goes with one character when we set options using getopt or optparse. but is there anyway to use a single dash with more than one character, like -apply? I know that a ...
3
votes
2answers
2k views

KSH - built-in getopts unknown option argument value

I'm trying to use the ksh built-in getopts to manage runtime options for my ksh code. I keep on getting the error: "unknown option argument value" when using an option that requires and argument. ...
1
vote
3answers
984 views

Why this while-case does not work in Ubuntu?

I am switching away from BSD to completely Linux. Script in Ubuntu 16.04 #!/bin/sh while (( "$#" )); do case "$1" in -i | --ignore-case) [ $# -ne 2 ] && echo "2 arguments i ...
4
votes
5answers
6k views

Execute default option when no options are specified in getopts

I followed the tutorial here to learn how to use getopts. I'm able to execute all the options properly provided by the user. But now I want to execute a default option when none of the options are ...
3
votes
1answer
711 views

sed whole string replacement not working

I have two scripts ins.sh and variable.sh. variables.sh holds various key-value pairs. ins.sh #!/usr/bin/env bash set -e # Exit upon error # This script generates a 64-bit system source variables....