getopts

An example of how to use getopts in bash

▼魔方 西西 提交于 2019-12-16 19:51:45
问题 I want to call myscript file in this way: $ ./myscript -s 45 -p any_string or $ ./myscript -h #should display help $ ./myscript #should display help My requirements are: getopt here to get the input arguments check that -s exists, if not return an error check that the value after the -s is 45 or 90 check that the -p exists and there is an input string after if the user enters ./myscript -h or just ./myscript then display help I tried so far this code: #!/bin/bash while getopts "h:s:" arg; do

How to create a flag with getopts to run a command

寵の児 提交于 2019-12-13 11:06:37
问题 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 "$opt" in d) CHECKMOUNT="true" ;; usage >&2 exit 1;; esac done shift `expr $OPTIND-1` FS_TO_CHECK="/dev" if [ "$CHECKMOUNT" = "true" ] then if cat /proc/mounts | grep $FS_TO_CHECK > /dev/null; then # Filesystem is mounted else # Filesystem is not mounted

How to use mutually exclusive flags in your shell and add an optional argument flag ( stuck with getopts)

天大地大妈咪最大 提交于 2019-12-13 02:36:14
问题 I am using a standard getopts logic. But I want to how I can make the options I offer- mutually exclusive. e.g. shell.sh -a SID <accepted> shell.sh -b SID <accepted> shell.sh -ab SID Message- using ab together is the same as running shell.sh without any options supplying just SID . Help usage < ya da ya > shell.sh Please enter SID at the minimum. Usage < ya da ya > shell.sh SID <accepted> I am trying to develop this logic using something like below while getopts ":a:b:" opt; do case $opt in a

Bash long options/flags - how to do it?

こ雲淡風輕ζ 提交于 2019-12-10 19:29:36
问题 I am trying to change my working script with getopts to getopt ( long flags ). Below i present my code which is working. getopts 'm:' mode modeValue=$OPTARG getopts 'p:' parameter parameterValue=$OPTARG getopts 'u:' parameter parameterValue2=$OPTARG getopts 'l:' parameter parameterValue3=$OPTARG getopts 'n:' parameter parameterValue4=$OPTARG getopts 'e:' parameter parameterValue5=$OPTARG getopts 'w:' parameter parameterValue6=$OPTARG getopts 'r:' parameter parameterValue7=$OPTARG case

Using getopts within user-defined-function in bourne shell

爷,独闯天下 提交于 2019-12-10 16:54:45
问题 Is it possible to pass command line arguments into a function from within a bourne script, in order to allow getopts to process them. The rest of my script is nicely packed into functions, but it's starting to look like I'll have to move the argument processing into the main logic. The following is how it's written now, but it doesn't work: processArgs() { while getopts j:f: arg do echo "${arg} -- ${OPTARG}" case "${arg}" in j) if [ -z "${filename}" ]; then job_number=$OPTARG else echo

getopts won't call twice in a row? [duplicate]

依然范特西╮ 提交于 2019-12-09 14:42:11
问题 This question already has an answer here : Why does getopts only work the first time? (1 answer) Closed 9 months ago . For some reason the options work fine the first call of lib_progress_bar -c "@" -u "_" 0 100 , but on the second call and beyond everything is default because it seems like getopts c:u:d:p:s:%:m: flag isn't true the second time around, or atleast the case is never executed when I used set -x #!/bin/bash lib_progress_bar() { local current=0 local max=100 local completed_char="

Having getopts to show help if no options provided

笑着哭i 提交于 2019-12-08 19:25:21
问题 I parsed some similar questions posted here but they aren't suitable for me. I've got this wonderful bash script which does some cool functions, here is the relevant section of the code: while getopts ":hhelpf:d:c:" ARGS; do case $ARGS in h|help ) help_message >&2 exit 1 ;; f ) F_FLAG=1 LISTEXPORT=$OPTARG ;; d ) D_FLAG=1 OUTPUT=$OPTARG ;; c ) CLUSTER=$OPTARG ;; \? ) echo "" echo "Unimplemented option: -$OPTARG" >&2 echo "" exit 1 ;; : ) echo "" echo "Option -$OPTARG needs an argument." >&2

Which module do I need when I got error info “Can't locate getopts.pl in @INC…”

人盡茶涼 提交于 2019-12-06 15:34:23
I run this command in my macOS $ perl ~/Desktop/blif2cnf.pl and got this error info: Can't locate getopts.pl in @INC (@INC contains: /Library/Perl/5.18/darwin-thread-multi-2level /Library/Perl/5.18 /Network/Library/Perl/5.18/darwin-thread-multi-2level /Network/Library/Perl/5.18 /Library/Perl/Updates/5.18.2 /System/Library/Perl/5.18/darwin-thread-multi-2level /System/Library/Perl/5.18 /System/Library/Perl/Extras/5.18/darwin-thread-multi-2level /System/Library/Perl/Extras/5.18 .) at /Users/Frank/Desktop/blif2cnf.pl line 10. In my linux 16.04, such problem can be solved by following this answer

Multiple option arguments using getopts (bash)

微笑、不失礼 提交于 2019-12-04 23:23:54
问题 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 (only grabs the 1st argument) madcap:~/projects$ ./getoptz.sh -s a b c -s was triggered Argument: a 2nd example (I want it to behave like this but without needing to quote the argument" madcap:~/projects$ ./getoptz.sh -s "a b c" -s was triggered Argument: a b c Is there a way to do this? Here's

How do you use getopts?

落爺英雄遲暮 提交于 2019-12-04 15:20:12
what is the easiest, most straight forward, way to use getopts in bash script. if i have a script called: myscript and it CAN take the the arguments: -p -r -s -x if argument x then exit if argument p then echo "port 10" if argument s then add 2+2 if argument r then echo env This is a hypothetical script but I would just like to see an example of how this would be done. while getopts :xpsr opt; do case $opt in x ) exit ;; p ) echo port 10 ;; s ) (( 2 + 2 )) ;; r ) echo env ;; \? ) echo "${0##*/}" [ -xpsr ]; exit 1 ;; esac done Arnaud F. usage() { echo "Usage: $0 [-o <offset>] [-h];" exit 0; } #