if statement in Unix

if statement in Unix

Post by doral.. » Thu, 24 Apr 2003 06:10:01



I have this list (maint_tag.lis3):
gab-501-qamaint
gab-400-devmaint
gab-302-qamaint

And I am trying to write a script that will let me search
the list above and if it finds "qamaint" then it will do
a command.  I can't seem to figure out the correct syntax
for the if statement.  The syntax below is looking for an
exact match of "qamaint" without the rest of the characters
that are specified before it.

 for maint_tag in `cat maint_tag.lis3`
 do
   if [[ $maint_tag = *qamaint ]]
   then
       print "good"
   else
       print "bad"
   fi
 done

Does anyone know the correct syntax?

Thanks
DT

 
 
 

if statement in Unix

Post by scriptOmati » Thu, 24 Apr 2003 06:36:33



> I have this list (maint_tag.lis3):
> gab-501-qamaint
> gab-400-devmaint
> gab-302-qamaint

> And I am trying to write a script that will let me search
> the list above and if it finds "qamaint" then it will do
> a command.  I can't seem to figure out the correct syntax
> for the if statement.  The syntax below is looking for an
> exact match of "qamaint" without the rest of the characters
> that are specified before it.

>  for maint_tag in `cat maint_tag.lis3`
>  do
>    if [[ $maint_tag = *qamaint ]]
>    then
>        print "good"
>    else
>        print "bad"
>    fi
>  done

> Does anyone know the correct syntax?

> Thanks
> DT

The syntax you have is correct as it is, for the ksh.
Does the script start with

#! /bin/ksh -p

???

I tried your script on linux, running pdksh (as /bin/ksh), and
it worked fine.

--
http://ftp.opensysmon.com is a shell script archive site with an
open source system monitoring and network monitoring software package.
Many platforms are supplied already compiled.

 
 
 

if statement in Unix

Post by Chris Co » Thu, 24 Apr 2003 07:07:18



> I have this list (maint_tag.lis3):
> gab-501-qamaint
> gab-400-devmaint
> gab-302-qamaint

> And I am trying to write a script that will let me search
> the list above and if it finds "qamaint" then it will do
> a command.  I can't seem to figure out the correct syntax
> for the if statement.  The syntax below is looking for an
> exact match of "qamaint" without the rest of the characters
> that are specified before it.

>  for maint_tag in `cat maint_tag.lis3`
>  do
>    if [[ $maint_tag = *qamaint ]]
>    then
>        print "good"
>    else
>        print "bad"
>    fi
>  done

> Does anyone know the correct syntax?

> Thanks
> DT

Bourne shell variant (should run almost anywhere, except the
evil csh)

for maint_tag in `cat maint_tag.lis3`
do
    if expr "$maint_tag" : '.*qamaint' >/dev/null
    then
        echo "good"
    else
        echo "bad"
    fi
done

or you could also do (a bit OT, doesn't use if)...

for maint_tag in `cat maint_tag.lis3`
do
    case "$maint_tag" in
    *qamaint)
        echo "good"
    ;;
    *)
        echo "bad"
    ;;
    esac
done

or (and this may not work in the REAL program.. a bit OT)...

sed -n -e 's/.*qamaint/good/p' -e 't' -e 's/.*/bad/p' <maint_tag.lis3

 
 
 

if statement in Unix

Post by doral.. » Thu, 24 Apr 2003 23:42:30




> > I have this list (maint_tag.lis3):
> > gab-501-qamaint
> > gab-400-devmaint
> > gab-302-qamaint

> > And I am trying to write a script that will let me search
> > the list above and if it finds "qamaint" then it will do
> > a command.  I can't seem to figure out the correct syntax
> > for the if statement.  The syntax below is looking for an
> > exact match of "qamaint" without the rest of the characters
> > that are specified before it.

> >  for maint_tag in `cat maint_tag.lis3`
> >  do
> >    if [[ $maint_tag = *qamaint ]]
> >    then
> >        print "good"
> >    else
> >        print "bad"
> >    fi
> >  done

> > Does anyone know the correct syntax?

> > Thanks
> > DT

> The syntax you have is correct as it is, for the ksh.
> Does the script start with

> #! /bin/ksh -p

> ???

> I tried your script on linux, running pdksh (as /bin/ksh), and
> it worked fine.

That's really strange...it does work.  I tried it all day yesterday and
it kept printing "bad". I am really surprised.  Oh well, thanks so much.

D

 
 
 

if statement in Unix

Post by doral.. » Thu, 24 Apr 2003 23:43:10




> > I have this list (maint_tag.lis3):
> > gab-501-qamaint
> > gab-400-devmaint
> > gab-302-qamaint

> > And I am trying to write a script that will let me search
> > the list above and if it finds "qamaint" then it will do
> > a command.  I can't seem to figure out the correct syntax
> > for the if statement.  The syntax below is looking for an
> > exact match of "qamaint" without the rest of the characters
> > that are specified before it.

