Questions tagged [getopts]

getopts is a Bourne/POSIX shell builtin for parsing command-line options, available in ash, bash, dash, ksh, zsh, ... on Linux and other Unix systems.

getopts is a shell builtin for parsing command-line options of the form -a where a is a letter or other character. It is available in all Bourne-style shells (Bourne, ash, bash, dash, ksh, zsh, ...) and defined by the POSIX standard.

Using getopts usually involves using a loop calling getopts OPTSTRING varname, in which each call to getopts will parse the next option, set varname with the option name and return success.

Example

#!/bin/sh

while getopts a:b flag
do
    case $flag in
        a) echo "a flag used, with value $OPTARG" ;;
        b) echo "b flag used" ;;
        ?) echo "Usage: PROGRAM [-a ARG1] [-b]"; exit 1 ;;
    esac
done
shift $((OPTIND - 1)) # remove parsed args from the arglist
echo remaining args: $*

Documentation

444 questions
-1
votes
1 answer

Does not work 'WHILE GETOPS' in BASH script

I have a script with two parameters, host and domain name of this host. Look: #!/bin/bash data=$(date +%Y.%m.%d) log=/var/log/upgrade_old_ALLOS.log.$data function show_help { echo "Usage: $0 -h host -d dns" echo "For example:…
Piduna
  • 609
  • 12
  • 28
-1
votes
1 answer

How can I create shell command in linux like this: hh.sh filename -e -c?

The options are following the filename rather than hh.sh. And the following code does not work. while getopts ":ec" opt; do case $opt in e) eflag=1 ;; c) cflag=1 ;; esac done shift $(($OPTIND - 1))
WB Lee
  • 641
  • 1
  • 6
  • 10
-1
votes
2 answers

Options not parse as Argument in Unix

I has an existing script which works on $1 $2 $3, etc. I want to add an optional argument without disturbing the order. Do we have some ready made solution for: If a valid option (using getopts) is found, this index of argument should not be…
Sandeep Jindal
  • 14,510
  • 18
  • 83
  • 121
-1
votes
1 answer

When I use getopts in a Bash function, why does it ignore arguments on the second call?

Below is my code function waitTillGWState { if [[ $# -lt 6 ]] ; then echo "$FUNCNAME:: Wrong parameter specified. Exiting." exit 1 fi local Fname1 local fset local state local lhost echo "$# $@" waitTillGWState_Usage() { echo…
Abhishek Dave
  • 699
  • 1
  • 8
  • 21
-2
votes
1 answer

User specified errors with getopts colon feature

I want the ability to specify whether to use Normal Error Reporting or Silent Error Reporting provided with getopts. Then I would be able to call my bash function like this mari -s -n VAL -z VAL The -s would set Silent Error Reporting (i.e. set…
Dilna
  • 405
  • 1
  • 6
-2
votes
2 answers

Bash. getopts command. option that will do something at the end of the program

Problem: If my program takes option '-v' it should print "Good bye!" at the end of the program. I can do this, but it will print it at the beginning. So I need some command that will execute it in the end, if any. Or how can I do this? getopts…
AnorLondo
  • 37
  • 1
  • 7
-2
votes
1 answer

Use getopts like getopt on shell

system: freebsd 9.2 I want to use getopts to parse command line. Such as ./tesh.sh -o good a.c can get good.out ./tesh.sh -o a.c can get a.out But in getopts there is no "option" variable. Does anyone know how to solve this problem? I…
-2
votes
1 answer

How to create a flag with getopts to run a command

I need help with my getopts, i want to be able to run this command ( mount command) only if i pass a flag ( -d in this case). below output is what i have on my script but it doesn't seem to work. CHECKMOUNT=" " while getopts ":d" opt do case…
user1477324
  • 25
  • 1
  • 5
-3
votes
1 answer

Fill in bash array with script arguments

I got a request to pass a list of ip's as an array in a bash script. For example: ./myscript.sh 192.168.0.1,192.168.0.10,192.168.0.15...... The ip's in the above parameter should properly populate the array present in the bash script. I would like…
JUAmaned
  • 49
  • 1
  • 5
1 2 3
29
30