Parameter parsing


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Parameter parsing
# 1  
Old 06-22-2017
Parameter parsing

Hi. I need to parse a string with several optional parameters such as the following:
Code:
PARAMS="-server testserver -type standby -host testhost1"

How can I parse this to place any values after the -parameter into a new variable? E.g I want to place testserver into a variable called SERVER. But importantly, sometimes the parameters may have no values and sometimes the actual -param itself may not be in the string and also there will be an optional (preceding) DEBUG value.

I've tried a while loop with a case statement but it doesn't handle the situation when the -param is not included.

Code:
arg="DEBUG -dbversion 11.2.0.4.0 -type standby -host "
 typeset flags_args=''
for arg; do
if [[ $arg == -* ]]; then
    case $arg in
        -dbversion )  flags_args="${flags_args} -d" ;;
        -type )  flags_args="${flags_args} -t" ;;
        -host ) flags_args="${flags_args} -h" ;;
        * )       flags_args="${flags_args} $arg" ;;
    esac
fi
done
# push the args back on input stack
set -- ${flags_args}
print "New Parameters: $*"
 
# now use getops
while getopts :d:t:h: args; do
    case $args in
        d) typeset _VER="$OPTARG" ; [[ -z ${_VER} ]] && _VER=NONE ;;
        t) typeset _TYPE="$OPTARG" ; [[ -z ${_TYPE} ]] && _TYPE=NONE ;;
        h) typeset _HOST="$OPTARG" ; [[ -z ${_HOST} ]] && _HOST=NONE ;;
        *) usage ;;
    esac
done
shift $((OPTIND-1));
print "VER: $_VER"
print "TYPE: $_TYPE"
print "HOST: $_HOST"

Any help greatly appreciated.

Last edited by user052009; 06-22-2017 at 05:27 PM..
# 2  
Old 06-22-2017
Hi.

Some possibilities to consider:
Code:
Process command-line (CLI) options, arguments

        1) getopts, builtin, bash, ksh, zsh
           http://stackoverflow.com/questions/402377/using-getopts-in-bash-shell-script-to-get-long-and-short-command-line-options

        2) perl: libgetopt-euclid-perl, creates man page, help automatically

        3) getopt, enhanced getopts, part of the util-linux, allows GNU "--"
           Examples: /usr/share/doc/util-linux/examples, 2016.03.27

        4) argp.sh, wrapper for getopt, creates man and help (text, XML), etc.
           Allows mixed options and arguments.
           Compiled argp.c -> argp for faster execution.
           https://sourceforge.net/projects/argpsh/, 2016.03.27

        5) shflags, wrapper for getopt, creates help, allow mixed options
           and arguments
           https://github.com/kward/shflags, 2016.08.01

        6) ksh getopts, enhanced, process GNU "--", creates man, help, etc.
           Examples: Learning the Korn Shell, O'Reilly, 2nd, p 380ff

        7) zsh zparseopts
           man zshmodules, part of zshutil

