Are there some built-in tools that will recognize -x
and --xxxx
as switches, and not arguments, or do you have to go through all the input variables, test for dashes, and then parse the arguments thereafter?
| ||||
feedback
|
Unfortunately there really is no simple way to do this in a shell script. There are various
See also this | |||||
feedback
|
I assume you're using bash or similar. An example:
| |||||
feedback
|
You have to write a cycle to parse the parameters. Indeed you can use This is a simple example from
| |||
feedback
|
I wrote a script recently for work that was versatile and allowed for multiple kinds of switches in any order. I can't disclose the full script for obvious legal reasons (not to mention I don't have it with me at the moment), but here's the meat of it.. you can put it in a subroutine and call it at the end of your script:
I do recommend limiting your bash script to less than 400 lines/15k characters; my aforementioned script grew past this size and became greatly difficult to work on. I started rewriting it in Perl, which is much better suited for the task. Keep that in mind as you work on your scripts in bash. Bash is great for small scripts and oneliners, but anything more complex and you're better off writing it in Perl. Note, I haven't test the above, so it probably doesn't work, but you get the general idea from it. | |||||||||
feedback
|