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

how find if a stirng is a part of a variable.... 2

Status
Not open for further replies.

buccaneers

Programmer
Jun 3, 2004
28
US
Hello Guru's,

I have a question. But before i ask i need to give some background

I have a variable say VAR assigned with value say thistestvalue

now i have to find if "est" is a part of $VAR. If so then what is its starting position in the $VAR.

Is there a way to do this in shell script (ksh) ?

TIA

 
Not to cumbersome ?
case $VAR in *est*) echo $((1 + $(expr length $(expr $VAR : '\(.*\)est'))));; esac

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Thx PHV. this did help a little. But i forgot to mention one thing in my question. This string to be searched is in variable. like SRC_VAR=est.

Is there a way to get around this.

TIA
 
case $VAR in *$SRC_VAR*) echo $((1 + $(expr length $(expr "$VAR" : '\(.*\)'"$SRC_VAR"))));; esac

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Never mind. This works...

case $VAR in *${SRC_VAR}*) echo "String found" ;; esac

If i may ask one more question.
Why do i always have to mention "*". Why can i not just specify ${SRC_VAR} ?

Just want to better understand.

TIA.
 
Hello Guru's,

now this code is not working. Am i suppose to write individual "case" statements ?

Below is teh code :-
#!/bin/ksh

cl_cd=est
tgt_str=thistestvalue00

ist_flg=0
f_ist=0
f_cl=0
f_00=0
f_01=0
f_sybP=0
f_reg=0
tgt_str_ln=${#tgt_str}
cl_cd_ln=${#cl_cd}
chk_ln=`expr 3 + ${cl_cd_ln} + 2`


case ${tgt_str} in
*ist*) f_ist=1 ;;
*${cl_cd}*) f_cl=1 ;;
*sybP*) f_sybP=1 ;;
*00*) f_00=1 ;;
*01*) f_01=1 ;;
esac

echo "cl_cd : "$cl_cd
echo "tgt_str : "$tgt_str
echo "f_cl : "$f_cl
echo "f_ist : "$f_ist
echo "f_sybP : "$f_sybP
echo "f_00 : "$f_00
echo "f_01 : "$f_01
echo "f_reg : "$f_reg

if [ ${f_00} -eq 1 ] || [ ${f_01} -eq 1 ]
then
if [ ${f_cl} -eq 1 ]
then
if [ ${f_pro} -eq 1 ] || [ ${f_sybP} -eq 1 ]
then
ist_flg=1
elif [ ${tgt_srv_ln} -eq ${chk_ln} ]
then
ist_flg=1
fi
fi
fi
echo "ist_flg : "$ist_flg
exit 0
 
In your shell man page pay attention to the case compound-command.
Am i suppose to write individual "case" statements ?
Yes.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top