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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Why is this not working?

Status
Not open for further replies.

shine67

Programmer
Jan 11, 2002
41
0
0
US
Hi,

the following ksh script is supposed to detect a file name with 'security' but it doesn't.

file=security.040401.txt
if [[ $file == '*security*' ]]; then
print "detected security file"
else
print "Didn't detect security file"
fi

I tried with all of these regular expressions and also with "" instead of '' but none of them works: 'security', 'security*', '*security'

Help????

Thanks,
Shine.
 
Have you tried this ?
file=security.040401.txt
case $file in
*security*)
print "detected security file";;
*)
print "Didn't detect security file";;
esac


Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
Replace this:
if [[ $file == '*security*' ]]; then
by this:
if [[ $file = *security* ]]; then

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
You must not enclose the pattern between quotes :
[tt]
file=security.040401.txt
if [[ $file == *security* ]]; then
print "detected security file"
else
print "Didn't detect security file"
fi
[/tt]

Jean Pierre.
 
== is for numerical comparison (( a == b ))
= is for string comparison [[ a = b ]]
 
[[ = ]] and [[ == ]] are both accepted and give the same result with AIX ksh, pdksh (Public Domain KSH) and bash.

For bash the man page specify :
[[ expression ]] ...
When the == and != operators are used, the string to the right of the operator is considered a pattern and matched according to the rules described below under Pattern Matching.
CONDITIONAL EXPRESSIONS
string1 == string2 True if the strings are equal. = may be used in place of ==.


Jean Pierre.
 
It sounds like your checking if a file exists on the system, right now you have been puting it into a variable, but you can in a if sentence check directly for it. (it thats what you want)

if [ -e /security.040401.txt ]
then
print "detected security file"
else
print "Didn't detect security file"
fi

you can also use * in the if sentence
if [ -e /*security* ]


Larshg
 
Use double-quotes instead of single quotes. Your code is looking for a file with literal asterisks in the name.

Consider this completely different alternative...
Code:
FILE="security.040401.txt"
CHECKNAME=`echo $FILE | grep "security" | wc -l`

if [ $CHECKNAME -gt 0 ]; then
    print "detected security file"
else
    print "Didn't detect security file"
fi

ummm... that may be just my preference, but it looks clearer to me this way.

--
-- GhodMode
 
Thanks guys. It's working and you are great!

Shine.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top