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
0
votes
1 answer

getopts & preventing accidentally interpreting short options as arguments

Here's a toy example that shows what I mean: while getopts "sf:e:" opt; foundOpts="${foundOpts}${opt}" ; done echo $foundOpts problem is that getopts isn't particularly smart when it comes to arguments. for example, ./myscript.sh -ssssf will output…
user3534080
  • 1,201
  • 1
  • 14
  • 21
0
votes
2 answers

How can i read flags after positional arguments in bash script

How can I able to read the flags after the positional arguments. echo $1 echo $2 while getopts "j:" opt; do echo $opt $OPTIND $OPTARG done $ bash test.sh arg1 arg2 -f flag1 // out: arg1 arg2 Not able to get the flag. But if i…
m9m9m
  • 1,655
  • 3
  • 21
  • 41
0
votes
1 answer

getopts to get multiple values for same argument

I am looking to get multiple values from same argument using getopts. I want to use this script to ssh and run commands on a list of hosts provided through a file. Usage: .\ssh.sh -f file_of_hosts.txt -c "Command1" "command2" Expected output: ssh…
Noah
  • 35
  • 4
0
votes
1 answer

Bash getopts differentiate "-?" from invalid options

I want getopts to recognize when a user passes "-?" and "-h" and send to my help function. I also don't want getopts to send the user to the help function if an invalid option is presented. I want a different message to display telling them it was…
Dave
  • 727
  • 1
  • 9
  • 20
0
votes
2 answers

Bash getopts to remotely run the command via SSH

I am trying to write a bash script which will take the input in form of arguments and execute certain commands (provided as arguments) on the remote hosts which will be provided via txt file For eg: there is text file /var/tmp/hosts.txt which have…
Noah
  • 35
  • 4
0
votes
0 answers

How to use shortopts in getopt() in pyhton

I'm having trouble understanding the meaning of semi-colons in shortops. I have such a longopt ["help", "runNum=","allplots=","sys=","redblue=","signalinj=","ranking="] and I assign values to parameters with try: opts, args =…
mrq
  • 274
  • 1
  • 2
  • 14
0
votes
0 answers

Command not found on a funciton calling from another

i've tried to figure our , what wrong with my script , i tried using the below syntax for function fucntion name{ } , and also example(){} , both results creates errors , please advise what's wrong with my script # Objective : automation script for…
0
votes
1 answer

How can I get filename and argument with getopts from Command

Here is the code, I want to run this command Getting the test.log input, then convert it to text type, then store it on the output.txt file ./small.sh test.log -t text -o output.txt #!/usr/bin/env bash usage() { echo "$0 usage:" && grep " .)\ #"…
drowsyone
  • 27
  • 6
0
votes
0 answers

how to specify OPRARG only for a specfic option , when using getopts

i'm trying to solve this problem. i've a script that it's goal is to create a random password , afterwards if requested encrypt it / decrypt it. the main problem is : that the random function work perfectly with OPTARG , but the rest of the…
0
votes
1 answer

How do I iterate over bash script flags and use them?

I have a code that I wrap with a bash script, and I want to know if a certain flag was given (-b), and if so, to update some variable I have in the script (called "x"). Unfortunately, I need this to be done on the bash script, and the synthax here…
Gal
  • 45
  • 6
0
votes
1 answer

bash getopts uses option/flag as argument

I'm writing a bash shell script that can use the flags -l -u and -w [argument] I have the following (simplified) code: while getopts ":ulw:" arg; do case "$arg" in u) U=1 ;; l) L=1 ;; w) W=1 …
0
votes
1 answer

getopts in bash scripts

I am trying to understand a piece of bash script that uses getopts. I have #!/bin/bash function Argparser () { while getopts 'o:dfath' arg "$@"; do case $arg in 'o') echo "oooooh" …
KansaiRobot
  • 7,564
  • 11
  • 71
  • 150
0
votes
0 answers

Issues storing arguments from getopts in shell script

I am new using unix and I recently tried to create a code that would take some arguments and pass it to a python script and later use those arguments for downstream processes. I am trying to use getopts but after I tried running the script it looks…
0
votes
1 answer

Standard GNU-style keyword options

The book "The Art of UNIX Programming" by Eric Raymond has a section entitled "The -a to -z of command line options", in which he enumerates a "semantic standard" of what various single-letter flags are expected to mean. For example -a usually means…
NetHead
  • 71
  • 1
  • 10
0
votes
0 answers

How to accept the full command

How do I make the below script to accept the first command as one instead of treating the second character as an argument? When I execute this script using ./scriptname -i hello it works as expected but when I use something like ./scriptname -in…
user96931