While loop

Task: I need to continuously search the 2nd word in the last line of a file (hist1.dat) and execute a command (echo "complete") when that point becomes equal to 2. A line gets added to hist1.dat every 10 seconds or so by another software.

Attempt: I have tried to do it with the following script (but its been unsuccessful):

#!/bin/csh -f
 
set x=3
 
while [$x != 2] ; do
set x=`tail -1 hist1.dat | awk '{print $2}'` 
sleep 1
done
 
echo "Complete"

I keep getting an error message: �/usr/local/bin/done: Permission denied"

Can anyone spot an error here or have any idea how I can do that another way?

Any help is much appreciated. thanks!

Your program is wrong in a variety of interesting ways.

1) You are using c-shell, csh because of #!/bin/csh, but your code is Bourne instead.

2) Once you fix that, your code is also missing a lot of important spaces.

3) Your program doesn't do what you think it does. It just reads the same line every time.

#!/bin/sh

while read WORD1 WORD2 WORD3
do
        if [ "$WORD2" = "2" ]
        then
                break
        fi
done < hist1.dat

if [ "$WORD2" = "2" ]
then
        echo "Complete"
else
        echo "not found"
fi
1 Like

I'm not d'accord with Corona688's item 3), as tail -1 will always render the last line of that file which is appended every 10 seconds.
I'm not at all familiar with csh, but done - which provokes the error msg - is a builtin in the other shells. So - either you run the (corrected!) script with sh (bash, ksh) or you use constructs provided by csh, which might need rewriting the script.

1 Like

Thanks for your replies Corona668 and Rudic.

I have verified that Rudic is indeed right and the script will always read the last line of the file as i want it to. I have made some changes and the new script stands at:

#!/bin/csh
 # demoloop.csh - Sample loop script
 set x = 1
 while ( $x != 4 )
   set x=`tail -1 hist1.dat | awk '{print $2}'`
   sleep 2
   echo $x
 end
echo "done"

This actually does exactly what i ask it to do. Now instead of just outputting "done" at the end, i would like to change the directory and then launch a bunch of commands. This however does not happen.

If i add cd /users/

It doesnt actually go to that directory. Any ideas why this may be happening?

Does /users/ actually exist? Are you trying to change into a folder named 'users' in the root directory or the current directory?

/users/ exits.

I can manually enter the command cd /users/ to go to that folder. I am entering the full path. I have also tried the path that i get from using pwd but to no avail

Are you expecting the script to change what directory your terminal is in? It can't do that. It has its own, independent current directory.

The following is more efficient and doesn't need a loop. Maybe it works for you?

tail -1 -f hist1.dat | awk '$2==2{exit}'
echo "Complete"
2 Likes

If you want to change the current directory in you terminal shell you will need a function.

Here is a bash solution:

$ pwd
/users/chubler

$ tail hist1.dat
3 4 1

$ function waitHist() {
>   while true
>   do
>      tail -1 hist1.dat | awk '$2==4{exit 1}' || break
>      sleep 1
>   done
>   echo "Complete"
>   cd /users/
> }

$ waitHist
Complete

$ pwd
/users

---------- Post updated at 07:01 AM ---------- Previous update was at 06:22 AM ----------

I also managed to get this working with a alias in csh however I did need to set a variable ($D) to the dollar symbol first:

$ tail -1 hist1.dat
2 4 6

$ pwd
/users/chubler

$ set D = \$ ; alias waitHist "set x = 1\
?  while ( ${D}x != 4 )\
?    tail -1 hist1.dat | awk '${D}2==4{exit 1}' || break\
?    sleep 2\
?  end\
? echo 'done'\
? cd /users/ "

$ waitHist
done

$ pwd
/users/
1 Like

@corona688 - you were right - cd does not work. I have hence decided to scan the file in another folder instead of changing the folder. I finally got the code to work

#!/bin/csh

#Initialise dummy variable x
 set x = 1

 while ( $x != 50 )
   set x=`tail -1 ../hist.dat | awk '{print $2}'`
   sleep 100
   echo "Iterations completed:$x"
 end
 
echo "Submitted"

---------- Post updated at 05:51 AM ---------- Previous update was at 05:05 AM ----------

Chubler_XL I will give your code a go.

Thanks a lot!