Two arguments for optional switch

How to declare the two argument for optional switch ?

I have a script that search for a string in current or old zipped log file.
Im using a option something like this

${basename} [ -c <CRRNT_LOG_FILE> | -o <DATE:YYYY-MM-DD> <OLD_LOG_FILE> ]

Since $1 can have only one argument should be passed when user select swicth -c and -o need to have mandatory two arguments YYYY-mm-dd & OLD_FILE_NAME in case of -o switch.

Can we have this method ?? please suggest. Thanks in advance

Requirement not very clear. However, try googling on getopt.

regards,
Ahamed

Well, you could start with this, and modify accordingly:

#!/bin/ksh
badopt=0
current=
olddate=

while getopts c:o: opt
do
    case $opt in
        c)      current="$OPTARG";;
        o)      olddate="$OPTARG";;
        ?)      badopt=1;;
    esac
done
shift $((OPTIND - 1))

[ -z "$current" -a -z "$olddate" ] && badopt=1
[ -n "$olddate" -a $# -ne 1 ] && badopt=1

if [ $badopt -eq 1 ]; then
    echo "Syntax: $(basename $0) [ -c <CRRNT_LOG_FILE> | -o <DATE:YYYY-MM-DD> <OLD_LOG_FILE> ]" >&2
    exit 1
fi

if [ -n "$current" ]; then
    echo current log file: $current
elif [ -n "$olddate" ]; then
    old="$1"
    echo old log date: $olddate
    echo old log file: $old
fi

exit 0

Best regards,
Mark.

I have posted some wrong phrase in my previous post..

It should be something in this way .


${basename} [ -c <LOG_FILE> | -o <DATE:YYYY-MM-DD> <LOG_FILE> ]

The only difference between Current Log & Archive Log is that its suffix.

Current Log Format : LOG_FILE_Group1, LOG_FILE_Group2, LOG_FILE_Group3
Archive Log Format : LOG_FILE_Group[1-3]_2011-02-12.log.gz

If script running with option -o
Firstly It will check with DATE
Secondly It will Check with LOG_FILE

ls -ltr | grep -i 2011-02-12 | grep -i LOG_FILE_Group*

If $?=0

It does the gzgrep -i STRING LOG_FILE_Group* > OutputFile