Sponsored Content
Full Discussion: parse long input parameters
Top Forums Shell Programming and Scripting parse long input parameters Post 302268968 by Lie Algebra on Tuesday 16th of December 2008 03:36:42 PM
Old 12-16-2008
How to parse long and/or short options in shell scripts

Hi ,

it has been a while since I have tried to parse command line arguments
made of both short and long options. There are many ways to do so but
not as many of them handle switch clustering in a flawlessly way. Here
is a snippet of code that illustrates my point. It is, however, far from
being straightforward (getopts is) since it requires to do the parsing
by hand. At last, the sample uses builtins commands only so the parsing
is expected to be optimal.


Code:
 
get_arg () {
    case "${#ARGSTR}" in
	0) test -n "$1" && logger "$1" && return 0 || { logger "--> -${OPTSTR} argument ?" >&2; return 1; } ;;
	*) logger "$ARGSTR"; return 2;;
    esac
}

At parsing time, OPTARG holds the last expected option argument.
Code:
 
while :; do 
    case "$#" in 0) break; esac; OPTSTR="$1" 
    case "$OPTSTR" in --) break   ;; 
             --flag_1) OPTSTR=-f1 ;;
             --flag_2) OPTSTR=-f2 ;;
             --flag_3) OPTSTR=-f3 ;;
           --optarg_1) OPTSTR=-o1 ;;
           --optarg_2) OPTSTR=-o2 ;;
    esac 
    case "$OPTSTR" in 
           -*) OPTSTR="${OPTSTR#-}"
               while :; do 
                     case "${#OPTSTR}" in 0) break; esac 
                     ARGSTR="${OPTSTR#?}"; OPTSTR="${OPTSTR%${ARGSTR}}"
                     case "$OPTSTR" in 
                                 o1) OPTARG="$(get_arg "$2")" && shift; case "$?" in 1) exit 65; esac 
                                     logger "-o1/--optarg_1 argument  is $OPTARG";;
                                 o2) OPTARG="$(get_arg "$2")" && shift; case "$?" in 1) exit 65; esac
                                     logger "-o2/--optarg_2 argument  is $OPTARG";;
                                 f1) logger "FLAG_1 is set";;  
                                 f2) logger "FLAG_2 is set";;
                                 f3) logger "FLAG_3 is set";;
                                  *) logger "--> unrecognized option -${OPTSTR}" >&2 && exit 65 ;; 
                     esac 
                     case "$OPTSTR" in o1*|o2*) break; esac
                     OPTSTR="$ARGSTR"
               done 
    esac 
    shift 
done


Last edited by Lie Algebra; 03-02-2009 at 01:46 AM..
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Input parameters

I have a script which take 3 input parameters 1st - a date (i.e. current date) 2nd - type (i.e. A) 3rd - limit (i.e. 40) normally the date parameter would be current date, so I thought I could do this calculate.sh $(date +%Y-%m-%d) A 40 however, it seems like it can't be done,... (3 Replies)
Discussion started by: mpang_
3 Replies

2. Shell Programming and Scripting

input parameters pls help!

Hi i am a newbie who is trying to input parameters into this script, but not sure where to start. The parameters that need to be input are the baseline label "abc.0111.BL " mantioned bellow, and database string "abc/abcp@db2 @counter.sql " Environment: Windows XP Running script: Cygwin 3.2 ... (2 Replies)
Discussion started by: yarik20
2 Replies

3. Shell Programming and Scripting

Verify input parameters

I have to write a script to verify input parameters; say esr should be YES or NO other wise the script should print an error. This is what i tried in my script but I get the following error : esr="YES" if ; then print " Error should specify esr options YES/NO" else esr =$esr fi ... (2 Replies)
Discussion started by: ramky79
2 Replies

4. Shell Programming and Scripting

Parse HTML tag parameters and text

Hi! I have a bunch of HTML files, which I want to parse to CSV files. Every page has a table in it, and I need to parse each row into a csv record. With awk and sed, I managed to put every table row in separate lines. So my file looks like this: <TR> .... </TR> <TR> .... </TR> ...One... (1 Reply)
Discussion started by: senszey
1 Replies

5. Programming

Parse parameters with getopts

Hi, this is my problem I have script with two parameters -n name and -s surname. Both have arguments and I want to know how parse these parameters with getopts. When you write ./names -n John -s White it find you all persons, which name is John White, but when you write ./names -n John ... (1 Reply)
Discussion started by: frees
1 Replies

6. Shell Programming and Scripting

specified path name is too long passing parameters to awk via shell script

Hello, I have this shell script that runs awk code by passing in parameters however now it doesn't work anymore with the parameters and I don't know why. It removes duplicates from an input file based on a part of the last field and a key column. It removes the record with the older datetime... (0 Replies)
Discussion started by: script_op2a
0 Replies

7. Shell Programming and Scripting

[Solved] I need to parse an username in parameters

Hi! Thank you ina advance for your Help. Sorry my english, Im from Argentina! Great Place!!! So, here si my problem I have a variable, lets say PARAMETERS that could be set like this: PARAMETROS="-Usistemas string1 -Astring2 -G -Y -Kcoco -Y string4 -Z" or PARAMETROS="string1... (1 Reply)
Discussion started by: Hercraft
1 Replies

8. Shell Programming and Scripting

Shell script to run sql query having a long listing of parameters

Hi, I have a query regarding execution of a sql query having long listing of parameters ..I need to execute this query inside a shell script. The scenario is like.... Suppose I have a file abc.txt that has the card numbers..it could be in thousands.. then I need to fire a query like ... (12 Replies)
Discussion started by: vsachan
12 Replies

9. Shell Programming and Scripting

How can I take input parameters after the first one?

Hi, Is there a simple way to take input of parameters after the first one? As following example, if I assign others=$2, it only takes the second one, if I assign others=$@, it will include the first one. I may try to assign others="$2 $3 $4 $5 $6 $7 $8 $9", it looks very ugly and could missing... (1 Reply)
Discussion started by: hce
1 Replies

10. AIX

Input parameters

friends and I can validate whether to run the shell has input parameters m event date, I occasionally happen something like this does not work if $ 1 is null then echo has entered input parameters else echo "parameter ok" fi (2 Replies)
Discussion started by: tricampeon81
2 Replies
funnel(1)							     logtools								 funnel(1)

NAME
funnel - split one pipe stream to one or more files or programs SYNOPSIS
funnel [|] [>[>]file] [|process] DESCRIPTION
The funnel program will read data from standard input and write it to several output streams. The | symbol means to copy standard input to standard output. The > symbol means that a file name immediately follows with no separating spaces, the file will be truncated if it exists or created if it doesn't. The >> means that the file is to be appended to if it exists. A | symbol followed immediately by text indicates a command that is to be run by system() and will have all input piped to it. EXIT STATUS
0 No errors 1 Error opening input Other errors are 100 + number of commands or files that fail AUTHOR
This program, its manual page, and the Debian package were written by Russell Coker <russell@coker.com.au>. FEATURES
Uses popen (3) for output so that shell expansion gets performed. If you want such shell expansion then you must ensure that you quote the parameters correctly so that they get expanded by popen (3) not the shell that calls funnel. SEE ALSO
popen(3) Russell Coker <;russell@coker.com.au> 0.06 funnel(1)
All times are GMT -4. The time now is 08:32 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy