ksh processing options and parameters


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting ksh processing options and parameters
# 1  
Old 05-31-2013
ksh processing options and parameters

I have a requirement where I need to process both options and parameters.

command line call
Code:
ie xxx.ksh -sid TEST1 -search_str LOCKED  user1 user2 .....

I am using the following peice of code but I am usure how I can loop through all my parameters user1, user2, ... Note at the minium there were
be at least "user1"

Can somebody provide and example.

Code:
while [ $# -gt 0 ]
do
     case $1 in
     -+([sS]*(id|ID)))
          shift
          export ORACLE_SID=$1
          ;;
     -+([sS]*(earch_string|EARCH_STRING)))
          shift
          export SEARCH_STRING=$1
          ;;
       *) shift ;;
   esac
done

Thanks to all who answer

---------- Post updated at 02:50 PM ---------- Previous update was at 11:29 AM ----------

My solutuon. Its not fancy but appears to work. Any elegant solutuons are more than welcome

Code:
 
#!/bin/ksh
 
let i=0
set -A parameters
typeset -L1 first_char_in_last_arg

while [ $# -gt 0 ]
do
     case $1 in
     -+([sS]*(id|ID)))
          shift
          export ORACLE_SID=$1
          shift
          ;;
     -+([sS]*(earch_string|EARCH_STRING)))
          shift
          export SEARCH_STRING=$1
          shift
          ;;
       *)
         first_char_in_last_arg="$1"
         if [ "$first_char_in_last_arg" != "-" ]
         then
            parameters[$i]="$1"
            ((i=i+1))
         fi
         shift ;;
   esac
done
for i in ${parameters[@]}
do
   echo $i
done


Last edited by joeyg; 05-31-2013 at 04:07 PM.. Reason: code not icode... (jg just adjusted)
# 2  
Old 05-31-2013
Not fancy perhaps, but far from being bad also, I just had a little moment to spare and looked at your script and I like it...
# 3  
Old 05-31-2013
I'm not sure where to start here, but there are several problems. By convention, UNIX utility options that are introduced with a single minus sign should have single character option names. Long options (options with option names that are more than one character) should be introduced by two minus signs, (e.g.,--version). Option names should be case sensitive. (It isn't unusual for some option arguments to be case insensitive, but not the option names themselves.) When a utility uses a single character option that uses an option argument, it should allow that option argument to be part of the same command line argument as the option itself. (This means, for example, that the width of an output line to be produced by the fold utility can be specified either as:
Code:
fold -w72 file
    or as:
fold -w 72 file

and get exactly the same results.) However, this does not apply to long option names. A long option option argument must be a separate argument from the argument that contains the long option name.

The standards only specify the behavior of single character options and provide the getopts utility to parse command line options given to shell scripts, but most implementations provide implementation-specific extensions to parse command line options when the utility is parsing long options. The getopts(1) man page on your system will probably either describe how it has been extended to parse long options or the SEE ALSO section will point you to another utility that can be used to parse long options on that system.

There is a reasonable example on the POSIX getopts man page linked to above showing how to parse commands lines similar to what you're trying to do as long as you only use single character option names. That example would be worth examining even if you're going to use long options.

If your ie script starts with the code you included in the 1st message in this thread, a recent Korn shell is used to evaluate it (I'm guessing since you specified xxx.ksh as an argument to this script), and you invoke it with the command:
Code:
ie xxx.ksh -sid TEST1 -search_str LOCKED  user1 user2 .....

a case like:
Code:
     -+([sS]*(id|ID)))
          shift
          export ORACLE_SID=$1
          ;;

will not only match -s, -S, -sid and -SID, it will also match -SsSssSS and -SidIDssIDid and an infinite number of other combinations; and the following command line argument will not only be assigned as the value the ORACLE_SID exported environment variable, it will also processed as another command line argument the next time through the loop. I would have expected that you intended something like:
Code:
     -([sS]?(id|ID)))
          shift
          export ORACLE_SID=$1
          shift
          ;;

which would match -s, -S, -sid, -Sid, -sID, and -SID; or:
Code:
     -([sS]?([iI][dD])))
          shift
          export ORACLE_SID=$1
          shift
          ;;

