1831136 Members
2443 Online
110020 Solutions
New Discussion

script loop help

 
SOLVED
Go to solution
Cem Tugrul
Esteemed Contributor

script loop help

Hi forum,
i need urgent help about a script that i have
written...
in script i want to go loop for to find a specific strings in another file and if i find the string then want to remove some files
and exit from loop

All replies will be submitted points!!!
Thank's
Our greatest duty in this life is to help others. And please, if you can't
18 REPLIES 18
Cem Tugrul
Esteemed Contributor

Re: script loop help

opppps,
here is my transfer script...
Our greatest duty in this life is to help others. And please, if you can't
Pete Randall
Outstanding Contributor

Re: script loop help

How about using a find command?

find /start_dir -exec grep "string to look for" |xargs rm


Pete

Pete
Geoff Wild
Honored Contributor

Re: script loop help



Code something like:

MAX_EST_TIME=120

#
# Loop until the establish is complete or times out.
#
count=1
_ret=1
while [ $count -le $MAX_EST_TIME -a $_ret != 0 ]
do
sleep 60

# your code

for i in `grep $STRING /somefile`
do
rm $FILE1
exit 1
done

done


or something like that...

Rgds...Geoff


Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
Pete Randall
Outstanding Contributor

Re: script loop help

Oops, correction:

find /start_dir -exec grep "string to look for" {} \; |xargs rm


Pete

Pete
Steven E. Protter
Exalted Contributor

Re: script loop help

my favorite loop for these issues.

while read -r
do

# code

done < input_file #lists the files to be processed, can be created by Pete's find command.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Cem Tugrul
Esteemed Contributor

Re: script loop help

Hi Geoff & Pete,
if you look my script i do not want to use
sleep command...it is a transfer script
i mean i have a transfer program called
RVS and it is also screen oriented but i do not use..i have put the script and run as root from cron..i have many transfers scripts
with rvs and always run from root cron...
When a file succesfully transfer it saves
in rlstat.log file which belongs to user
RVs...
Our greatest duty in this life is to help others. And please, if you can't
Pete Randall
Outstanding Contributor

Re: script loop help

Sorry, didn't see your script.


Pete

Pete
Laurent Menase
Honored Contributor

Re: script loop help



to exit from a loop you can use break
while :
do
if [ -f /tmp/toto ]
then
rm /tmp/toto
break
fi
sleep 500
done

or with a function
function testandremove
{
if [ -f /tmp/toto ]
then
rm /tmp/toto
return 1
fi
return 0
}

while testandremove
do
sleep 600
done

or
while :
do
testandremove
if [ $? == 0 ]
then
break
fi
sleep 600
done
Cem Tugrul
Esteemed Contributor

Re: script loop help

Hi Guys,
i am really bat at scripting...Please
take a look of my script and maybe the
3rd eyes can see or maintain my script
so i need your guide...
Our greatest duty in this life is to help others. And please, if you can't
Simon Hargrave
Honored Contributor

Re: script loop help

There's no "looping" in your script that I can see, but if I understand correctly, you want to shrink:

if [ -f ${time_chk_f_crt}_TFBUCH1C.100.txt ]
...
fi
if [ -f ${time_chk_f_crt}_TFBUCH1C.300.txt ]
...
fi
if [ -f ${time_chk_f_crt}_TFBUCH1C.500.txt ]
...
fi

down to a more manageable "loop" (basically detecting and processing 100 300 500 etc)? Then I would recommend something like: -

for FILE in ${time_chk_f_crt}_TFBUCH1C.100.txt
do
NUM=`cut -d. -f2`
/users/rvs/system/rvsut2fv $DIR1/${time_chk_f_crt}_TFBUCH1C.$NUM.txt.rvs F 650 $DIR1/${time_chk_f_crt}_TFBUCH1C.$NUM.txt T
echo "SEND DSN=\"$DIR1/${time_chk_f_crt}_TFBUCH1C.$NUM.txt.rvs\" FORMAT=F (SID=MAN CODEOUT=A DSNNEW=$FILENEW)" >$DIR1/TFBUCH1C.$NUM.rvs.tmp
/users/rvs/system/rvsbat /i$DIR1/TFBUCH1C.$NUM.rvs.tmp
sleep 600;
egrep -i ${time_chk_f_crt}_TFBUCH1C.$NUM.txt.rvs /users/rvs/db/rlstat.log
if [ $? = 0 ]
then
echo $DIR1/${time_chk_f_crt}_TFBUCH1C.$NUM.txt.rvs succesfully SEND... `date`>>$DIR0/rvs_sap_send_log
rm -Rf *.$NUM.rvs.tmp
rm -Rf *.$NUM.txt.rvs
rm -Rf *.$NUM.rcp
rm -Rf *.$NUM.txt
rm -Rf *.$NUM
else
echo $DIR1/${time_chk_f_crt}_TFBUCH1C.$NUM.txt.rvs is WAITING!!!or RVS Error!!! `date`>>$DIR0/rvs_err_log
fi
done

This assumes that your variable ${time_chk_f_crt} has no "." characters in, otherwise you'll have to change the cut command accortingly.
Simon Hargrave
Honored Contributor

Re: script loop help

Sorry, the first 3 lines of the script should be:

for FILE in ${time_chk_f_crt}_TFBUCH1C.*.txt
do
NUM=`echo $FILE | cut -d. -f2`


Basically what it's doing is looping for each file that matches (TFMUCH1C.100, 300, 500 etc.) and using the cut to get the number portion as variable $NUM. This is then used in the rest of the loop code in place of .100. in your rm's, calls to rvs code etc.

