Prev: alternative for ctime
Next: multiple cmnds in ssh..
From: Scott Bass on 29 Apr 2010 18:36 On Apr 30, 1:10 am, Janis Papanagnou <janis_papanag...(a)hotmail.com> wrote: > Scott Bass schrieb: > > > > > On Apr 27, 5:46 pm, Janis Papanagnou <janis_papanag...(a)hotmail.com> > > wrote: > >> Scott Bass schrieb: > > >>> Hi, > >>> I'm writing a script (ksh88) which is very modular, with a number of > >>> function definitions, then a main section which "glues it all > >>> together". One of these functions is to parse the command line for > >>> arguments. > >>> Here is an excerpt: > >>> #=================================================================== > >>> function parse_command_line # process command line arguments > >>> #=================================================================== > >>> { > >>> while getopts :o:a:h optchar > >>> do case $optchar in > >>> "o") # specify options for the sas program > >>> options=$OPTARG > >>> ;; > >>> "a") # specify delayed start of job > >>> starttime=$OPTARG > >>> ;; > >>> "h") # display help screen > >>> show_help "$0 command - help screen:" > >>> exit 0 > >>> ;; > >>> "?") # illegal option specified > >>> show_help "$OPTARG is not a valid option." > >>> exit 1 > >>> ;; > >>> ":") # Option argument is missing. > >>> show_help "$OPTARG requires an argument." > >>> exit 1 > >>> ;; > >>> esac > >>> done > >>> # Shift past option arguments > >>> shift $(($OPTIND - 1)) > >>> # Remaining arguments are program files > >>> jobs=$* > >>> } > >>> Then later, at the bottom of the script: > >>> #################################################################### > >>> # MAIN PROGRAM > >>> #################################################################### > >>> # If running attached to a terminal, the script is running in a shell > >>> if [[ $usertty != "not a tty" ]] > >>> then > >>> initialize_variables || exit $? > >>> parse_command_line $* || exit $? > >> You most likely want > > >> parse_command_line "$@" || exit $? > > >>> Questions: > >>> 1) Is there any way I can keep this modular, or do I need to move the > >>> code in parse_command_line into open code (the "main" program)? > >>> 2) Is there a way I can shift the arguments in the parse_command_line > >>> function and have it affect the global list of arguments? > >> It's not really necessary to do so. > > >>> IOW so I > >>> can substitute $* for $jobs in the above code. > >> (Saying that without looking into the details of your program...) > >> Switch to "$@" and you should be fine. > > > <rest of thread deleted> > > > Thanks Janis, that helped a lot. However, I still need a way to have > > the command line parsing code modify the global list of arguments. > > A call of shift in the function will shift the arguments provided to > the function, not in the global environment. > > > > > > > A short test script: > > > #!/bin/ksh > > > function foo > > { > > echo 1 $@ > > shift > > shift > > shift > > echo 2 $@ > > } > > > echo 0 $@ > > foo "$@" > > echo 3 $@ > > > Results: > > > test_script a b c d e f > > 0 a b c d e f > > 1 a b c d e f > > 2 d e f > > 3 a b c d e f > > > I need 3 to equal 2. Reason: the outer script parses the command > > line, processing arguments and shifting the results. I return the > > list of jobs in $jobs. However, I also want the inner script to be > > standalone, processing $@ as a list of jobs if called directly. > > As last line in your function foo add print 3 or maybe print $# > and call the function as > > shiftvalue=$( foo "$@" ) > shift $shiftvalue > > or resp. > > remainingargs=$( foo "$@" ) > shift $(( $# - $remainingargs )) > > (Not that this would look any better than a hack.) > > Janis > > > Thanks, > > Scott Hi Janis, Thanks again for the help. Sincerely appreciated. I've come up with this approach. Let me know if it's a worse approach than what you've stated above. My outer script processes the command line arguments (in the function), shifts its function arguments, and returns the jobs to run in the variable $jobs. The inner script processes each $job in $jobs. In the inner script, I've coded jobs=${jobs:-$@}. So, if $jobs exists and is not null, I know it was set by the outer script. If not, then the inner script was called as a standalone script, and the list of jobs were passed in as its arguments. The only time this would fail is if there was an environment variable in the calling shell named $jobs, which could conceivably happen but is unlikely. Regards, Scott |