Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [getopts]

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

0
votes
1answer
63 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
70 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
624 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
103 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
80 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
153 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
351 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
137 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
764 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
19 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
39 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 ...
6
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 ...
0
votes
1answer
2k 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
76 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
354 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 ...
1
vote
2answers
506 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
75 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
173 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 ...
0
votes
1answer
220 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
128 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
508 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
53 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
5k 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 ...
3
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
1k 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
285 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
177 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
408 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
481 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
495 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
161 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
132 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
49 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
1k 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
6k 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
871 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
1k 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
766 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
4k 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
567 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....
2
votes
1answer
405 views

how to pass a mixture of arguments to a script and only of them optional -argument

I've been exploring this issue for longer than it should take me and finding getopts a very confusing tool. All I want to do is the following. Have a script that I can pass arguments like this $1 $2 ...
1
vote
1answer
362 views

deny use of multiple getopts arguments

I'm trying to figure out a way to deny the usage of more than one getopts args in a certain situation. Say we have something like this: while getopts "a:b:c:def" variable; do case $variable in a)a=$...
3
votes
2answers
483 views

Can a bash array be used in place of eval set — “$params”?

I'm taking a look at the optparse library for bash option parsing, specifically this bit in the generated code: params="" while [ $# -ne 0 ]; do param="$1" shift case "$param" in --my-...
1
vote
1answer
498 views

Difference between $OPTIND and $#

Hey is there any difference between $OPTIND and $#? Is there a certain reason for that you use $OPTIND with getopts, not $#?
2
votes
3answers
190 views

How to pass args into scripts?

I am trying to pass arg to this scripts but it does not work anyone knows how fix this ? and I do not want to use args i want to use getopts For This part # Get command line parameters i= a= y= ...
7
votes
4answers
5k views

Bash function with `getopts` only works the first time it's run

I defined the function f in Bash based on the example here (under "An option with an argument"): f () { while getopts ":a:" opt; do case $opt in a) echo "-a was triggered, ...
2
votes
1answer
416 views

Provide three argument to one option using getopts in bash [duplicate]

I am trying to process a command line using getopts in bash. I have to pass three arguments after -w option. For example, -w 1 do loop. It should print one line before and after each line from loop ...
1
vote
2answers
3k views

How to extract unknown arguments within a shell script?

I have a shell script that accepts a variety of options, some with arguments, some without, some short, some long. It should handle some of these options itself and pass on the rest it does not know ...
2
votes
3answers
3k views

Command line options with argument in shell script

I have a script to get ldap user name, email and mobile number: #!/bin/bash echo -n "Enter Unix id > " read UNIXID ldapsearch -x "(cn=$UNIXID)" | awk '/givenName/||/mobile/||/mail/' Here is the ...
1
vote
1answer
592 views

How can I detect that not enough options were passed with getopts

I want to add a line of code that tells the user that enough arguments were not given (may be an error message somewhere. but i am not sure where?) blastfile= comparefile= referencegenome= ...