Two arguments for optional switch


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Two arguments for optional switch
# 1  
Old 04-28-2011
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

Code:
${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
# 2  
Old 04-28-2011
Requirement not very clear. However, try googling on getopt.

regards,
Ahamed
# 3  
Old 04-28-2011
Well, you could start with this, and modify accordingly:
Code:
#!/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.
# 4  
Old 04-28-2011
I have posted some wrong phrase in my previous post..

It should be something in this way .

Code:

${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

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

If $?=0

It does the gzgrep -i STRING LOG_FILE_Group* > OutputFile

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Getopts with optional parameters

Hi Unix Gurus, i am on learning path of unix, and yet to discover many things. I came across with this requirement where i need to pass parameters but the position of parameters is not fixed so after doing some google search got to know "getopts" can handle that. So here is my code: function... (3 Replies)
Discussion started by: gnnsprapa
3 Replies

2. Red Hat

Linux Optional Packages

Please forgive but I am new to Linux and still learning. When installing Linux (any flavor) over PXE, it asks if you want to customize which packages to install. Most engineers ask us to install all packages but this entails A LOT of clicking. Is there a way to "select all" packages by using a... (3 Replies)
Discussion started by: svolbruck
3 Replies

3. Programming

Passing arguments from command line to switch case statement in C

Hi Am pretty new to C.. Am trying to pass the arguments from command line and use them in switch case statement.. i have tried the following #include <stdlib.h> main(int argc, char* argv) { int num=0; if ( argc == 2 ) num = argv; printf("%d is the num value",num); switch ( num ) ... (2 Replies)
Discussion started by: Priya Amaresh
2 Replies

4. Shell Programming and Scripting

How to make shell script arguments optional?

Here is my script: #!/bin/ksh usage () { echo " Usage: $0 <opt1> <opt2> <opt3> <opt4>" } if ; then usage exit; fi prog -a $1 -b $2 -c $3 -d $4 2>&1 | tee -a ~/$1.log I want argument 4 to be optional, so if there's no argument for opt4, that it doesn't... (8 Replies)
Discussion started by: guitarscn
8 Replies

5. Shell Programming and Scripting

Usage: optional and mandatory arguments

I have an awk script which can be used in the following ways: xi and xf will only be mandatory when processing the file fin.zc. awk -v xi=0/-0.5 -v xf=80/30 -f ./zc2cmd.awk fin.zc > fout.cmod awk -f ./zc2cmd.awk -u awk -f ./zc2cmd.awk --usg awk -f ./zc2cmd.awk -e awk -f ./zc2cmd.awk... (1 Reply)
Discussion started by: kristinu
1 Replies

6. Shell Programming and Scripting

using switch on multiple arguments

I have a switch statement, and I want to have two options performing the same thing. For example, if $opt matches "-fb" or "--fbase", I want to perform the same operation. How can I include various matches in "case" ? switch ($opt) case "-T": set Atpath = $par set opt_tpath =... (8 Replies)
Discussion started by: kristinu
8 Replies

7. Shell Programming and Scripting

Optional Parameters/arguments while executing a script.

Hello, I have a shell script "Test.ksh" and I need to pass 8 parameters/arguments while executing the script ./Test.ksh 1 2 3 4 5 6 7 8 Out of these I want first 3 to be compulsory and rest 5 to be optional. Can you suggest the way to do this like and also how to pass these optional... (3 Replies)
Discussion started by: indrajit_u
3 Replies

8. Shell Programming and Scripting

grep with regular expression optional value

field 12345 12345 field 12345 field 12345 123456 last fleld (from three) is optional, but if it occures It has to be composed only of nummbers and maximum 5 positions long. If I use: grep "^field \{5\}" I get: field 12345 12345 field 12345 field 12345 123456 But I wont... (11 Replies)
Discussion started by: necroman08
11 Replies

9. Shell Programming and Scripting

How to make parameters optional?

Hi, I am trying to call a function inside a shell script. Is there a way in which I can make the parameters options in the call? Please help me with this. Thanks!!! (2 Replies)
Discussion started by: neeto
2 Replies
Login or Register to Ask a Question