case statement for different cmd arguments


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting case statement for different cmd arguments
# 1  
Old 02-16-2012
case statement for different cmd arguments

Hello friends,

I have a boubt passing different arguments at a time for any one option in below code.
I would also like to check which option has been selected (any one of i, r, u ) so that whether or not matching argument passed can be verified.

for i and r - install and re-install - I want to pass directory path
and for u -upgrade - I want to pass list of patches to be upgraded.

Is there a way I can catch i, r, u option value in a variable and match them with the argument pass for that option.

Code:
while getopts :iru: OPTION
        do
         case "$OPTION" in
          i) install="$path install_function" ;;
          r) reinstall="$path Reinstall_Function"     ;;
          u) patch_upgrade=$OPTARG upgrade_function ;;
          :) echo "$0: $OPTARG option missing argument!"
             exit 2 ;;
          ?) echo "$0: $OPTARG is an invalid option!"
             echo "$0 -ab -c optarg patch_list"
             exit 2 ;;
         esac
         done
       shift $OPTIND-1



Any help would be highly appreciated.

Thanks in advance

PD2


Moderator's Comments:
Mod Comment How to use code tags

Last edited by Franklin52; 02-16-2012 at 03:43 AM.. Reason: Please use code tags for code and data samples, thank you
# 2  
Old 02-27-2012
Hello friends,

Your help is highly appreciated to get list of all parameters ($@) passing as an argument to option U in below code - patch_upgrade. Here I am passing list of patches as an argument ( patch count may vary ) for option -u but not sure, what I am missing , I am not able to pass all argument $OPTARG accepts first one only. Where as I wanted a complete list to get l on OPTARG. Please help me correcting below case statement.

Code:
install=0
reinstall=""
patch_upgrade=""
# if install and re-install dont need argument but upgrade need
while getopts :iru: OPTION
       do
         case "$OPTION" in
          i ) install="yes"   ;;
          r ) reinstall="on"     ;;
          u ) patch_upgrade=$OPTARG  ;;   # patch_list
          : ) echo "$0: $OPTARG option missing argument!"
              exit 2 ;;
          ? ) echo "$0: $OPTARG is an invalid option!"
              echo "$0 -ir -u optarg patch_list"
              exit 2 ;;
         esac
       done
       shift $((OPTIND-1)); OPTIND=1

if [ $install = "yes" ] ; then
 echo " call install function"
fi 
 
if [ $reinstall = "on" ] ; then
 echo " call Re-install function"
fi
 
 if [ $patch_upgrade = $OPTARG ] ; then
  echo " call patch_Upgrade function "
  echo " list of patches is $OPTARG"
  echo " array of patch list is $@"
 fi

Thank you very much in advance.
-Pd

Last edited by methyl; 02-27-2012 at 09:12 PM.. Reason: please use code tags
# 3  
Old 02-27-2012
Seems like every post you have made here has required a moderator to come along and update after you to add Code Tags, they are a lot more patient than me.

Have you considered using a quoted string for list of patches eg:

Code:
$ your_code -i -u "patch1 patch3 patch201" testit
Installing testit
Applying patch1.....
Applying patch3.....
Applying patch201.....
All done


Last edited by Chubler_XL; 02-27-2012 at 09:08 PM..
# 4  
Old 02-27-2012
DOH!! How I can be so stupid not tried "" or '' ..both are working . but Isn't there a way without puting " " or '' we can treat all passing argument as a string that optarg shud hold.

Any way thank you Chubler , for your quick respond and pointing my mistake. $$ to you .

cheers
Pramod
# 5  
Old 02-27-2012
Well it's not really the standard way unix commands get their args. Usually any list of optional items appear as the last args eg:

Code:
command [-i] [-r] [patch [patch ...]]

So everything after last passed arg is assumend to be a patch.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

If statement arguments

I'm stuck on a particular problem and need some guidance. I have a file with a name and a phone number in it (teledir.txt). I need to do a $# in a separate script to take a positional parameter and check to see if it is in the file. To quote the question: If one argument is supplied, check... (6 Replies)
Discussion started by: Eric7giants
6 Replies

2. Shell Programming and Scripting

Arguments in usage statement

Hello, I have a question regarding the usage statement of a script. I have 2 parameters "--pto" and "--pto_list". To start the script I will need one of them. Both together are not possible. How this would be printed out within a usage statement? My suggestion would be: Usage:... (4 Replies)
Discussion started by: API
4 Replies

3. Shell Programming and Scripting

Case -- esac number of arguments problem

hi Scripting experts, I am using case..esac in my script .. I have given 6 option..e.g. 1 2 3 4 5 and *, howerver my script works welll for 1st 4 options but for 5 it considers * and exists. Is there a maximum limit on number of options given for case ..esac? (1 Reply)
Discussion started by: sdgawande
1 Replies

4. 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

5. Shell Programming and Scripting

Problem assigning cmd output to variable then using in IF statement

Hi, I'm using the bourn shell on a Sun Solaris Unix system. I am relatively new to UNIX scripting so please bear with me... I'm having a couple issues: 1) I need to have a variable $FSIZE set with the output of a command each time the script runs. (the command looks for a file and... (8 Replies)
Discussion started by: dqrgk0
8 Replies

6. Shell Programming and Scripting

How to use case and command line arguments in shell script?

Hi... can anyone please help me out in using the CASE and command line argument in shell script... i am bit new to shell scripting...below i have explained my proble with example... say i have an executable file with name 'new1.sh' and there are 3 functions in it a(), b() and c()....and there... (5 Replies)
Discussion started by: swap21783
5 Replies

7. Shell Programming and Scripting

tar cmd how many arguments into parameters of filenames

Hi I would like to use tar cmd in my script. I have a variable with filenames, e.g. 1000 records and I would like to paste its values into tar cmd. For this example I used three elements variable strings. strings="file1.txt file2.txt file3.txt" `tar cf file1.tar $strings` Whether... (1 Reply)
Discussion started by: presul
1 Replies

8. Shell Programming and Scripting

help with case statement

I am writing a script to pull diskspace information from our servers. Here is the script that I wrote: #!/bin/ksh for host in `cat /oper/hosts/esc.misc` do ssh -q -o ConnectTimeout=10 operator@$host df -h|grep "/dev/" |egrep '8%|9%|100%' | awk '{print H " " "at " $5 " with " $4 "... (1 Reply)
Discussion started by: rkruck
1 Replies

9. UNIX for Dummies Questions & Answers

If or Case Statement

I want to write a program with the following variables: a=7000 b=24000 c=613.8 The user can enter two words: Vivid or Blue for example. The challenge is that the user might not want to write the words the way they appear. The user can write V or v or vivid or Vivid or write Blue or blue, or B,... (1 Reply)
Discussion started by: Ernst
1 Replies

10. Shell Programming and Scripting

if statement - how can I do 2 arguments?

I am new to shell, and I am trying to do a if statement like the following: if ; then basically it works fine if both arguments of the if are met, however the next elif is: elif ; then if the conditions of the elif are met, then it says "final1.sh: line 67: [: too many arguments" ... (6 Replies)
Discussion started by: Darklight
6 Replies
Login or Register to Ask a Question