Bash argument analysis tools

I have a bash script that uses several variables (name them $fooand $bar). Right now, the script defines them on top with hard-coded values, such as:

foo=fooDefault
bar=barDefault

....

# use $foo and $bar

I want to use a script like any of them:

myscript              # use all defaults
myscript -foo=altFoo  # use default bar
myscript -bar=altBar  # use default foo
myscript -bar=altBar -foo=altFoo

An ideal solution would allow me to simply list the variable that I want to check for flags.

Is there a good way to do this?


I saw getopt, and I think that this can do about 70% of what I'm looking for, but I'm wondering if there is any tool or india that builds on it or the like that does the rest.

+3
source share
1 answer

eval :

foo=fooDefault
bar=barDefault

for arg in "$@"; do
    eval ${arg}
done

, :

myscript foo=newvalue

. eval, /​​, , script. , ${arg} , eval.

, , , ( eval):

myscript foo='"with sapces"'
+1

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


All Articles