I wrote bash script, in which I included some logging, to see what is going on in each step of exectuion.
Now, I can split those logs in debuggin info and user info (that something has completed, etc.). So I’d like to have some flag parameter, like --verbose
, which I saw in some other bash functions to enable full logging and usage was like:
some_function --verbose
or
some_function -v
I call it flag parameters and don’t know what’s the right name, thus I can’t find anything useful.
How to define such parameters for bash script?
2
Answers
For now, I used workaround and take it as normal positional parameter (as
$n
). To be exact, I have list of four parameters, so I collect this flag like this:So, if parameter is not matching a flag, then I leave it empty and if I want to use it, I just compare it against empty string.
There's one big disadvantage for me though: it has to be at 4th position in parameters list.
Case suits better for this
Same can be done in function, but note that in this case it’ll process parameters passed to function
some_function -v
.Then somewhere in script you can check if
verbose
is set