Testing for a particular parameter without looping
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Shell > Testing for a particular parameter without looping




Pages (2): [1] 2 »   Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    Testing for a particular parameter without looping  
Timur Tabi


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
10-04-04 11:01 PM

I have a bash script that can get passed any number of different
parameters.  At one point, I need to test if a particular parameter
has been passed.  For example, the script could be called by "myscript
A B C", or it could be called by "myscript B C A".  All I care about
is if "B" was passed.

I know I can do this with a loop, checking each parameter, but I was
wondering if it can be done without a loop, and preferably without
calling an external program (like awk).  Any ideas?





[ Post a follow-up to this message ]



    Re: Testing for a particular parameter without looping  
Bit Twister


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
10-04-04 11:01 PM

On 4 Oct 2004 16:01:38 -0700, Timur Tabi wrote:
> I have a bash script that can get passed any number of different
> parameters.  At one point, I need to test if a particular parameter
> has been passed.  For example, the script could be called by "myscript
> A B C", or it could be called by "myscript B C A".  All I care about
> is if "B" was passed.
>
> I know I can do this with a loop, checking each parameter, but I was
> wondering if it can be done without a loop, and preferably without
> calling an external program (like awk).  Any ideas?

use _if_ statements





[ Post a follow-up to this message ]



    Re: Testing for a particular parameter without looping  
Bob


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
10-05-04 01:47 AM

On Mon, 04 Oct 2004 16:01:38 -0700, Timur Tabi wrote:

> I have a bash script that can get passed any number of different
> parameters.  At one point, I need to test if a particular parameter has
> been passed.  For example, the script could be called by "myscript A B C",
> or it could be called by "myscript B C A".  All I care about is if "B" was
> passed.
>
> I know I can do this with a loop, checking each parameter, but I was
> wondering if it can be done without a loop, and preferably without calling
> an external program (like awk).  Any ideas?

Here is something that works if all parameters are single characters. Only
lightly tested -- may have bugs or make unwarranted assumptions!

=================================
#!/bin/bash

p="C"	# variable p is set to parameter to look for

if [ "${*/$p/}" != "$*" ] ; then
echo "$p seen"
else
echo "$p not seen"
fi
=================================







[ Post a follow-up to this message ]



    Re: Testing for a particular parameter without looping  
Seb


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
10-05-04 01:47 AM

On 2004-10-05, Bob <bob@dont.spam.me> wrote:
> Here is something that works if all parameters are single characters. Only
> lightly tested -- may have bugs or make unwarranted assumptions!
>
> =================================
> #!/bin/bash
>
> p="C"	# variable p is set to parameter to look for
>
> if [ "${*/$p/}" != "$*" ] ; then
>         echo "$p seen"
> else
>         echo "$p not seen"
> fi
> =================================

For non-single characters, following the same idea, one could do:

p="theOption"	# variable p is set to parameter to look for

if [ "${*/ $p /}" != "$*" ] ; then
echo "$p seen"
else
echo "$p not seen"
fi

--Seb





[ Post a follow-up to this message ]



    Re: Testing for a particular parameter without looping  
William Park


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
10-05-04 01:47 AM

Timur Tabi <nospam_timur@tabi.org> wrote:
> I have a bash script that can get passed any number of different
> parameters.  At one point, I need to test if a particular parameter
> has been passed.  For example, the script could be called by "myscript
> A B C", or it could be called by "myscript B C A".  All I care about
> is if "B" was passed.
>
> I know I can do this with a loop, checking each parameter, but I was
> wondering if it can be done without a loop, and preferably without
> calling an external program (like awk).  Any ideas?

Not in standard shell.  With my patch to Bash shell,
${*|/B}
${@|/B}
will return positional parameters matching glob pattern 'B'.

Ref:
http://freshmeat.net/projects/bashdiff/
--
William Park <opengeometry@yahoo.ca>
Open Geometry Consulting, Toronto, Canada





[ Post a follow-up to this message ]



    Re: Testing for a particular parameter without looping  
Bob


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
10-05-04 08:01 AM

On Tue, 05 Oct 2004 02:10:27 +0000, Seb wrote:

> On 2004-10-05, Bob <bob@dont.spam.me> wrote: 
>
> For non-single characters, following the same idea, one could do:
>
> p="theOption"	# variable p is set to parameter to look for
>
> if [ "${*/ $p /}" != "$*" ] ; then
>   echo "$p seen"
> else
>   echo "$p not seen"
> fi
>
> --Seb

