ksh parsing arguments in a string rather than from the cmdln


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting ksh parsing arguments in a string rather than from the cmdln
# 1  
Old 11-15-2017
ksh parsing arguments in a string rather than from the cmdln

Hi. I have a piece of code that reads and parses command line options. I'd like to alter it slightly to read from a string (that's set elsewhere in the script) rather than directly from the command line (arg). Can somebody show me how to do this? Many thanks.

My code is as follows:

Code:
typeset flags_args=''
for arg; do
    case $arg in
        -version )  flags_args="${flags_args} -t" ;;
        -host ) flags_args="${flags_args} -r" ;;
        -debug ) flags_args="${flags_args} -d" ;;
        * )       flags_args="${flags_args} $arg" ;;
    esac
done
set -- ${flags_args}

while getopts :t:r:d args; do
    case $args in
        t) typeset VERSION="$OPTARG" ;;
        r) typeset HOST="$OPTARG" ;;
        d) typeset DEBUG="true"; typeset DEBUG="DEBUG" ;;
        *) print "Invalid option specified." ;;
    esac
done
shift $((OPTIND-1));

Instead of reading the arguments from command line I'd like the code to read from the following variable that's set inside the KSH script:
Code:
STR="-version 99 -host testhost "

or
Code:
STR="-version 99 -host testhost -debug"

# 2  
Old 11-15-2017
Quote:
Originally Posted by pnolanch
Hi. I have a piece of code that reads and parses command line options. I'd like to alter it slightly to read from a string (that's set elsewhere in the script) rather than directly from the command line (arg). Can somebody show me how to do this? Many thanks.

My code is as follows:

Code:
typeset flags_args=''
for arg; do
    case $arg in
        -version )  flags_args="${flags_args} -t" ;;
        -host ) flags_args="${flags_args} -r" ;;
        -debug ) flags_args="${flags_args} -d" ;;
        * )       flags_args="${flags_args} $arg" ;;
    esac
done
set -- ${flags_args}

while getopts :t:r:d args; do
    case $args in
        t) typeset VERSION="$OPTARG" ;;
        r) typeset HOST="$OPTARG" ;;
        d) typeset DEBUG="true"; typeset DEBUG="DEBUG" ;;
        *) print "Invalid option specified." ;;
    esac
done
shift $((OPTIND-1));

Instead of reading the arguments from command line I'd like the code to read from the following variable that's set inside the KSH script:
Code:
STR="-version 99 -host testhost "

or
Code:
STR="-version 99 -host testhost -debug"

Many versions of the Korn shell include a getopts that supports both long options and short options (although support for long options isn't always mentioned in the ksh man page). If you could introduce your long options with -- instead of a single -, you might want to try the following replacement for your code:
Code:
while getopts ':t:(version)r:(host)d(debug)' args; do
    case $args in
        t) typeset VERSION="$OPTARG" ;;
        r) typeset HOST="$OPTARG" ;;
        d) typeset DEBUG="DEBUG" ;;
        *) print "Invalid option specified." ;;
    esac
done
shift $((OPTIND-1));
printf 'DEBUG has been set to "%s"\n' "$DEBUG"
printf 'HOST has been set to "%s"\n' "$HOST"
printf 'VERSION has been set to "%s"\n' "$VERSION"

and see what happens when you invoke it with:
Code:
script_name --version 99 --host=testhost

and:
Code:
script_name -t 98.6 -rmyhost --debug

When I try the above two commands with ksh on macOS High Sierra (version 10.13.1), I get the output:
Code:
DEBUG has been set to ""
HOST has been set to "testhost"
VERSION has been set to "99"

and:
Code:
DEBUG has been set to "DEBUG"
HOST has been set to "myhost"
VERSION has been set to "98.6"

# 3  
Old 11-15-2017
Thanks Don. But how do I read the parameters into that case statement from a variable set within the script rather than the command line?
# 4  
Old 11-15-2017
Change
Code:
for arg ; do

to
Code:
for arg in $STR ; do

Beware that things like quotes-inside-quotes won't do what you want when you store arguments.
This User Gave Thanks to Corona688 For This Post:
# 5  
Old 11-15-2017
yep ... that does the job. Many thanks.
This User Gave Thanks to user052009 For This Post:
# 6  
Old 11-22-2017
Quote:
Originally Posted by user052009
Thanks Don. But how do I read the parameters into that case statement from a variable set within the script rather than the command line?
You can also use a subfunction to parse the command line and feed it the string:

Code:
parse_cmd ()
{
while getopts :t:r:d args; do
    case $args in
        t) typeset VERSION="$OPTARG" ;;
        r) typeset HOST="$OPTARG" ;;
        d) typeset DEBUG="true"; typeset DEBUG="DEBUG" ;;
        *) print "Invalid option specified." ;;
    esac
done
shift $((OPTIND-1));

return 0
}


...
# main()
STR="<your arguments here in the same form as parameters from stdin>"

parse_cmd $STR    # process opts and args from STR

parse_cmd $*      # process opts and args from stdin

exit 0

Note that if you use a subfunction like above you need to get the parsed values out of the function somehow, because the variables in it are now local to the function.

I hope this helps.

bakunin
# 7  
Old 11-23-2017
Quote:
Originally Posted by bakunin
You can also use a subfunction to parse the command line and feed it the string:

