-2

system: freebsd 9.2

I want to use getopts to parse command line. Such as

./tesh.sh -o good a.c  
can get  good.out

./tesh.sh -o a.c   
can get a.out

But in getopts there is no "option" variable. Does anyone know how to solve this problem? I found lots of webpages but no one can solve it.

here is my code

#!/bin/bash


while getopts "hs:o:" optname
do
    case "$optname" in
    "h")
        echo "Option $optname is specified"
        ;;
    "s")
        echo "Option $optname has value $OPTARG"
        ;;
    "o")    
        echo "$optname"
        ;;
    "?")
        echo "Unknown option $OPTARG"
        ;;
    ":")
    ;;
    *)
        # Should not occur
        echo "Unknown error while processing options"
        ;;
    esac
    echo "OPTIND is now $OPTIND"
done    

if i type ./test.sh -o a.c it will print echo "$optname"

and i want that if i type ./test.sh -o can also print echo "$optname"

now it will print wrong message

thx!

  • Explain more about your scenario as it is not clear and confusion what your requirement is. – Sriharsha Kalluru Nov 07 '14 at 05:25
  • See [Shell script templates](http://stackoverflow.com/questions/430078/shell-script-templates/430680#430680); one part of the answer has an outline of how to use `getopts` to parse a command line. You may also find useful information in [Using `getopts` in Bash shell script to get long and short command line options](http://stackoverflow.com/questions/402377/using-getopts-in-bash-shell-script-to-get-long-and-short-command-line-options/402413#402413) helpful too. – Jonathan Leffler Nov 07 '14 at 05:28
  • @SriharshaKalluru thx ! i add more about my question – Zi-Yuan Liu Nov 07 '14 at 08:36
  • @JonathanLeffler thx! but i can find the solution what i want =( – Zi-Yuan Liu Nov 07 '14 at 08:37
  • 2
    If you define "-o" option requiring an argument and you do not provide an argument, you'll get an error message. – Jdamian Nov 07 '14 at 08:44
  • thx so i want to know if any way to solve this such gcc complier gcc -o great.out a.c it will generally great.out and gcc -o a.c it will generally a.out – Zi-Yuan Liu Nov 07 '14 at 08:59
  • Are you asking for an optional output file name after the `-o` option? If so, it will be hard to code. The way getopt and getopts work will use the `a.c` name in your `script -o a.c` command line as the argument to `-o`. You would have to analyze the name and decide it isn't the output filename and arrange to (1) insert the correct derived name and (2) process whatever got treated as the option argument correctly. Messy, at best. There are so many pitfalls on the way that I would not try to do it. – Jonathan Leffler Nov 07 '14 at 14:34
  • @JonathanLeffler OK! I will find the other way ! thanks a lot – Zi-Yuan Liu Nov 07 '14 at 15:09

1 Answers1

0

For your requirement you need not use getopts and you can use the following code to make it happen.

If only -o is given it will be treated as a flag. If -o is givne with any value then it is treated as option.

> cat test
#!/bin/bash

while [ $# -gt 0 ]; do
case $1 in
        -h) echo "Option $optname is specified" ;;
        -s) VAL_S=$2; echo "Option $optname has value $VAL_S" ;shift;;
        -o) VAL_O=$2; F=$(echo $VAL_O |cut -c1);
            if [ "$F" = "-" -o -z "$F" ]; then
                echo "-o is a flag"
                DEFAULT=a.c
            else
                echo "-o is a option"
                DEFAULT=$VAL_O
                shift
            fi
        ;;
        *) echo "Unknown option $OPTARG" ;exit ;;
esac
shift
done

I ran it in the possible cases.

> ./test -o
-o is a flag
> ./test -o a.c
-o is a option
> ./test -o -h
-o is a flag
Option  is specified
> ./test -o a.c -h
-o is a option
Option  is specified
Sriharsha Kalluru
  • 1,743
  • 3
  • 21
  • 27