Skip to main content
The 2024 Developer Survey results are live! See the results

All Questions

Tagged with
-1 votes
1 answer
329 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 ...
Vlastimil Burián's user avatar
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: "${...
braveterry's user avatar
2 votes
1 answer
25k 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 $...
St3an's user avatar
  • 123
3 votes
2 answers
1k 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-...
Wildcard's user avatar
  • 36.8k
2 votes
3 answers
6k 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 ...
XZS's user avatar
  • 1,448
4 votes
1 answer
519 views

Why do some utilities parse operands before options?

According to several sources, the UNIX utility guidelines specify that operands should always be processed after options: utility_name[OPTIONS][operands...] Some older UNIX utilities are known to ...
j--'s user avatar
  • 437
3 votes
1 answer
2k views

Strange leading whitespace in OPTARG when using getopts

I spent quite a while researching the problem I encountered but none of the getopts tutorial say anything about the leading whitespace in OPTARG when using getopts. In bash(on Ubuntu and OSX), ...
GJ.'s user avatar
  • 153
15 votes
1 answer
52k views

Provide two arguments to one option using getopts

In below code when I give option r then getopts requires one arguments: while getopts ":hr::l:" opt; do case $opt in r ) echo "Run Numbers - argument = $OPTARG " ;; l ) echo "...
ramkrishna's user avatar
2 votes
2 answers
1k views

Simpler processing of shell script options

I'm looking for way to process shell script arguments that is cleaner and more "self documenting" than getopt/getopts. It would need to provide... Full support of long options with or without a ...
DocSalvager's user avatar
  • 2,262
74 votes
5 answers
64k views

getopt, getopts or manual parsing - what to use when I want to support both short and long options?

Currently I'm writing a Bash script which has the following requirements: it should run on a wide variety of Unix/Linux platforms it should support both short and (GNU) long options I know that ...
helpermethod's user avatar
  • 2,002
25 votes
6 answers
119k views

How can I detect that no options were passed with getopts?

I have this code - #getoptDemo.sh usage() { echo "usage: <command> options:<w|l|h>" } while getopts wlh: option do case $option in (w) name='1';; ...
Hussain Tamboli's user avatar
7 votes
2 answers
7k views

Getopts option processing, Is it possible to add a non hyphenated [FILE]?

I'm using getopts for all of my scripts that require advanced option parsing, and It's worked great with dash. I'm familiar with the standard basic getopts usage, consisting of [-x] and [-x OPTION]. ...
J. M. Becker's user avatar
  • 4,949
26 votes
4 answers
51k views

How do I handle switches in a shell script?

Are there some built-in tools that will recognize -x and --xxxx as switches (flags or "boolean options", rather than "ordinary arguments"), or do you have to go through all the ...
user394's user avatar
  • 14.5k
3 votes
1 answer
5k views

How to run a specified codeblock with getopts when no options or arguments are supplied?

So I am writing a script that mixes options with arguments with options that don't. From research I have found that getopts is the best way to do this, and so far it has been simple to figure out and ...
MaQleod's user avatar
  • 2,644
3 votes
2 answers
8k views

How to catch optioned and non optioned arguments correctly?

I want to write a shell script which will take some arguments with some options and print that arguments. Suppose the name of that script is abc.ksh. Usage of that script is - ./abc.ksh -[a <arg>...
chanchal1987's user avatar