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

learn more… | top users | synonyms

1
vote
2answers
126 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
139 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
42 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= ...
1
vote
1answer
78 views

How to have getopts functionality when just calling another function within the file

I'd like to have named parameters for my functions. I seem to only be able to use GETOPTS for the main function called from the command line. If I have multiple functions within one file is there ...
2
votes
3answers
110 views

How do I use inserted values using getopts

I have this bash code combined with getopts and if I understood getopts correctly OPTIND contains the index of the next command line option and all the command line options provided to shell script ...
1
vote
1answer
89 views

getopts passing value of declared parameter to function

I have function port.sh as stand alone script and i'm wondering if is possible to put this function in the same script where getopts is and pass value of OPT_B into function and get output of it? ...
3
votes
1answer
58 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 ...
1
vote
1answer
137 views

Calling bash getopts from a function fails the 2nd time

I'm trying to parse arguments passed to a bash function using getopts inside that function. It works when the function is called for the first time but fails on all subsequent calls. Here is a simple ...
2
votes
1answer
90 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), ...
5
votes
1answer
2k 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 ...
2
votes
1answer
2k views

Using getopts to parse options after a non-option argument

I have a bash script as below in a file nepleaks_upd.sh, that I want to run as ./nepleaks_upd.sh bootstrap --branch off. Couldn't make it to take --branch , but what it works with is ./nepleaks_upd.sh ...
0
votes
0answers
51 views

how to pass asterisk as a parameter to getopts [duplicate]

I wrote a shell scripts with getopts to take arguments. But I want to pass an asterisk as an argument, for example script -a"Hello * all" -b"Another argument" if I echo the -a argument from the ...
0
votes
2answers
187 views

Alternate option field

In a script i have a list of options that needs to be entered by the user. They are -c cell name -> it must be there -n node -> it must be there -s server -> it must be there Uptill here its all ...
0
votes
2answers
129 views

How to input two arguments with getopts

Hi friends I am writing a simple bash script; My script installs ppa. The problem is I can't add two arguments. I want write a simple: ./ppa.sh -i ppa:chris-lea/node.js nodejs I tried it; but ...
1
vote
1answer
800 views

getopts - how to avoid read next $OPTARG as argument?

Just started use getopts and found one problem... For example - have a script with: while getopts "h:loav" opt; do case $opt in h) h=$OPTARG echo $h ;; But whet I run it: $ ./ftpclean.sh -h ...
3
votes
1answer
520 views

How do I fix the argument received from getopt?

I have the following commands in my script: set -- `getopt -q agvc:l:t:i: "$@"` ... while [ -n "$1" ] do -i) TIME_GAP_BOOT=$2 shift ;; ... sleep $TIME_GAP_BOOT When invoking the script ...
2
votes
2answers
597 views

shell script options pass through to sub-command

I'm writing a wrapper script for that uses getopts to set various options for mysqldump. It's working well so far. However one thing I would like to do is be able to add another option in my shell ...
4
votes
5answers
5k views

bash getopts, short options only, all require values, own validation

I'm trying to build up a shell script that accepts various options and getopts seems like a good solution as it can handle the variable ordering of the options and arguments (I think!). I'll only be ...
3
votes
3answers
502 views

Including sub-parameters in help options to execute wisely without getopt or getopts?

I am writing a script which can choose a file and print specific content. For example, san#./script.sh Expected Usage : ./script.sh --file1 --dns (Here it checks for file1, search for dns name and ...
1
vote
1answer
2k views

Multiple option arguments using getopts in bash

I am trying to process command line arguments using getopts in bash. One of the requirements is for the processing of an arbitrary number of option arguments (without the use of quotes). 1st example ...
2
votes
1answer
291 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 ...
10
votes
3answers
12k 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 ...
2
votes
2answers
3k views

How to deal with end of options — in getopts

I use getopts to parse arguments in bash scripts as while getopts ":hd:" opt; do case $opt in d ) echo "directory = $OPTARG"; mydir="$OPTARG"; shift $((OPTIND-1)); OPTIND=1 ;; h ) helptext ...
4
votes
2answers
14k 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';; ...
3
votes
2answers
346 views

how to make getopts just read the first character post `-`

I have a shell script testShell.sh which uses getopts as below: #!/bin/bash while getopts ":j:e:" option; do case "$option" in j) MYHOSTNAME=$OPTARG ;; e) SCRIPT_PATH=$OPTARG ;; ...
3
votes
2answers
1k 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]. ...
10
votes
4answers
9k views

How do I handle switches in a shell script?

Are there some built-in tools that will recognize -x and --xxxx as switches, and not arguments, or do you have to go through all the input variables, test for dashes, and then parse the arguments ...
3
votes
1answer
3k 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 ...
5
votes
3answers
730 views

How to specify -? option with GNU getopt

When parsing command line arguments with GNU getopt command, how do I (if possible) do recognize -? as another option? Is there a way to escape it in the opstring?
3
votes
2answers
6k 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 ...