Best wishes ... cheers, drl
# 3  
Old 06-22-2017
With a recent shell offering "here strings" and "arithmtic expansion", you could try
Code:
arg="DEBUG -dbversion 11.2.0.4.0 -type standby -host "
ARR=($arg)
for ((i=0; i<${#ARR[@]}; i++)); do TMP=${ARR[$i]}; TMPM=${ARR[$i]/-}; [ $TMP = $TMPM ] && read ${TMP^^} <<< 1 || read ${TMPM^^} <<< ${ARR[((++i))]}; done
echo $DEBUG, $DBVERSION, $TYPE, $HOST
1, 11.2.0.4.0, standby,

# 4  
Old 06-23-2017
Thanks Rudi. Getting the following when I run it from within a script (although it works on the cmdln):
Code:
./p.ksh: line 4: syntax error at line 5: `^' unexpected

# 5  
Old 06-23-2017
Unfortunately, in my proposal there's neither a line 4 not a line 5. PLEASE get accustomed to post a decent context!
I guess ksh doesn't like the "Case Modification" Parameter Expansion known from bash. Try to typeset -u TMP TMPM and remove the ^^ in the code line.

Last edited by RudiC; 06-23-2017 at 10:02 AM.. Reason: typo
This User Gave Thanks to RudiC For This Post:
# 6  
Old 06-23-2017
Works perfectly. Thank you very much.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Use parameter expansion over a parameter expansion in bash.

Hello All, Could you please do help me here as I would like to perform parameter expansion in shell over a parameter expansion. Let's say I have following variable. path="/var/talend/nat/cdc" Now to get only nat I could do following. path1="${path%/*}" path1="${path1##*/}" Here... (8 Replies)
Discussion started by: RavinderSingh13
8 Replies

2. Shell Programming and Scripting

Call Script with Parameter (that has another parameter)

Hi. How do I achieve this sh /EDWH-DMT02/script/MISC/exec_sql.sh "@/EDWH-DMT02/script/others/CSM_CKC/Complete_List.sql ${file_name}" Complete_List.txt The /EDWH-DMT02/script/MISC/exec_sql.sh has two parameters and it's working fine with this sh /EDWH-DMT02/script/MISC/exec_sql.sh... (7 Replies)
Discussion started by: aimy
7 Replies

3. Shell Programming and Scripting

Resolving a parameter which is passed as parameter

Hi, I have the following files. ->cat scr.sh export TMP_DIR=/home/user/folder1 export TMP_DIR_2=/home/user/folder2 while read line do cat "$line" done<file_list.dat ------------------------ -> cat file_list.dat $TMP_DIR/file1.txt $TMP_DIR_2/file2.txt --------------------------- -> cat... (6 Replies)
Discussion started by: barath
6 Replies

4. Shell Programming and Scripting

How to get the parameter value from the parameter file in perl?

hi all, i have a parameter file of following format, i want a method which can get the value of specific parameter. parameter file format: <Parameter Name="FileLocationWindows"> <Description> The directory location of the logger file. ... (1 Reply)
Discussion started by: laxmikant.hcl
1 Replies

5. Shell Programming and Scripting

Passing parameter to script, and split the parameter

i am passing input parameter 'one_two' to the script , the script output should display the result as below one_1two one_2two one_3two if then echo " Usage : <$0> <DATABASE> " exit 0 else for DB in 1 2 3 do DBname=`$DATABASE | awk -F "_" '{print $1_${DB}_$2}` done fi (5 Replies)
Discussion started by: only4satish
5 Replies

6. Shell Programming and Scripting

Command that takes one parameter and then searches for the passed in parameter

Hi I am looking for a unix command or a small shell script which can takes one parameter and then searches for the passed in the parameter in any or all files under say /home/dev/ Can anyone please help me on this? (3 Replies)
Discussion started by: pankaj80
3 Replies

7. Shell Programming and Scripting

Parsing a command line parameter in script

I have a simple script that builds a complex program call which passes a number of parameters to the program. I'm trying to enhance the script to include the value of the command line parameter in the name of a file being created. The problem I'm having is that the parameter may include a forward... (11 Replies)
Discussion started by: pbmax626
11 Replies

8. Shell Programming and Scripting

Parsing of file for Report Generation (String parsing and splitting)

Hey guys, I have this file generated by me... i want to create some HTML output from it. The problem is that i am really confused about how do I go about reading the file. The file is in the following format: TID1 Name1 ATime=xx AResult=yyy AExpected=yyy BTime=xx BResult=yyy... (8 Replies)
Discussion started by: umar.shaikh
8 Replies

9. Shell Programming and Scripting

Perl parsing compared to Ksh parsing

#! /usr/local/bin/perl -w $ip = "$ARGV"; $rw = "$ARGV"; $snmpg = "/usr/local/bin/snmpbulkget -v2c -Cn1 -Cn2 -Os -c $rw"; $snmpw = "/usr/local/bin/snmpwalk -Os -c $rw"; $syst=`$snmpg $ip system sysName sysObjectID`; sysDescr.0 = STRING: Cisco Internetwork Operating System Software... (1 Reply)
Discussion started by: popeye
1 Replies

10. Shell Programming and Scripting

how do I make dynamic parameter names? Or get the value of a parameter evaluated twi

Say I write something like the following: var1=1 var2=2 for int in 1 2 do echo "\$var$int" done I want the output to be: 1 2 Instead I get something like: $var1 $var2 (2 Replies)
Discussion started by: Awanka
2 Replies
Login or Register to Ask a Question