How to keep monitoring a file to be created in a directory in TCL? - directory

How to keep on waiting till a file is created in a directory within 50 seconds in TCL and prints an error if the file is not created.
For eg, in /tmp directory I need to print a msg "File available" if a file named abc.txt is available within 50 seconds, and if not, print an error message "Timed-out!"
I tried the below code:
set lCount 0
while { [file exists $lFileCreated] == 0 } {
if { $lCount > 50 } {
puts "Timed-out!"
}
after 5000
incr lCount 5
puts "Waited for $lCount seconds for $lFileCreated"
}
puts "File Available"
Is this the right way? I think it will fail for remote files.

Related

How to fix error message in tcl script having command [exec bjobs] when no jobs are running?

when I am running a Tcl script that contains the following lines:
set V [exec bjobs ]
puts "bjobs= ${V}"
When jobs are present it's working properly but, no jobs are running it is showing an error like this:
No unfinished job found
while executing
"exec bjobs "
invoked from within
"set V [exec bjobs ]"
How to avoid this error? Please let me know how to avoid this kind of errors.
It sounds to me like the bjobs program has a non-zero exit code in this case. The exec manual page includes this example in a subsection WORKING WITH NON-ZERO RESULTS:
To execute a program that can return a non-zero result, you should wrap
the call to exec in catch and check the contents of the -errorcode
return option if you have an error:
set status 0
if {[catch {exec grep foo bar.txt} results options]} {
set details [dict get $options -errorcode]
if {[lindex $details 0] eq "CHILDSTATUS"} {
set status [lindex $details 2]
} else {
# Some other error; regenerate it to let caller handle
return -options $options -level 0 $results
}
}
This is more easily written using the try command, as that makes it
simpler to trap specific types of errors. This is done using code like
this:
try {
set results [exec grep foo bar.txt]
set status 0
} trap CHILDSTATUS {results options} {
set status [lindex [dict get $options -errorcode] 2]
}
I think you could write this as:
try {
set V [exec bjobs ]
} trap CHILDSTATUS {message} {
# Not sure how you want to handle the case where there's nothing...
set V $message
}
puts "bjobs= ${V}"
if {[catch {exec bjobs} result]} {
puts "bjobs have some issues. Reason : $result"
} else {
puts "bjobs executed successfully. Result : $result"
}
Reference : catch
Note carefully in the exec man
page:
If any of the commands in the pipeline exit abnormally or are killed or
suspended, then exec will return an error [...]
If any of the commands
writes to its standard error file and that standard error is not
redirected and
-ignorestderr is not specified, then exec will return an
error.
So if bjobs returns non-zero or prints to stderr when there are no jobs, exec needs catch or try as Donal writes.

zsh: Do I need to close file descriptors?

I use the following code to both output something to stdout, and pipe it to a program:
function example() {
local fd1
{
exec {fd1}>&1
{ echo hi >&$fd1 } | true
} always { exec {fd1}>&- }
}
I am wondering if I can safely drop always { exec {fd1}>&- }. fd1 goes out of scope after the function finishes anyways.
You need to keep always { exec {fd1}>&- }. If you get rid of that, the variable containing the file descriptor will go out of scope, but the file descriptor won't be closed, resulting in leaking it. You can see this by doing ls -l /proc/$$/fd before and after running your function without that line. Each run of the function will permanently add another FD to that list. Eventually, you'll run out of file descriptors and won't be able to open any new ones, which will break things.

How to use a Runtime variable in grep command in Unix

I have a Time.sh file which has the code to store the StartTime and EndTime, entered by the user in runtime. The code in Time.sh fie is
read -p "Enter Start Time : " StartTime
read -p "Enter End Time : " EndTime
echo "Start and End Time are:" $StartTime, $EndTime
The following is the code I used to get the StartTime and EndTime in Runtime.
$ chmod +x Time.sh
$ ./Time.sh
Now I have to use this values in the two variables and print the data in the file "Test.log". The data in Test.log file is as follows,
May 10 01:07:05 server1 user:err|error IIB[78658909]: error text
May 10 01:07:06 server1 user:notice superstp[78653958]: infotext notice text
May 10 01:07:07 server1 user:info syslog: infotext
May 10 01:07:08 server1 user:err|error IIB[78658909]: error text
May 10 01:07:09 server1 user:warn|warning IBM Java[78650709]: warning text
May 10 01:07:10 server1 user:info syslog: infotext
May 10 01:07:11 server1 user:err|error IIB[78658909]: error text
May 10 01:07:12 server1 user:warn|warning IBM Java[78650709]: warning text
May 10 01:07:13 server1 user:notice superstp[78653958]: infotext notice text
May 10 01:07:14 server1 user:info syslog: infotext
May 10 01:07:15 server1 user:warn|warning IBM Java[78650709]: warning text
I have used the following code,
grep < "$EndTime" Test.log
But it is not working. Can anyone help to use both the variables and get the data within the StartTime and EndTime. Thanks in Advance.
The following should help:
BEGIN { Found = 0; EndFound = 0;}
{
if($0 ~ Start)
{
Found = 1;
}
if($0 ~ End)
{
EndFound = 1;
}
else
{
if(EndFound == 1)
{
Found = 0;
}
}
if(Found)
{
print $0;
}
}
It should be run as awk -v Start="01:07:06" -v End="01:07:10" -f program data_file, where program contains the code above and "01:07:06" and "01:07:10" start and end times respectively.
This is an example only. Please change fit it for your exact requirements.

Dccp protocol simulation in ns2 2.34

How to add dccp patches to ns2 2.34? Please give me detailed steps.
The file is the file is ns234-dccp-1.patch.
The error comes when I try to simulate dccp is
Kar#ubuntu:~$ ns audiodccp.tcl
invalid command name "Agent/DCCP/TCPlike"
while executing
"Agent/DCCP/TCPlike create _o726 "
invoked from within
"catch "$className create $o $args" msg"
invoked from within
"if [catch "$className create $o $args" msg] {
if [string match "__FAILED_SHADOW_OBJECT_" $msg] {
delete $o
return ""
}
global errorInfo
error "class $..."
(procedure "new" line 3)
invoked from within
"new Agent/DCCP/TCPlike"
invoked from within
"set dccp1 [new Agent/DCCP/TCPlike]"
(file "audiodccp.tcl" line 50)
UBUNTU-10.04
NS2 allinone 2.34
audiodccp.tcl : Unknown file.
invalid command name "Agent/DCCP/TCPlike"
→ → You have a failed build. Or you are using the wrong executable 'ns'. The suggestion is to do :
cd ns-allinone-2.34/-ns-2.34/
cp ns ns-dccp
sudo cp ns-dccp /usr/local/bin/
... and then do simulations with $ ns-dccp [file.tcl]
You can also use ns-2.35, which has DCCP included by default.
Note : You can have as many times ns-allinone-2.xx as you want, installed at the same time. But : Do never add any PATH text to .bashrc. Not required.

Unix system programming: File open error

I am trying to open a file which I created just before open command. But it hangs at open() command line. Do you have any idea?
if(mkfifo("test", S_IRWXU | S_IRWXG | S_IRWXO))
{
printf("File creation error.\n");
return 0;
}
// Hangs below
while (((test_fd = open("test", O_RDONLY)) == -1) && (errno == EINTR));
from the manpage of mkfifo :
Opening a FIFO for reading normally blocks until some other process opens the same FIFO for writing, and vice versa.
See fifo(7) for nonblocking handling of FIFO special files.

Resources