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?
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
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.
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?
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
@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 ----------