Error parsing arguments with getopts in bash

I tried to change the bd script to use getopts. I'm new to bash scripting

my script -

while getopts ":hvis:d:" opt
do
...
done

...

echo $somedirpath
cd "$somedirpath"    

this works great when executed

$ ./bd -v -i -s search

or

$ ./bd -is search -d dir

But when you run this type

$ . ./bd -s search

getoptsdoesn't read arguments at all. And all the variables set in the loop whileaccording to the arguments are not set, so the script no longer works. Please, help!

+4
source share
2 answers

The setting OPTIND=1before the call getoptsworks fine.

, getopts OPTIND, , script - 1 getopts , . script ( ). , , , getopts OPTIND, 1!

, , . !

, @tripleee .

+5

script, , getopts, , source.

, script cd "$(bd)" (, ).

+6

Source: https://habr.com/ru/post/1540031/


All Articles