LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-11-2008, 08:39 AM   #1
Amey Joshi
LQ Newbie
 
Registered: Aug 2008
Posts: 7

Rep: Reputation: 0
Unhappy Not able to do looping in linux


Hi,

There is an scenario where i have to check for the file if it exists then i have to mail to the output of the file. if it does not exist i have to wait for the file .....For this i have used "while do " loop..but some how it is not working..
the following are the codes which i have tried:
Code:
#!/bin/sh
echo 'LOoping for file' > $PARMDIR/test.txt
echo 'LOoping for file' >> $PARMDIR/test.txt
cat $PARMDIR/test.txt | while read line
do
 if [ -f $PARMDIR/filename.txt ]
 then
 nail -a $$PARMDIR/filename.txt  -s Testing xyz@abc.com
 echo '' > $PARMDIR/test.txt
 else
 echo file not present
 echo waiting for file
 echo 'LOoping for file' >> $PARMDIR/test.txt
 fi
done
but instead of running in the loop..it is just running for 2 times...
PLease let me know if there is any other method to do the same..or some modification in above script...

Thanks in Advance!
A
 
Old 08-11-2008, 10:14 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Your code loops over the content of $PARMDIR/test.txt. It has two lines, so the loop is executed twice. If you add an extra line before executing the loop, it will be executed three times. Furthermore you overwrite the file inside the loop, just to make things more complicate. Why don't simply loop over the existence of the file? In other words you can loop until the file filename.txt does not exists:
Code:
#!/bin/bash
PARMDIR=/path/to/some/dir
while [ ! -f $PARMDIR/filename.txt ]
do
  echo file not present
  echo waiting for file
  sleep 5
done
mail -a $PARMDIR/filename.txt -s Testing xyz@abc.com
After exiting the loop, that is when the file exists, you can send it via mail. Also consider to add a sleep command inside the loop, as in my example, unless you want to loop every nanosecond!
 
Old 08-12-2008, 06:06 AM   #3
Amey Joshi
LQ Newbie
 
Registered: Aug 2008
Posts: 7

Original Poster
Rep: Reputation: 0
Question

Thanks for your reply!
Now actually there is bit change in the requirement ...
NOw we have to check whether there are 14 number of '.done' files present in the specific path.if it present then we have to run another script..if not we have to wait till all these '.done' files are generated.
As per the above mention post i have tried to loop the script using 'while' loop but it is not giving me warning...
the following is the code:
Code:
#!/bin/sh
PATHDIR=/EDWNAS1/opt/dwqa1
cd $PATHDIR
while [ ! -f $PARMDIR/test_running.txt ]
do
  echo file not present
  echo waiting for file
  cntdone=`ls -ltr | grep done | wc -l`
  if [ $cntdone -eq 14 ]
   then
    sh batch2.sh  #running different script
    touch $PARMDIR/test_running.txt
  else
    echo in the else loop
    echo 'Still Running' 
    sleep 5
   fi
done
The above script does not comes out of the loop even though value of "cntdone=14"..it is throwing me following error:
Code:
 /test_running.txt: Permission denied

Thanks in Advance!
 
Old 08-12-2008, 06:15 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Code:
/test_running.txt: Permission denied
This is another issue. If you look carefully at the error message, you will see that the path of the file is /test_running.txt and maybe you don't have permissions to look under the root of the directory tree. This results from the fact that in your script the variable PARMDIR is not assigned. Check it and run the script again.

An aside note. The line
Code:
cntdone=`ls -ltr | grep done | wc -l`
can be simply
Code:
cntdone=`ls *.done | wc -l`
if you are intersted in the number of .done files, you don't need the 'lrt' option of ls. Also the grep command is not needed, since you can use wild characters in the ls command to select a bunch of files whose name matches a given pattern.
 
Old 08-12-2008, 07:38 AM   #5
Amey Joshi
LQ Newbie
 
Registered: Aug 2008
Posts: 7

Original Poster
Rep: Reputation: 0
Smile

Thanks for your imediate reply!
Actually the problem is it not coming out of the loop..even though there are "14"'.done' files...
Is there any other method..so that script will run in the loop till 14.dones files gets created..
Thanks in Advance!
 
Old 08-12-2008, 10:35 AM   #6
john test
Member
 
Registered: Jul 2008
Distribution: ubuntu 9.10
Posts: 527
Blog Entries: 1

Rep: Reputation: 35
[QUOTE=Amey Joshi;3244631]Thanks for your reply!

[CODE]#!/bin/sh
PATHDIR=/EDWNAS1/opt/dwqa1
cd $PATHDIR
while [ ! -f $PARMDIR/test_running.txt ]
do
-------------------------\

Don't you have to initialze $PARMDIR before you use it in line 4 of your code?
 
Old 08-12-2008, 08:33 PM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,247

Rep: Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328
Note also that although /bin/sh is often symlinked to /bin/bash in Linux, you should not rely on it. Always explicitly specify the shell you want ie

#!/bin/bash

(like colucix is doing) otherwise you may get strange results/errors.
 
Old 08-13-2008, 04:25 AM   #8
Amey Joshi
LQ Newbie
 
Registered: Aug 2008
Posts: 7

Original Poster
Rep: Reputation: 0
Thumbs up

Quote:
Originally Posted by chrism01 View Post
Note also that although /bin/sh is often symlinked to /bin/bash in Linux, you should not rely on it. Always explicitly specify the shell you want ie

#!/bin/bash

(like colucix is doing) otherwise you may get strange results/errors.

Thanks for your reply!
I just replaced #!/bin/sh with #!/bin/bash and it started working properly.....
So my issue is resolved..
Again thanks all for your help!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Looping a script Keentolearn Linux - Newbie 4 01-30-2007 04:54 PM
login looping plaircpa Slackware 3 10-31-2005 09:10 PM
looping in mplayer zexter Linux - Software 1 12-22-2003 05:39 PM
[C] for looping problem wuck Programming 6 10-19-2003 05:29 PM
looping kernel error adenardo Linux - General 0 05-13-2003 07:27 PM


All times are GMT -5. The time now is 08:50 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration