Find the answer to your Linux question:
Results 1 to 2 of 2
I am having a shell script that is triggered by an external program. This external program starts the shell script with a few options and values. The problem here is ...
  1. #1
    Just Joined!
    Join Date
    Mar 2009
    Posts
    5

    Handling options with null values in bash

    I am having a shell script that is triggered by an external program. This external program starts the shell script with a few options and values. The problem here is that the external script does not always fill all the options with values,it sometimes leaves option on the command line without any value attached to it. In that case the options which does not have a value assumes the next option to be its value

    ************Program code************************************
    cat testing
    #!/bin/bash
    while getopts c:l:g:u:s:t:a:e:y:z option
    do
    case ${option} in
    c) c=${OPTARG};;
    l) l=${OPTARG};;
    s) s=${OPTARG};;
    g) g=${OPTARG};;
    t) t=${OPTARG};;
    a) a=${OPTARG};;
    e) e=${OPTARG};;
    u) u=${OPTARG};;
    y) y=${OPTARG};;
    z) z=${OPTARG};;
    \?) echo " usage error"
    exit;
    esac
    done

    echo "c=$c"
    echo "l=$l"
    echo "s=$s"
    echo "t=$t"
    echo "a=$a"
    echo "e=$e"
    echo "g=$g"
    echo "u=$u"
    echo "y=$y"
    echo "z=$z"
    **********************End of program**************************

    eg :-
    ./testing -a val1 -l val2 -g -u val4
    output:-
    c=
    l=val2
    s=
    t=
    a=val1
    e=
    g=-u
    u=
    y=
    z=


    Here we see that -g option assumes that -u is a value fed to it. This way I am loosing out on the last value val4. Is there a work around for this problem in bash?

  2. #2
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
    Posts
    1,117
    Hi.

    Welcome to the forums.

    It's much easier to read code if you use CODE tags -- select the text in the editing / composing window and then click the # just above the window.

    Also no need to shout, most of us can read the standard-size text.

    There is an external form of option processing. Here's an example of use:
    Code:
    #!/usr/bin/env bash
    
    # @(#) s2       Demonstrate external getopt.
    
    echo
    set +o nounset
    LC_ALL=C ; LANG=C ; export LC_ALL LANG
    echo "Environment: LC_ALL = $LC_ALL, LANG = $LANG"
    echo "(Versions displayed with local utility \"version\")"
    version >/dev/null 2>&1 && version "=o" $(_eat $0 $1) getopt
    set -o nounset
    
    echo
    echo " Results:"
    
    echo
    echo " No arguments:"
    ./getopt-external -c1 -a
    
    echo
    echo " -c, no optional arg:"
    ./getopt-external -c -a
    
    echo
    echo " -c, no immediate following arg:"
    ./getopt-external -c 1 -a
    
    exit 0
    producing:
    Code:
    % ./s2
    
    Environment: LC_ALL = C, LANG = C
    (Versions displayed with local utility "version")
    OS, ker|rel, machine: Linux, 2.6.11-x1, i686
    Distribution        : Xandros Desktop 3.0.3 Business
    GNU bash 2.05b.0
    getopt (enhanced) 1.1.3
    
     Results:
    
     No arguments:
    Option c, argument `1'
    Option a
    Remaining arguments:
    
     -c, no optional arg:
    Option c, no argument
    Option a
    Remaining arguments:
    
     -c, no immediate following arg:
    Option c, no argument
    Option a
    Remaining arguments:
    --> `1'
    You might notice that the optional argument needs to be next to the option letter. Otherwise how could it tell what is legitimate and what is not?

    You can find this code at: Getopt: Parse command-line arguments from shell scripts

    It has other advantages, such as processing long arguments. You will, however, need to do some work -- get it, compile it, install it.

    If you think this is too non-standard or too much work, I think the only other solution is to parse everything yourself. This usually means a long case statement. You are then not dependent on anything else, so you have good portability. I did this a few weeks ago when I thought users might miss an option. I checked for a leading "-" in the option.

    Best wishes ... cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

Posting Permissions

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