Problem With Arguments Test in Bash Script
I'm still a bit of a novice when it comes to bash scripting, and am having problems processing arguments from the command line. I tend to run my script as:
Code:
./myscript file1 file2 or ./myscript action file1 file2 I want to say: (1) if one or more files are listed, with no action, then process with default action (2) if an action is given, along with one or more files, then process the files with this action (3) action, if given must be $1 Can anyone help? My current incarnation looks like this: Code:
if [ -f "$1" -o -d "$1" ] # if this is a file or directory (1) Even though I have checked my arguments here, I still have to strip $1 from those functions that do process the files anyway. (2) The action1 function works great, but the action2 function breaks, even though it processes the files in reverse from action1. This part of action1 and action2 are exactly alike: Code:
action() Thanks! |
Hi,
Quote:
Code:
#!/bin/bash Quote:
Code:
#!/bin/bash Quote:
Hope this helps. |
Many Thanks! Shift works. I thought I was not understanding the use of shift, since I could not get action2 to work for the life of me. Through process of elimination, I now realize the problem lay elsewhere (namely inside action2), specifically the file/directory test. But I have learned a lot about hunting bugs - and processing command line arguments.
|
Quote:
I'd have to say that its too close to call, in general, but that you might find that one approach suits your style more than the other. |
Thanks for that Salasi, as is frequently true, I suppose it's a matter of there being more than one way to skin a cat.
|
Stupid answer:
http://aplawrence.com/Unix/getopts.html http://www.shelldorado.com/goodcoding/cmdargs.html http://www.bash-hackers.org/wiki/dok...topts_tutorial http://bashcurescancer.com/the-60-se...-tutorial.html http://stackoverflow.com/questions/4...d-line-options (It is a stupid answer, because the only thing here is links, and all can be found by the use of a search engine. I still hope that they are valuable, though, because you don't have to sort through all sorts of rubbish to get there.) |
I like getopts because it:
Code:
lf=$'\n' |
I agree that if you have varying nums (& types) of params, then getopts is the way to go
|
I agree, getopts is a good idea, but it does not support that kind of syntax
|
All times are GMT -5. The time now is 08:49 PM. |