Need to account for the optoin being first or last:

p="theOPtion"

opts=" $* "
if [ "${opts/ $p /}" != "$opts" ] ; then
<the rest as above>

Warning *totally* untested!






[ Post a follow-up to this message ]



    Re: Testing for a particular parameter without looping  
Ed Morton


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
10-05-04 08:01 AM



Timur Tabi wrote:
> I have a bash script that can get passed any number of different
> parameters.  At one point, I need to test if a particular parameter
> has been passed.  For example, the script could be called by "myscript
> A B C", or it could be called by "myscript B C A".  All I care about
> is if "B" was passed.
>
> I know I can do this with a loop, checking each parameter, but I was
> wondering if it can be done without a loop, and preferably without
> calling an external program (like awk).  Any ideas?

This will work if all your parameters are single-character and take no
arguments that might contain a "B":

case $* in
*B* )	echo "Eureka!" ;;
* )	echo "boo hoo..." ;;
esac

Regards,

Ed.





[ Post a follow-up to this message ]



    Re: Testing for a particular parameter without looping  
Stephane CHAZELAS


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
10-05-04 08:01 AM

2004-10-04, 22:15(-05), Ed Morton:
[...]
> case $* in
> 	*B* )	echo "Eureka!" ;;
> 	* )	echo "boo hoo..." ;;
> esac
[...]

That tests wether the concatenation (with spaces unless IFS was
modified) of the arguments contains "B", not wether there's a
"B" amongst the arguments

With zsh:

if (( $+argv[(r)B] )); then
echo "Eureka!"
else
echo "boo hoo..."
fi

There's no equivalent with bash, unless you're ready to suppose
that one particular character is not present in the arguments:

If it's "|":

IFS="|"
case "|$*|" in
*"|B|"*) echo yes;;
*) echo no;;
esac

--
Stephane





[ Post a follow-up to this message ]



    Re: Testing for a particular parameter without looping  
Bill Marcum


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
10-05-04 10:58 PM

On 4 Oct 2004 16:01:38 -0700, Timur Tabi
<nospam_timur@tabi.org> wrote:
> I have a bash script that can get passed any number of different
> parameters.  At one point, I need to test if a particular parameter
> has been passed.  For example, the script could be called by "myscript
> A B C", or it could be called by "myscript B C A".  All I care about
> is if "B" was passed.
>
> I know I can do this with a loop, checking each parameter, but I was
> wondering if it can be done without a loop, and preferably without
> calling an external program (like awk).  Any ideas?

case "$*" in
B *|* B *|* B)  echo "script has B" ;;
esac

--
System Events
=-=-=-=-=-=-=
Sep 16 03:31:11 don kernel: lp0 on fire






[ Post a follow-up to this message ]



    Re: Testing for a particular parameter without looping  
Stephane CHAZELAS


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
10-05-04 10:58 PM

2004-10-5, 07:15(-04), Bill Marcum:
[...]
> case "$*" in
>  B *|* B *|* B)  echo "script has B" ;;
> esac

~$ case "$*" in
case>  B *|* B *|* B)  echo "script has B" ;;
zsh: parse error near `*'
bash: syntax error near unexpected token `*'
esac> ksh: syntax error: `*' unexpected
esac> syntax error: `*' unexpected


Should be:

case "$*" in
B\ *|*\ B\ *|*\ B)  echo "script has B" ;;
esac

But that works only for Bourne shell or with POSIX shells if IFS
is unset or starts with " ".

It's not reliable if arguments may contain spaces.

It can be shortened as:

case " $* " in
*" B "*)  echo "script has B" ;;
esac

Actually, with bash and ksh93 (not zsh), you can do it as:

IFS=,
case ",${*//,/}," in
*,B,*) echo script has B;;
esac

Of course, that doesn't work if instead of "B", you have a
string with commas in it (but you could use an other character
then as first character of IFS).

I guess you shouldn't rely on that behavior as it's not the
behavior one would expect (at least by Bourne standard, $* is
supposed to be a normal parameter whose value is the
concatenation of the arguments with first character of IFS
inbetween, so that ${*//,/} shouldn't contain any ",").

--
Stephane





[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 12:18 PM.      Post New Thread    Post A Reply      
Pages (2): [1] 2 »   Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register