LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Not able to do looping in linux (https://www.linuxquestions.org/questions/linux-newbie-8/not-able-to-do-looping-in-linux-661974/)

Amey Joshi 08-11-2008 07:39 AM

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.:scratch: if it does not exist i have to wait for the file :rolleyes: .....For this i have used "while do " loop..but some how it is not working..:cry:
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...:o

Thanks in Advance!
A

colucix 08-11-2008 09:14 AM

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! ;)

Amey Joshi 08-12-2008 05:06 AM

Thanks for your reply!:)
Now actually there is bit change in the requirement ...:o
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..:confused: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...:cry:
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
:newbie:
Thanks in Advance!

colucix 08-12-2008 05:15 AM

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.

Amey Joshi 08-12-2008 06:38 AM

Thanks for your imediate reply!
Actually the problem is it not coming out of the loop..even though there are "14"'.done' files...:scratch:
Is there any other method..so that script will run in the loop till 14.dones files gets created..:confused:
Thanks in Advance!

john test 08-12-2008 09:35 AM

[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?

chrism01 08-12-2008 07:33 PM

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.

Amey Joshi 08-13-2008 03:25 AM

Quote:

Originally Posted by chrism01 (Post 3245342)
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.:D....:confused:
So my issue is resolved..:)
Again thanks all for your help!


All times are GMT -5. The time now is 11:19 PM.