[CentOS] Bash Help
I am trying to write a script that can pass cmd line args into an executable it runs, I need at least 4 args, but I need to pass the rest to it in the script if they exist. $@ doesn't help because I use the 1st and 2nd etc, so how do I elegantly handle passing args 4+ if they exist on the end of the command I call? Is there a simple way without constructing the command to execute on the fly by analyzing $# first and building it everytime? Thanks! jlc ___ CentOS mailing list CentOS@centos.org http://lists.centos.org/mailman/listinfo/centos
Re: [CentOS] Bash Help
On Mon, Aug 17, 2009 at 10:01 AM, Joseph L. Casale wrote: > I am trying to write a script that can pass cmd line args into an > executable it runs, I need at least 4 args, but I need to pass the rest > to it in the script if they exist. > > $@ doesn't help because I use the 1st and 2nd etc, so how do I elegantly > handle passing args 4+ if they exist on the end of the command I call? maybe you call shift (more than once) and then pass $...@? HTH, -- -Bob ___ CentOS mailing list CentOS@centos.org http://lists.centos.org/mailman/listinfo/centos
Re: [CentOS] Bash Help
>maybe you call shift (more than once) and then pass $...@? Didn't know bash had a shift! Thx! jlc ___ CentOS mailing list CentOS@centos.org http://lists.centos.org/mailman/listinfo/centos
Re: [CentOS] Bash Help
I can't think of the exact syntax at the minute but something like; if $# => 4 then for i = 1 to ($# - 4) echo "arg number $i is $expr($i)" next fi $# is the number of args passed so just pass the total number of args minus four ($~-4) That syntax is all wrong, but somebody on this list (in fact many people on this list) knows the correct syntax, thats just my crapp pesudo code to explain one method of achieving you desired result, HTH. James ;) -BEGIN GEEK CODE BLOCK- Version: 3.1 GIT/MU/U dpu s: a--> C++>$ U+> L++> B-> P+> E?> W+++>$ N K W++ O M++>$ V- PS+++ PE++ Y+ PGP t 5 X+ R- tv+ b+> DI D+++ G+ e(+) h--(++) r++ z++ --END GEEK CODE BLOCK-- ___ CentOS mailing list CentOS@centos.org http://lists.centos.org/mailman/listinfo/centos
Re: [CentOS] Bash Help
On Mon, 2009-08-17 at 19:55 +0100, James Bensley wrote: > I can't think of the exact syntax at the minute but something like; > > if $# => 4 then > for i = 1 to ($# - 4) > echo "arg number $i is $expr($i)" > next > fi $ echo $* 1 2 3 4 $ \ > while [ "$1" != '' ] ; do > echo $1 > shift > done 1 2 3 4 $ > > -- Bill ___ CentOS mailing list CentOS@centos.org http://lists.centos.org/mailman/listinfo/centos
Re: [CentOS] Bash Help
William L. Maltby wrote: > On Mon, 2009-08-17 at 19:55 +0100, James Bensley wrote: >> I can't think of the exact syntax at the minute but something like; >> >> if $# => 4 then >> for i = 1 to ($# - 4) >> echo "arg number $i is $expr($i)" >> next >> fi > > $ echo $* > 1 2 3 4 > $ \ >> while [ "$1" != '' ] ; do >> echo $1 >> shift >> done > 1 > 2 > 3 > 4 > $ > >> Note that (a) the args are gone as you shift them out, so a real script would need to save $1 into a named variable before each shift if it wants the value later, and (b), '' is a legitimate and possible argument on a shell command line so it might be better to check $# for the count remaining. -- Les Mikesell lesmikes...@gmail.com ___ CentOS mailing list CentOS@centos.org http://lists.centos.org/mailman/listinfo/centos