How to add multiple flags in bash script
Hi Guys,
I got a bash script and I need to add two flags for that. I have already achieved the same with one flag using shift command but how do I do the same thing for 2 flags. my command syntax would be convertRoute -i <input file> -o <output file> Thanks, Manya |
Quote:
|
I tried that using while and shift but dont know whats going wrong. Do you have syntaxes or template codes?
|
Quote:
Code:
#!/bin/bash the above example requires 2 strings ($1 and $2) before it can work Code:
sh script.sh string1 string2 |
Nope this is not my requirement. I'll forward the test script that I managed to wrote so that you'll get a fair idea about it
|
Yes, it will help to see your script to better understand what you want to do. Have you considered using getopts? It's intended for parsing command line options. Takes a little understanding but very sweet when you've got it.
|
I use a setup like this:
Code:
for opt in $@ Code:
for opt in $@ |
There you are, this is kinda i wanna achieve it. But what's happening here is while using conventional method I was not able to shift parameter which comes after $4. which should shift to $2.
Here is the thing but its not complete at least you will get a fair idea about it. if [ "$1" = "-i" ];then "$3"="-0" shift 2 fi if [ "$4" = "-o" ];then shift 1 fi RUN #----- RUN is my function done I am doing something wrong here but not able to figure that out what that is. |
When you shift, the option locations change. If you have 4 arguments $1, $2, $3 and $4, when you shift 1, then $2 becomes 1$, $3 becomes 2 etc.
And this is correct syntax for anything: "$3"="-0" Put some echo statements in there so you can see what is happening: Code:
#!/bin/bash |
Don't shift the params; just use them as $1 $2 $3 etc.
If you want to use options with switches eg blah.sh -i one -o two -t three the use getopts http://tldp.org/LDP/abs/html/internal.html#EX33 |
Why roll your own when there's a tool for the job. Something like this should work (not tested)
Code:
lf=$'\n' |
All times are GMT -5. The time now is 11:08 PM. |