> >  for maint_tag in `cat maint_tag.lis3`
> >  do
> >    if [[ $maint_tag = *qamaint ]]
> >    then
> >        print "good"
> >    else
> >        print "bad"
> >    fi
> >  done

> > Does anyone know the correct syntax?

> > Thanks
> > DT

> Bourne shell variant (should run almost anywhere, except the
> evil csh)

> for maint_tag in `cat maint_tag.lis3`
> do
>     if expr "$maint_tag" : '.*qamaint' >/dev/null
>     then
>         echo "good"
>     else
>         echo "bad"
>     fi
> done

> or you could also do (a bit OT, doesn't use if)...

> for maint_tag in `cat maint_tag.lis3`
> do
>     case "$maint_tag" in
>     *qamaint)
>         echo "good"
>     ;;
>     *)
>         echo "bad"
>     ;;
>     esac
> done

> or (and this may not work in the REAL program.. a bit OT)...

> sed -n -e 's/.*qamaint/good/p' -e 't' -e 's/.*/bad/p' <maint_tag.lis3

D wrote,

Thanks you Chris!

 
 
 

if statement in Unix

Post by Chris Co » Fri, 25 Apr 2003 03:43:37




...snip...

>>sed -n -e 's/.*qamaint/good/p' -e 't' -e 's/.*/bad/p' <maint_tag.lis3

I don't know where my head was when I type this...
how about (thought the above will work)...

sed -n 's/.*qamaint/good/p;t;s/.*/bad/p' <maint_tag.lis3

(you can separate the commands safely with semicolons)

Quote:

> D wrote,

> Thanks you Chris!

You're welcome.
 
 
 

if statement in Unix

Post by Stephane CHAZELA » Fri, 25 Apr 2003 04:20:51


[...]

Quote:>>>sed -n -e 's/.*qamaint/good/p' -e 't' -e 's/.*/bad/p' <maint_tag.lis3

> I don't know where my head was when I type this...
> how about (thought the above will work)...

> sed -n 's/.*qamaint/good/p;t;s/.*/bad/p' <maint_tag.lis3

> (you can separate the commands safely with semicolons)

No, not safely for the 't' command. 't' takes an optional
branching label and some seds allow ';' in a label name (you
can't put ';' either after 'b label' or ': label' or '}'.

sed -ne 's/.*qamaint/good/p;t' -e 's/.*/bad/p' <maint_tag.lis3
or
sed -n 's/.*qamaint/good/p;t
s/.*/bad/p' <maint_tag.lis3

Or
sed -e 's/.*qamaint/good/;t' -e 's/.*/bad/' <maint_tag.lis3

--
Stphane

 
 
 

if statement in Unix

Post by Chris Co » Fri, 25 Apr 2003 05:32:26




> [...]

>>>>sed -n -e 's/.*qamaint/good/p' -e 't' -e 's/.*/bad/p' <maint_tag.lis3

>>I don't know where my head was when I type this...
>>how about (thought the above will work)...

>>sed -n 's/.*qamaint/good/p;t;s/.*/bad/p' <maint_tag.lis3

>>(you can separate the commands safely with semicolons)

> No, not safely for the 't' command. 't' takes an optional
> branching label and some seds allow ';' in a label name (you
> can't put ';' either after 'b label' or ': label' or '}'.

> sed -ne 's/.*qamaint/good/p;t' -e 's/.*/bad/p' <maint_tag.lis3
> or
> sed -n 's/.*qamaint/good/p;t
> s/.*/bad/p' <maint_tag.lis3

> Or
> sed -e 's/.*qamaint/good/;t' -e 's/.*/bad/' <maint_tag.lis3

Thanks, I knew there was a reason for using the -e, thought
I blew a gasket.  Too many incompatibilities, too little time.
 
 
 

1. Help with if then statement and loop statement

hi im struggling on trying to make an if then & loop statement work. could
anyone help

my program should have the prompt
and loop whenever enter is hit

that i think i can figure out.

The hard part is accepting input from the prompt
for example
(psuedo code)

echo ">"
read input
if test $input=" " then loop back to the echo ">"
if test $input="look here" then echo "you typed look here"
                ^^^^^^^^^
                important that it has a space

could someone help me with those four lines?
i am using the c shell.  Im not even sure you can do loops in that shell
either.  

                                                        thanks
                                                        -Vinny

----

"Total  action.
 Total bird."  -Crackers the Corporate Crime Fighting Chicken

2. Mitsumi 1x CDROM on SB16 MultiCD doesn't work

3. Oracle iFS as a central file store for UNIX and Windows?

4. 2.9 Freezes: "sd0: not queued, error 5"

5. IFS for UNIX???

6. X works - but where are the programs

7. Unix Plug-ins?

8. uClinux Info!

9. Remote unix (RS/6000) servers with dial-ins

10. Unix Libraries Ins/Outs

11. The only interesting statement in the UNIX haters handbook

12. Unix If statement?

13. Pratical unix and internet security: reference to statement