Code:
parse_cmd ()
{
while getopts :t:r:d args; do
    case $args in
        t) typeset VERSION="$OPTARG" ;;
        r) typeset HOST="$OPTARG" ;;
        d) typeset DEBUG="true"; typeset DEBUG="DEBUG" ;;
        *) print "Invalid option specified." ;;
    esac
done
shift $((OPTIND-1));

return 0
}


...
# main()
STR="<your arguments here in the same form as parameters from stdin>"

parse_cmd $STR    # process opts and args from STR

parse_cmd $*      # process opts and args from stdin

exit 0

Note that if you use a subfunction like above you need to get the parsed values out of the function somehow, because the variables in it are now local to the function.

I hope this helps.

bakunin
Hi bakunin,
Note that typeset is not in the standards and the way it behaves varies some between shells. In bash declaring a variable with local or with typeset makes the variable definition have a scope that is local to the function and values assigned to such variables are not visible when the function returns to its caller. But, a variable that is not declared with local nor with typeset has a global scope and if such a variable is assigned a value inside a function, the value assigned in the function will still be visible when the variable is expanded outside of the function.

In ksh, declaring a variable with typeset in a function does not give it a local scope; it has a global scope. The ksh command language does not have a local keyword or built-in function.

So, in both ksh and bash, if you change:
Code:
        t) typeset VERSION="$OPTARG" ;;
        r) typeset HOST="$OPTARG" ;;
        d) typeset DEBUG="true"; typeset DEBUG="DEBUG" ;;
        *) print "Invalid option specified." ;;

in your sample to:
Code:
        t) VERSION="$OPTARG" ;;
        r) HOST="$OPTARG" ;;
        d) DEBUG="DEBUG" ;;
        *) print "Invalid option specified." ;;

The values assigned to VERSION, HOST, and DEBUG in the function parse_cmd can be found after parse_cmd completes by just expanding those variables with $VERSION, $HOST, and $DEBUG.

Note that I removed the 1st assignment to DEBUG. I don't see any reason to assign it the value TRUE and then immediately assign another value (DEBUG) to it.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell function parsing arguments, which didn't receive. Why?

I have a shell script, which has a function, which call a perl script. Also it has a shell function call parse_ars as fogllow: #!/bin/ksh load_ici_dat() { cd ${OUTPUT_DIR} typeset batch_file=XRED${MM}${DD} typeset SCRIPT="${PROG}.pl" echo "time ${PERLEXE5X} ${PERL_HOME}/${SCRIPT}... (1 Reply)
Discussion started by: digioleg54
1 Replies

2. Shell Programming and Scripting

Parsing Command Line Arguments In C shell script

]I have a string like "/abc/cmind/def/pq/IC.2.4.6_main.64b/lnx86" and this string is given by user. But in this string instead of 64b user may passed 32 b an i need to parse this string and check wether its is 32b or 64 b and according to it i want to set appropriate flags. How will i do this... (11 Replies)
Discussion started by: codecatcher
11 Replies

3. Programming

Parsing command line arguments in Python

Hi, I've a python script called aaa.py and passing an command line option " -a" to the script like, ./aaa.py -a & Inside the script if the -a option is given I do some operation if not something else. code looks like ./aaa.py -a . . if options.a ---some operation--- if not options.a... (1 Reply)
Discussion started by: testin
1 Replies

4. Shell Programming and Scripting

ksh script that will accept arguments

Hi, I am not very skilled using ksh scripts. How do I create a ksh script that will accept arguments and use them in the script ? I need to make this: Run this command with this argument: ./mykshprogram.ksh madsen and sometimes I need to do this: Run the ksh again with 2... (3 Replies)
Discussion started by: hasselhaven
3 Replies

5. Shell Programming and Scripting

Need help with parsing the arguments using getopts !!

Hi all, I am trying to use long arguments for my existing script. right now my script would work if given <script_name> -t <arg1> -b <arg2> -v <arg3>. The script code is shown below. while getopts t:v:b: OPT;do case "$OPT" in t) Todo=$OPTARG;; b) Batch=$OPTARG;; ... (3 Replies)
Discussion started by: SSSB
3 Replies

6. Shell Programming and Scripting

Parsing Comma separated Arguments

Hi unix guru's I want to execute a shell script like ksh printdetails.ksh Andy,Bob,Daisy,Johnson like passing all the four names in the as the arguments and these arguments are varies between 1 to 10. How to pass these names to the shell script variable. and also i want to know the count... (4 Replies)
Discussion started by: Reddy482
4 Replies

7. Shell Programming and Scripting

Help parsing command line arguments in bash

Looking for a little help parsing some command line arguments in a bash script I am working on, this is probably fairly basic to most, but I do not have much experience with it. At the command line, when the script is run, I need to make sure the argument passed is a file, it exists in the... (3 Replies)
Discussion started by: Breakology
3 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. UNIX for Advanced & Expert Users

Parsing the command line arguments

Is there a way to get the command line arguments. I am using getopt(3) but if the arguments are more than one for a particular option than it just ignores the second argument. For eg ./a.out -x abc def now abd will be got with -x using getopt "( x : )" and string abc\0def will get stored... (7 Replies)
Discussion started by: jayakhanna
7 Replies
Login or Register to Ask a Question