Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

regular expression, can you do something from "if" without using grep? 4

Status
Not open for further replies.

gr0124

Programmer
Aug 29, 2004
7
0
0
US
I know I can use grep to see if certain string is present. Is there something that I can do with just "if".
Something like :
if [ $sript_name ???bla_bla_bla "_encrypt_" ]
then
{
echo "Sorry, script $0 you called is a wrong script"
exit 2
}
fi

echo "Good usage of script. Continue."
exit 0
 
It depends on your shell, but with most shells you can match filespec style expressions using '=':

Code:
if [ "$script_name" = *_encrypt_* ]
then
    echo "Sorry, script $0 you called is a wrong script"
    exit 2
fi

echo "Good usage of script. Continue."
exit 0

(no need for squiggly brackets)

I'm assuming you wanted to check whether the script name contains "_encrypt_" somewhere?

Annihilannic.
[small]tgmlify - code syntax highlighting for your tek-tips posts[/small]
 
Hi

Well, that condition not works for me in Bash and MKsh.

However Bash's and Ksh's [tt][[[/tt] understands it :
Code:
[b]if[/b] [teal][[/teal] [green][i]"$script_name"[/i][/green] [teal]=[/teal] [teal]*[/teal]_encrypt_[teal]*[/teal] [teal]];[/teal] [b]then[/b]
[small]  echo [green][i]"Sorry, script $0 you called is a wrong script"[/i][/green]
  [b]exit[/b] [purple]2[/purple]
[b]fi[/b][/small]
And with Bash's [tt][[[/tt] regular expression would also work :
Bash:
[b]if[/b] [teal][[[/teal] [green][i]"$script_name"[/i][/green] [teal]=~[/teal] [teal].*[/teal]_encrypt_[teal].*[/teal] [teal]]];[/teal] [b]then[/b]
[small]  echo [green][i]"Sorry, script $0 you called is a wrong script"[/i][/green]
  [b]exit[/b] [purple]2[/purple]
[b]fi[/b][/small]
But the portable way would be to use [tt]case[/tt] :
Code:
[b]case[/b] [green][i]"$script_name"[/i][/green] [b]in[/b]
  [teal]*[/teal]_encrypt_[teal]*)[/teal]
[small]    echo [green][i]"Sorry, script $0 you called is a wrong script"[/i][/green]
    [b]exit[/b] [purple]2[/purple]
  [teal];;[/teal]
[b]esac[/b][/small]
However I see no problem in using [tt]grep[/tt], if you are more familiar with :
Code:
[b]if[/b] echo [green][i]"$script_name"[/i][/green] [teal]|[/teal] grep [green][i]'.*_encrypt_.*'[/i][/green] [teal]>[/teal] /dev/null[teal];[/teal] [b]then[/b]
[small]  echo [green][i]"Sorry, script $0 you called is a wrong script"[/i][/green]
  [b]exit[/b] [purple]2[/purple]
[b]fi[/b][/small]

Feherke.
 
Yep, I meant to use [[.

Annihilannic.
[small]tgmlify - code syntax highlighting for your tek-tips posts[/small]
 
The Korn shell also has a pattern match operator, "[tt]@(<pattern>)[/tt]""...
Code:
if [[ $script_name = [b]@(*_encrypt_*)[/b] ]]
then
  echo "Sorry, script $0 you called is a wrong script"
  exit 2
fi


 
Hi

Bash also has that but as far as I know, it is not operator, is just a sub-pattern delimiter with quantifier. As such, not needed in this case. It would be useful for example for
Code:
[b]if[/b] [teal][[[/teal] [navy]$script_name[/navy] [teal]=[/teal] [teal]*[/teal]_@[teal]([/teal]en[teal]|[/teal]de[teal])[/teal]crypt_[teal]*[/teal] [teal]]];[/teal] [b]then[/b]
[small]  echo [green][i]"Sorry, script $0 you called is a wrong script"[/i][/green]
  [b]exit[/b] [purple]2[/purple]
[b]fi[/b][/small]

[gray]# or[/gray]

[b]if[/b] [teal][[[/teal] [navy]$script_name[/navy] [teal]=[/teal] [teal]*[/teal]_encr@[teal]([[/teal]yi[teal]])[/teal]pt_[teal]*[/teal] [teal]]];[/teal] [b]then[/b]
[small]  echo [green][i]"Sorry, script $0 you called is a wrong script"[/i][/green]
  [b]exit[/b] [purple]2[/purple]
[b]fi[/b][/small]

Feherke.
 
Thanks everybody a lot, I will try these things today.
The platform were I am testing is really ZOS (MVS)
 
running this:
script_name="aa_encrypt_sh"
if [[ $script_name = @(*_encrypt_*) ]]
then
{
echo aaaaa
}
fi

[[: ./1 38: FSUM6807 expression syntax error, points to line 38 of my script which is if [[ $script_name = @(*_encrypt_*) ]]

running this:
if [[ $script_name = *_@(en|de)crypt_* ]]
then
{
echo aaaaa
}
fi
get this message:
./1 38: FSUM7332 syntax error: got |, expecting ]]
points to same line

running this:
if [[ "$script_name" =~ .*_encrypt_.* ]]
then
{
echo aaaaa
}
fi

[[: ./1 39: FSUM6807 expression syntax error
points to the same line

now just to make sure that general syntax is ok:
running this:
script_name="aa_encrypt_sh"
if [[ $script_name = "aa_encrypt_sh" ]]
then
{
echo aaaaa
}
fi

and have got echo :
aaaaa




 
Yes, you should be getting errors when running these commands on Z/OS (MVS).

Try putting this as the very first line of your script...
Code:
#!/bin/ksh

Or this...
Code:
#!/bin/bash

If it doesn't help, you are in the wrong forum.

[bigsmile]
 
Anyway, I'd use the case syntax

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
#!/bin/ksh did not help. I had originally #!/bin/sh

What has worked was both with #!/bin/sh and with #!/bin/ksh
is the following:
script_name="aa_encrypt_sh"
case $script_name in
*_encrypt_*)
echo "Sorry, script $0 you called is a wrong script"
exit 2
;;
esac

Thanks everybody a lot.
 
Sam, I was puzzling over the same thing, but a colleague with IBM experience tells me there is a "z/OS POSIX environment" available under MVS which includes Unix shells and tools.

Reminds me of Tandem's NonStop unix-like environment...

Annihilannic
[small]tgmlify - code syntax highlighting for your tek-tips posts[/small]
 
Thanks Anni. I was thinking the same thing since it seemed to be taking Bourne commands. I've got VMS experience and OpenVMS has a POSIX environment. The only thing is that it's "close", but has differences. I'm wondering how big the differences are.

gr0124, can you type this command and tell us what it says?
Code:
ps -f

 
No problem, this is the response to `ps -f`

$ps -f
UID PID PPID C STIME TTY TIME CMD
FIA1GSZ 33554500 594 - 12:31:02 ttyp0002 0:00 ps -f
FIA1GSZ 594 83886191 - 12:30:56 ttyp0002 0:00 sh -L
$


 
It looks like you're using a Bourne shell. I would use the "case" statement that feherke provided. It will be the most portable and will give you an easy way of coding the "else" conditions.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top