Results 1 to 5 of 5
  1. #1
    Join Date
    Jun 2003
    Posts
    35

    Unanswered: parameter processing

    Hi Anyone,

    if i have a script that expecting a parameter such as:

    > ./myscript.sh -Uuser -Ppassword -Ffilename


    How do i process those parameter to get the user,password and filename?

    if i use $1 or $2, i will get -Uuser and -Ppassword not user / password.

    Are there any unix command that might be usefull to do this?


    Thanks.
    Rookie

  2. #2
    Join Date
    Aug 2001
    Location
    UK
    Posts
    4,650

    Re: parameter processing

    Search for
    ksh getopt
    in google ...

    From a search result,

    The following code fragment shows how one might process the arguments for
    a command that can take the options a or b, as well as the option o,
    which requires an argument:

    set -- `getopt abo: $*`
    if [ $? != 0 ]
    then
    echo $USAGE
    exit 2
    fi
    for i in $*
    do
    case $i in
    -a | -b) FLAG=$i; shift;;
    -o) OARG=$2; shift 2;;
    --) shift; break;;
    esac
    done

    This code accepts any of the following as equivalent:

    cmd -aoarg file file
    cmd -a -o arg file file
    cmd -oarg -a file file
    cmd -a -oarg -- file file
    Visit the new-look IDUG Website , register to gain access to the excellent content.

  3. #3
    Join Date
    Jun 2002
    Location
    UK
    Posts
    525
    Depends on the shell. You can always do something messy and check for -P/U/F and then 'cut -c2-'.

    If you have bash or ksh (and probably some others but I'm ignorant), you can use 'getopts'.

    An example below:

    ------------------------------------------------------
    # getoptsScript

    while getopts ":abc:d:" opt
    do
    case $opt in
    a) echo "Option a has been passed"
    optionA=true
    ;;
    b) echo "Option b has been passed"
    optionB=true
    ;;
    c) echo "Option c has been passed"
    optargC=$OPTARG
    echo "Option c has argument $optargC"
    ;;
    d) echo "Option d has been passed"
    optargD=$OPTARG
    echo "Option d has argument $optargD"
    ;;
    *) echo "Invalid option - $opt"
    ;;
    esac
    done
    shift $((OPTIND - 1))

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

    This script takes options a, b, c and d.

    getoptScript -a -b OR getoptScript -ab is exactly the same

    The options list (":abc:d:") holds the valid options. Options c and d are suffixed with a ":" which indicates that they require an option argument.

    getoptScript -abc "An argument for c" -d "An argument for d"

    The $OPTIND variable is incremented when options are processed. If you shift your input parameters by $OPTIND after processing your options, any additional parameters would be set to $1, $2...

    e.g.

    getoptScript -c "An argument for c" some other values

    Would hold some, other and values in $1, $2 and $3 respectively.

    HTH
    Last edited by Damian Ibbotson; 07-31-03 at 07:38.

  4. #4
    Join Date
    Aug 2003
    Location
    Argentina
    Posts
    780

    Re: parameter processing

    Originally posted by msetjadi
    Hi Anyone,

    if i have a script that expecting a parameter such as:

    > ./myscript.sh -Uuser -Ppassword -Ffilename


    How do i process those parameter to get the user,password and filename?

    if i use $1 or $2, i will get -Uuser and -Ppassword not user / password.

    Are there any unix command that might be usefull to do this?


    Thanks.
    Rookie
    Hi Rookie,
    I do not have left very clear.
    To see if this helps you.
    For example:

    usu=$1
    pass=$2
    file=$3
    ...............

    or

    Using ksh

    echo "Username: \c"
    read usr
    echo off
    echo "Password: \c"
    read pass
    echo on
    echo "File name: \c"
    read file
    .......................

    Gustavo.

  5. #5
    Join Date
    Aug 2003
    Location
    Argentina
    Posts
    780

    Re: parameter processing

    Originally posted by gurey
    Hi Rookie,
    I do not have left very clear.
    To see if this helps you.
    For example:

    usu=$1
    pass=$2
    file=$3
    ...............

    or

    Using ksh

    echo "Username: \c"
    read usr
    echo off
    echo "Password: \c"
    read pass
    echo on
    echo "File name: \c"
    read file
    .......................

    Gustavo.
    Sorry Rookie

    The correct form is

    Using ksh

    echo "Username: \c"
    read usr
    echo "Password: \c"
    echo off
    read pass
    echo on
    echo "File name: \c"
    read file

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •