|
|
|
|
||||||
| comp.unix.shell Using and programming the Unix shell. |
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
Hi everyone,
I am trying to create a scritp which will run with either one argument or with no arguments, like this $ .myscript -m or $ .myscript My script should run in one way if I give -m option and in other way if I dont give any options at all. Is it possible with getopts? Thanks, RB |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
On 31 Jul., 17:00, rach...@gmail.com wrote:
> Hi everyone, > > I am trying to create a scritp which will run with either one argument > or with no arguments, like this > > $ .myscript -m > > or > > $ .myscript > > My script should run in one way if I give -m option and in other way > if I dont give any options at all. Is it possible with getopts? Sure it is. Haven't you read the man page? while getopts m opt do case $opt in m) handle_m;; *) handle_default;; esac done shift $(($OPTIND-1)) : handle rest of args in "$@" (I hope I made no typos. And have a look into the docs how to handle wrong input.) Janis > > Thanks, > RB |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
On 31 Jul., 17:00, rach...@gmail.com wrote:
> Hi everyone, > > I am trying to create a scritp which will run with either one argument > or with no arguments, like this > > $ .myscript -m > > or > > $ .myscript > > My script should run in one way if I give -m option and in other way > if I dont give any options at all. Is it possible with getopts? Sure it is. Haven't you read the man page? while getopts m opt do case $opt in m) handle_m;; *) handle_default;; esac done shift $(($OPTIND-1)) : handle rest of args in "$@" (I hope I made no typos. And have a look into the docs how to handle wrong input.) Janis > > Thanks, > RB |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
On Jul 31, 11:00 am, rach...@gmail.com wrote:
> Hi everyone, > > I am trying to create a scritp which will run with either one argument > or with no arguments, like this > > $ .myscript -m > > or > > $ .myscript > > My script should run in one way if I give -m option and in other way > if I dont give any options at all. Is it possible with getopts? > > Thanks, > RB Nevermind I figured it out, here it is $ cat testgetopts #!/bin/ksh #Test getopts with one/more or no argument if getopts :m options then case $options in m) echo " You entered -m as an option";; \?) echo "Not a legal option";; esac else echo "You can run this without -m option" fi |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
On Jul 31, 11:00 am, rach...@gmail.com wrote:
> Hi everyone, > > I am trying to create a scritp which will run with either one argument > or with no arguments, like this > > $ .myscript -m > > or > > $ .myscript > > My script should run in one way if I give -m option and in other way > if I dont give any options at all. Is it possible with getopts? > > Thanks, > RB Nevermind I figured it out, here it is $ cat testgetopts #!/bin/ksh #Test getopts with one/more or no argument if getopts :m options then case $options in m) echo " You entered -m as an option";; \?) echo "Not a legal option";; esac else echo "You can run this without -m option" fi |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
On Jul 31, 11:27 am, rach...@gmail.com wrote:
> On Jul 31, 11:00 am, rach...@gmail.com wrote: > > > > > > > Hi everyone, > > > I am trying to create a scritp which will run with either one argument > > or with no arguments, like this > > > $ .myscript -m > > > or > > > $ .myscript > > > My script should run in one way if I give -m option and in other way > > if I dont give any options at all. Is it possible with getopts? > > > Thanks, > > RB > > Nevermind I figured it out, here it is > > $ cat testgetopts > #!/bin/ksh > #Test getopts with one/more or no argument > > if getopts :m options > then > case $options in > m) echo " You entered -m as an option";; > \?) echo "Not a legal option";; > esac > else > echo "You can run this without -m option" > fi- Hide quoted text - > > - Show quoted text - Thanks Janis, That was really quick response. Regards, RB |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
On Jul 31, 11:27 am, rach...@gmail.com wrote:
> On Jul 31, 11:00 am, rach...@gmail.com wrote: > > > > > > > Hi everyone, > > > I am trying to create a scritp which will run with either one argument > > or with no arguments, like this > > > $ .myscript -m > > > or > > > $ .myscript > > > My script should run in one way if I give -m option and in other way > > if I dont give any options at all. Is it possible with getopts? > > > Thanks, > > RB > > Nevermind I figured it out, here it is > > $ cat testgetopts > #!/bin/ksh > #Test getopts with one/more or no argument > > if getopts :m options > then > case $options in > m) echo " You entered -m as an option";; > \?) echo "Not a legal option";; > esac > else > echo "You can run this without -m option" > fi- Hide quoted text - > > - Show quoted text - Thanks Janis, That was really quick response. Regards, RB |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
rachit7@gmail.com wrote:
> On Jul 31, 11:27 am, rach...@gmail.com wrote: > >>On Jul 31, 11:00 am, rach...@gmail.com wrote: >> >> >> >> >> >> >>>Hi everyone, >> >>>I am trying to create a scritp which will run with either one argument >>>or with no arguments, like this >> >>>$ .myscript -m >> >>>or >> >>>$ .myscript >> >>>My script should run in one way if I give -m option and in other way >>>if I dont give any options at all. Is it possible with getopts? >> >>>Thanks, >>>RB >> >>Nevermind I figured it out, here it is >> >>$ cat testgetopts >>#!/bin/ksh >>#Test getopts with one/more or no argument >> >>if getopts :m options >>then >> case $options in >> m) echo " You entered -m as an option";; >> \?) echo "Not a legal option";; >> esac >>else >>echo "You can run this without -m option" >>fi- Hide quoted text - >> >>- Show quoted text - > > > Thanks Janis, > > That was really quick response. You're welcome. If you want your script extensible just change the if construct to a while loop and add the 'shift' to be able to process the rest of the arguments. Then you can use that same pattern for all your scripts. Janis > > Regards, > RB > |
|
![]() |
| Outils de la discussion | |
|
|