Hope this makes sense?
Steve Steel
Honored Contributor
Solution

Re: script loop help

Hi


Some nice answers

For scripting I always check

www.shelldorado.com


Steve Steel
If you want truly to understand something, try to change it. (Kurt Lewin)
Cem Tugrul
Esteemed Contributor

Re: script loop help

Simon,
i know i do not have loop but i need loop.
As you seen my script;
i have a directory and there are some files
waiting to transfer somewhere (out of country)
now the rules are;
the files only *.rcp extension are ready
to transfer...
let's say in diretory;
DVA.E04MAN.TFBUCH1C.100
DVA.E04MAN.TFBUCH1C.100.rcp
so this means DVA.E04MAN.TFBUCH1C.100 must
be transferred but before transfering
i have to convert this original file
(you see in my script)
/users/rvs/system/rvsut2fv $DIR1/${time_chk_f_crt}_TFBUCH1C.100.txt.rvs F 650 $DIR1/${time_chk_f_crt}_TFBUCH1C.100.txt T
echo "SEND DSN=\"$DIR1/${time_chk_f_crt}_TFBUCH1C.100.txt.rvs\" FORMAT=F (SID=MAN CODEOUT=A DSNNEW=$FILENEW)" >$DIR1/TFBUCH1C.100.rvs.tmp
/users/rvs/system/rvsbat /i$DIR1/TFBUCH1C.100.rvs.tmp
so transfers starts...
Now the trick is here
i have to check whether transfer succesfully
finish or not!!!! so
the control file is rlstat.log which after
transfers save in here and this file have records like;
$ pwd
/users/rvs/db
$ cat rlstat.log|tail -3
R MAN 566182 566187 2005/06/10 15:17:37 DVA.MANE04.MLLC2 /users/rvs/usrdat/taxxxxxx/taxxxxxx.T162149 4623
S MAN 566202 566203 2005/06/10 16:30:08 DVA.E04MAN.TFBUCH1C /sap/TFBUCH1C/2005_06_10_1630_TFBUCH1C.300.txt.rvs 1987
S MAN 566221 566222 2005/06/10 17:00:05 DVA.E04MAN.FAVE /users/rvs/usrdat/fave_gidecek_tmp/fave061005151902.txt.rvs 292

SO that's why i use egrep -i

Now the most important point is the files
are approx 300MB and that's why i use
sleep 600;
but this transfer time may take 20 minutes
and at this time i can not delete the files
because when the transfer is Ok i have to
empty my DIRECTORY...

Now,i how can i set a loop;
When transfer starts and waits,and try to see this file untill in rlstat.log file in my script..this is my problem..and
i think i need a loop...

Our greatest duty in this life is to help others. And please, if you can't
Cem Tugrul
Esteemed Contributor

Re: script loop help

only a link from Steve,
:-(
thks,
Our greatest duty in this life is to help others. And please, if you can't
Simon Hargrave
Honored Contributor

Re: script loop help

Okay, instead of sleep 600 I would replace with something like: -

while [ "`fuser {time_chk_f_crt}_TFBUCH1C.100.txt.rvs`" != "" ]; do
sleep 60
done

This will use fuser to see if any process has the file open (your file transfer program). If this returns a PID, then the loop will sleep for 60 seconds then test again. It will keep doing this until no process has that file open.

You can then perform your grep to confirm the logfile output.
Simon Hargrave
Honored Contributor

Re: script loop help

Or if you do not want to use fuser (which is root only, and may cause it to look forever if someone else is looking at the file):

---------

MAXMINUTES=60
grep -iq ${time_chk_f_crt}_TFBUCH1C.$NUM.txt.rvs /users/rvs/db/rlstat.log
while [ $? -ne 0 ]; do
sleep 60
(( MAXMINUTES = MAXMINUTES - 1 ))
if [ $MAXMINUTES -eq 0 ]; then
echo "Maximum transfer time reached!"
exit 1
fi
echo $MAXMINUTES
grep -iq ${time_chk_f_crt}_TFBUCH1C.$NUM.txt.rvs /users/rvs/db/rlstat.log
done

--------

This will keep checking (every minute) for the filename in the log file. Every time it checks, it counts MAXMINUTES down 1. If this reaches zero it errors.

You need the MAXMINUTES in case the script loops forever. This should be what you need.
Cem Tugrul
Esteemed Contributor

Re: script loop help

Simon,
you seem understood me...
how about this ?
until [ "`cat rlstat.log|egrep -i ${time_chk_f_crt}_TFBUCH1C.100.txt.rvs|awk '{print $8}'|cut -c-15-` = "${time_chk_f_crt}_TFBUCH1C.100.txt.rvs"
do
sleep;
done
echo $DIR1/${time_chk_f_crt}_TFBUCH1C.100.txt.rvs succesfully SEND... `date`>>$DIR0/rvs_sap_send_log
rm -Rf *.100.rvs.tmp
rm -Rf *.100.txt.rvs
rm -Rf *.100.rcp
rm -Rf *.100.txt
rm -Rf *.100

Does it work?
Our greatest duty in this life is to help others. And please, if you can't
Cem Tugrul
Esteemed Contributor

Re: script loop help

i have solved the problem like;
while true; do
if egrep -i ${time_chk_f_crt}_TFBUCH1C.100.txt.rvs /users/rvs/db/rlstat.log
then
echo $DIR1/${time_chk_f_crt}_TFBUCH1C.100.txt.rvs succesfully SEND... `date`>>$DIR0/rvs_sap_send_log
rm -Rf *.*
break
else
sleep 1
fi
Our greatest duty in this life is to help others. And please, if you can't