getopts

Why is OPTIND messing my positional params?

淺唱寂寞╮ 提交于 2020-05-24 06:08:28
问题 I have this function: sgrep () { local OPTIND; if getopts i o; then grep --color=auto -P -in "$1" "$2"; shift $((OPTIND-1)); else grep --color=auto -P -n "$1" "$2"; fi | sed -E -n 's/^([0-9]+).*/\1/p' | xargs -I{} vim +"{}" "$2"; stty sane } It should use grep case sensitive, if it is invoke with -i . But when it is, the it put -i is in place of search string and search string is in place of somefile : $ set -x $ sgrep 'somesearch' 'somefile' ---#output--- + sgrep -i 'somesearch' 'somefile' +

Using getopt_long (C++) how do I code up a long & short option to both require arguments?

若如初见. 提交于 2020-01-12 08:36:29
问题 #include <iostream> #include <getopt.h> #define no_argument 0 #define required_argument 1 #define optional_argument 2 int main(int argc, char * argv[]) { std::cout << "Hello" << std::endl; const struct option longopts[] = { {"version", no_argument, 0, 'v'}, {"help", no_argument, 0, 'h'}, {"stuff", required_argument, 0, 's'}, {0,0,0,0}, }; int index; int iarg=0; //turn off getopt error message opterr=1; while(iarg != -1) { iarg = getopt_long(argc, argv, "svh", longopts, &index); switch (iarg)

getopts no argument provided

拈花ヽ惹草 提交于 2020-01-04 14:08:10
问题 how to check whether there was no required argument provided? I found that ":" option in switch case should be sufficient for this purpose, but it never enters that case (codeblock). It doesn't matter whether I put "colon-case" at the beginning or elsewhere. my code: while getopts :a:b: OPTION; do case "$OPTION" in a) var1=$OPTARG ;; b) var2=$OPTARG ;; ?) exitScript "`echo "Invalid option $OPTARG"`" "5" ;; :) exitScript "`echo "Option -$OPTARG requires an argument."`" "5" ;; *) exitScript "

Using getopts to read one optional parameter placed as final place

拥有回忆 提交于 2019-12-25 10:31:20
问题 I wrote a bash script that takes flexible number of parameters and now I would like to add an optional argument ( -l ) to each of them. I am currently having difficulty getting the desired behavior. I want all of the following to execute correctly: ./Script.sh arg1 arg2 arg3 -l opt ./Script.sh arg1 arg2 arg3 ./Script.sh arg1 arg2 arg3 arg4 -l opt ./Script.sh arg1 arg2 arg3 arg4 arg5 The problem is that $OPTIND cannot be set. The following loop works if the -l opt is placed before first

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

徘徊边缘 提交于 2019-12-23 03:46:19
问题 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

How do you use getopts?

别来无恙 提交于 2019-12-21 20:48:44
问题 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. 回答1: while getopts :xpsr opt; do case $opt in x ) exit ;; p ) echo port 10 ;; s ) (( 2 + 2 )) ;; r ) echo env ;; \? ) echo "$

BASH: getopts with default parameters value

非 Y 不嫁゛ 提交于 2019-12-20 07:27:12
问题 I've got another bash-script problem I simply couldn't solve. It's my simplified script showing the problem: while getopts "r:" opt; do case $opt in r) fold=/dev dir=${2:-fold} a=`find $dir -type b | wc -l` echo "$a" ;; esac done I calling it by: ./sc.sh -r /bin and it's work, but when I don't give a parameter it doesn't work: ./sc.sh -r I want /dev to be my default parameter $2 in this script. 回答1: There may be other parameters before, don't hard-code the parameter number ($2). The getopts

In Bash, why does using getopts in a function only work once? [duplicate]

a 夏天 提交于 2019-12-20 03:10:11
问题 This question already has an answer here : Why does getopts only work the first time? (1 answer) Closed 9 months ago . I'm trying to create a simple function on macOS Sierra that counts the characters in a string. This works fine (added to my bashrc file): function cchar() { str=$1 len=${#str} echo "string is $len char long" } $ cchar "foo" string is 3 char long I'm trying to expand it with a -a option, so I added this to my function (and commented the rest out for testing): while getopts "a:

bash getopts with multiple and mandatory options

六眼飞鱼酱① 提交于 2019-12-17 15:23:08
问题 Is it possible to use getopts to process multiple options together? For example, myscript -iR or myscript -irv. Also, I have a situation where based on a condition script would need mandatory option. For example, if argument to script is a directory, I will need to specify -R or -r option along with any other options (myscript -iR mydir or myscript -ir mydir or myscript -i -r mydir or myscript -i -R mydir), in case of file only -i is sufficient (myscript -i myfile). I tried to search but didn

An example of how to use getopts in bash

你说的曾经没有我的故事 提交于 2019-12-16 19:53:32
问题 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