PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Serveur - Sécurité et techniques > comp.unix.shell > getops with one or no argument
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

getops with one or no argument

Réponse
 
LinkBack Outils de la discussion
Vieux 31/07/2007, 16h00   #1
rachit7@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut getops with one or no argument

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

  Réponse avec citation
Vieux 31/07/2007, 16h24   #2
Janis
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: getops with one or no argument

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



  Réponse avec citation
Vieux 31/07/2007, 16h24   #3
Janis
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: getops with one or no argument

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



  Réponse avec citation
Vieux 31/07/2007, 16h27   #4
rachit7@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: getops with one or no argument

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

  Réponse avec citation
Vieux 31/07/2007, 16h27   #5
rachit7@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: getops with one or no argument

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

  Réponse avec citation
Vieux 31/07/2007, 16h29   #6
rachit7@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: getops with one or no argument

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

  Réponse avec citation
Vieux 31/07/2007, 16h29   #7
rachit7@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: getops with one or no argument

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

  Réponse avec citation
Vieux 31/07/2007, 17h28   #8
Janis Papanagnou
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: getops with one or no argument

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
>

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 07h50.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,13336 seconds with 16 queries