Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

ksh parsing arguments in a string rather than from the cmdln

Shell Programming and Scripting


Tags
ksh arguments, rhel 6, solved

Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 3 Weeks Ago   -   Original Discussion by user052009
user052009's Unix or Linux Image
user052009 user052009 is offline
Registered User
 
Join Date: Nov 2016
Last Activity: 21 November 2017, 11:49 AM EST
Posts: 57
Thanks: 25
Thanked 1 Time in 1 Post
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"

Sponsored Links
    #2  
Old Unix and Linux 3 Weeks Ago   -   Original Discussion by user052009
Don Cragun's Unix or Linux Image
Don Cragun Don Cragun is offline Forum Staff  
Administrator
 
Join Date: Jul 2012
Last Activity: 12 December 2017, 6:43 AM EST
Location: San Jose, CA, USA
Posts: 10,769
Thanks: 590
Thanked 3,769 Times in 3,217 Posts
Quote:
Originally Posted by pnolanch View Post
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"

Sponsored Links
    #3  
Old Unix and Linux 3 Weeks Ago   -   Original Discussion by user052009
user052009's Unix or Linux Image
user052009 user052009 is offline
Registered User
 
Join Date: Nov 2016
Last Activity: 21 November 2017, 11:49 AM EST
Posts: 57
Thanks: 25
Thanked 1 Time in 1 Post
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 Unix and Linux 3 Weeks Ago   -   Original Discussion by user052009
Corona688's Unix or Linux Image
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 11 December 2017, 5:38 PM EST
Location: Saskatchewan
Posts: 22,546
Thanks: 1,159
Thanked 4,286 Times in 3,955 Posts
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.
The Following User Says Thank You to Corona688 For This Useful Post:
user052009 (3 Weeks Ago)
Sponsored Links
    #5  
Old Unix and Linux 3 Weeks Ago   -   Original Discussion by user052009
user052009's Unix or Linux Image
user052009 user052009 is offline
Registered User
 
Join Date: Nov 2016
Last Activity: 21 November 2017, 11:49 AM EST
Posts: 57
Thanks: 25
Thanked 1 Time in 1 Post
yep ... that does the job. Many thanks.
The Following User Says Thank You to user052009 For This Useful Post:
Corona688 (3 Weeks Ago)
Sponsored Links
    #6  
Old Unix and Linux 2 Weeks Ago   -   Original Discussion by user052009
bakunin's Unix or Linux Image
bakunin bakunin is offline Forum Staff  
Bughunter Extraordinaire
 
Join Date: May 2005
Last Activity: 10 December 2017, 6:03 PM EST
Location: In the leftmost byte of /dev/kmem
Posts: 5,647
Thanks: 109
Thanked 1,613 Times in 1,184 Posts
Quote:
Originally Posted by user052009 View Post
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
Sponsored Links
    #7  
Old Unix and Linux 2 Weeks Ago   -   Original Discussion by user052009
Don Cragun's Unix or Linux Image
Don Cragun Don Cragun is offline Forum Staff  
Administrator
 
Join Date: Jul 2012
Last Activity: 12 December 2017, 6:43 AM EST
Location: San Jose, CA, USA
Posts: 10,769
Thanks: 590
Thanked 3,769 Times in 3,217 Posts
Quote:
Originally Posted by bakunin View Post
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.
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Parsing command line arguments in Python testin Programming 1 12-16-2011 03:46 PM
Need help with parsing the arguments using getopts !! SSSB Shell Programming and Scripting 3 05-11-2010 01:13 PM
Parsing Comma separated Arguments Reddy482 Shell Programming and Scripting 4 11-16-2009 03:28 AM
Parsing of file for Report Generation (String parsing and splitting) umar.shaikh Shell Programming and Scripting 8 03-02-2009 01:38 AM
Parsing the command line arguments jayakhanna UNIX for Advanced & Expert Users 7 12-17-2003 07:42 PM



All times are GMT -4. The time now is 01:48 PM.