processing command args in bash
#!/bin/bash noob here.
i'm comfortable with C but i think some simpler tasks on my system will be more easily achived with bash. I'd also like to getter a better grasp of all my config files and initialisation scripts so here i go.. Code:
if [ "$#" -eq 0 ];then the program will process args of an indeterminate number but it will stipulate what the last arg will be and thus i need acces to it,i.e test that it is there etc i cant code [ -f $n .... where n is some integer value because i will not know what the integer value will be ahead of time. how do i achieve this pls? |
Quote:
Code:
#!/bin/bash |
Or, without "shift":
("${!i}" is actually the answer to your question "i cant code [ -f $n .... where n is some integer value") Code:
#!/bin/bash |
Hey Slzckboy,
Didn't quite understand what you were trying to do. The first comment seemed to be confusing. But I guess your actual problem was parsing the last argument. Yes, the solution to that is using 'shift' or "${!i}" as Hko suggested. I guess you wanted this : a) Invoke the script with n no. of arguments, b) If n = 0 ask the user to input the name of the file, else c) if n != 0 assume that the last argument is the name of a file, and check to see if that file exists or not. Here's how the above script would look with shift : Code:
#!/bin/bash Hope this helps ! |
no..thats fine . That is exactly what i meant.
thnks |
i think i will go the non "shift" way or else I will loose the data passed in on the previous args.!!?!
|
for parsing of command lines with flags,
man getopts make life easy for yourself! e.g parsing -d and -f flags, where -f requires an argument: Code:
Result Code:
$ bash opt.sh -df blah one two three |
Quote:
|
hmm...
ok.. well i'd value an opinion on what I have done thus far i'm getting "parameter passed in at end with no value" spat back at me. Code:
#!/bin/sh basically wots wrong with doing..... exec /usr/local/bin/$bittorrent --max_upload_rate=$up_speed ???? i tried "man getopts" but strangely the man page is not on my system. will have to google 4 it. |
Quote:
|
hey ..
thnks 4 everyones help. i have a working base now that I can expand on. I have worked out the bugs in the above posted code No need for getopts for now but I will keep it in mind for later use. |
I'm having some trouble wrapping my head around the {!1} part. I love seeing different ways to do the same thing (comparing this to the earlier shift example) it really helps me gain a deeper understanding of said language (and programming in general).
ARGV[$i] is already the name so what different is the {$i} doing? Is it like command substitution? I've done some research before asking and I've come across some info about hash keys, but I'm not sure that's really whats going on here. Thanks so much! Quote:
|
The problem to solve is that we need to access a parameter by name (number), without knowing the name in advance. We need to perform an indirect substitution, in other words.
The "${!var}" pattern provides a way to do such indirect substitutions. It expands to the value of the variable given, then again to the value of the variable generated by it. So if "$2 = bar" and "i=2", then "${!i}" will expand to "bar". Read the bash man page under Parameter Expansion for more detail. Note, by the way, that bash does not automatically have an ARGV array, that's why a loop like the one above is (was) needed to iterate through and print all the values. The output produced by it would look like this: Code:
Frankly, though, I would've just used a simple c-style for loop instead: Code:
Code:
I myself would personally just go the simple route and copy the argument list into a new dedicated ARGV-style array. You can use both ranges and negative indexes that way. Code:
|
That for loop makes it easier to see.
start at 1, until number of args, i++. Thanks I'm taking it that these two lines are equivalent: echo "${@: (-2):2}" # prints the last two arguments. echo "${ARGV[@]: (-2):2}" And in those lines the -2 is saying "start 2 back from the last" (the last is -1, the one before that is -2 so: ./argumentstuff.bash this that those many many would be -1, so the -2 is starting at those. Then the 2 thats after the -2 is saying do that to two items. so if it was ${@: (-2):1} would only delete "those"? |
Hi.
Some additional sources that I have found useful: Code:
Process command-line (CLI) options, arguments |
All times are GMT -5. The time now is 11:53 PM. |