which would match -s, -S, -sid, -siD, -sId, -sID, -Sid, -SiD, -SId, and -SID. (NOTE that I do not suggest nor condone either of these possibe replacements since I think you should follow standard utility command line parsing conventions; I'm just pointing out that the Korn shell pattern matching expressions you're using don't seem to match the description of what I think you were trying to do.)

Although many Linux utilities allow options to be intermixed with operands, the standards do not suggest that any new utilities do that. Options should come before operands and option processing should be terminated by the first non-option arugment that does not start with a minus sign or can be terminated by an operand that is exactly two minus signs. (There are a few utilities in the standard that allow intermixed options and operands for compatibility with historical versions of those utiiities. And there are occasional cases where it makes sense for new utilities when options change for different sets of operands, but, generally, this should be avoided.)

The code you provided not only allows intermixed options and operands, it also processes option arguments both as option arguments and as possible options or operands. And all operands are effectively ignored and made unavailable to the rest of your script after the options have been parsed.

I hope this gives you a few things to think about as you write specifications for utility behavior and as you write command line parsing code. Good luck.

-------------------

Oops. I see that you updated your script while I was composing my response. Your updated script is much better (it no longer just discards operands and it no longer double processes option arguments), but the other comments still apply.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Crontab not processing parameters sent to script.

Hello All, I'm running Debian on a ThinkPad X1/2G and all seems to be running well. However, it's got a SSD that needs the trim command run at regular intervals. I'm implementing this using cron and a shell script, as recommended. The cron job entries are as follow: 0 * * * *... (8 Replies)
Discussion started by: ASGR
8 Replies

2. Shell Programming and Scripting

Processing Multiple Arguments in Command Line Options

Hi All, I am new to scripting. Could you please assist me . Here is my requirement. I have written a script that has 2 option flags defined. -l) calls some function with the arguments passed in front of -l -r) calls second function with the arguments passed in front of -r *) calls the... (7 Replies)
Discussion started by: Jay Deshpande
7 Replies

3. Shell Programming and Scripting

Why double quotation marks doesn't work in ksh function parameters passing?

I'm working on AIX 6, ksh shell. The parameters are some strings quotated by double quotation marks which from a file. They are quotated because there may be spaces in them. Example: "015607" "10" " " "A"I want to pass these parameters to a shell function by writing the following command: ... (4 Replies)
Discussion started by: Shimmey
4 Replies

4. Shell Programming and Scripting

Processing files using ksh shell

I need to write a Shell Script to process a huge folder of nearly 20 levels.I have to process each and every file and check which files contain lines like select insert update When I mean line it should take the line till I find a semicolon in that file. I should get a result like this ... (1 Reply)
Discussion started by: 20033716
1 Replies

5. Shell Programming and Scripting

Get Options and Parameters

Hi there. Could someone explain how do i get the options separated from the arguments. My problem is that i have a lot of options and in all of those options i need to have the parameters available to use in that option and all the others. How can i take the arguments before i cycle throw the... (4 Replies)
Discussion started by: KitFisto
4 Replies

6. Shell Programming and Scripting

KSH script -text file processing NULL issues

I'm trying to strip any garbage that may be at the end of my text file and that part is working. The problem only seems to be with the really long lines in the file. When the head command is executed I am directing the output to a new file. The new file always get a null in the 4096 position but... (2 Replies)
Discussion started by: geauxsaints
2 Replies

7. UNIX for Dummies Questions & Answers

ksh: verifying number of parameters passed

hi all, i have a ksh script that takes up to 3 parameters -- only 2 of which are required. what's the simplest way to check if the user passed 2 or 3 parameters? if 3 parameters are not null then do this elif 2 parameters are not null then do this else echo "you need at least 2... (5 Replies)
Discussion started by: ankimo
5 Replies

8. Shell Programming and Scripting

getopts takes options for parameters

Here is my post with a question about getopts. I am running korn shell on Solaris 5.8. I am trying to ensure that certain options require a parameter, which is easy enough. I have found that if multiple options are entered on the command line together, but the parameter for one of the options is... (1 Reply)
Discussion started by: UCD-Randy
1 Replies

9. Programming

parameters from my program in c to a .ksh script

hi.. this is my question: it is possible transfer parameters from my program written in C to a .ksh script? how can i do it? i have a program in C, what is called from a .ksh script, and i need what the C program returns some values (parameters) please, help me - any idea thanks ... (2 Replies)
Discussion started by: DebianJ
2 Replies
Login or Register to Ask a Question