dwv%cadnet@UMIX.CC.UMICH.EDU (Doug Vander Wilt) (06/13/91)
After many years programming with the AEGIS shell (/com/sh) I wonder if I'm missing out on some easier argument processing methods. Is there a "shift" style command to shift arguments left? I haven't had any success referring to them with a variable index, e.g.: ^0 ^1 .. ^^n I currently use the following form to process an unknown number of shell arguments, but it won't properly handle quoted arguments: (e.g.: $ argtest a 'b c' d ) #!/com/sh # ARGTEST - test shell script arguments eon n := 0 args !* | while readln arg do n := ^n + 1 args (("Argument " + ^n + ": " + ^arg)) enddo BTW: the AEGIS shell is limited to 128 arguments (from: $ help limits) which is 127 arguments not counting ((^0)) if you're interested. Doug Vander Wilt Sr. CAD/CAM Engineer Honeywell MICRO SWITCH Division dwv@cadnet.micro.honeywell.com
philip@cel.cummins.com (Philip D. Pokorny) (06/14/91)
Doug Vander Wilt writes: > After many years programming with the AEGIS shell (/com/sh) I wonder if > I'm missing out on some easier argument processing methods. Is there a > "shift" style command to shift arguments left? I haven't had any success > referring to them with a variable index, e.g.: ^0 ^1 .. ^^n > > I currently use the following form to process an unknown number of shell > arguments, but it won't properly handle quoted arguments: > (e.g.: $ argtest a 'b c' d ) I remember having to do something like this when proccessing the equivalent of a shell array... Here's what I came up with: #!/com/sh # Print shell arguments. eon n := 0 while(( ^"set -c args @^^n" <> "" )) do arg := ^"set -c args @^^n" args (( "Argument " + ^n + ": " + ^arg )) n := ^n + 1 enddo This uses indirection via the 'set -c' shell command to evaluate the argument @^^n once (to ^3, etc) and then again. (to get the third argument, etc) The down-side to this scheme is that you may have to be carefull about null strings as arguments. (You could check all 127 arguments or if you are expecting an argument that might be null it's not a problem.) A sample run: $ test.sh 1 2 3 '^1' '4 5' Argument 0: test.sh Argument 1: 1 Argument 2: 2 Argument 3: 3 Argument 4: ^1 Argument 5: 4 5 $ Sincerely, Philip D. Pokorny philip@cel.cummins.com :)
nazgul@alphalpha.com (Kee Hinckley) (06/16/91)
In article <9106131931.AA08669@cel.cummins.com> philip@cel.cummins.com (Philip D. Pokorny) writes: >#!/com/sh ># Print shell arguments. >eon > >n := 0 > >while(( ^"set -c args @^^n" <> "" )) do > arg := ^"set -c args @^^n" > args (( "Argument " + ^n + ": " + ^arg )) > n := ^n + 1 >enddo Or... #!/com/sh eon # # Note that this will exit the loop early if there are any 0 length arguments. # for i := 1 to 99999 arg := ^"set -c args ^^i" if eqs ^arg '' then exit endif # Do your stuff here. args ^arg endfor # Btw. Here's the explanation, from the inside out. # args ^^i -> ^1 # set -c args ^1 -> <argument#1> # # set -c is like saying 'sh -c' except that it's faster and takes place at the # current level. This would have worked with "sh -c" as well. We are taking # advantage of the fact that command line arguments after -c are reparsed, so # we get one extra level of parsing. # # I *wrote* the shell and it it took me over a year to figure this hack out. # There really needs to be a better mechanism. Hopefully it will come out in # some future release. Note. If you KNOW that none of your arguments have any # spaces in them you can make this process much simpler by just saying: # # for arg in ^* # ... # endfor # -- Alfalfa Software, Inc. | Poste: The EMail for Unix nazgul@alfalfa.com | Send Anything... Anywhere 617/646-7703 (voice/fax) | info@alfalfa.com I'm not sure which upsets me more: that people are so unwilling to accept responsibility for their own actions, or that they are so eager to regulate everyone else's.