I have tried to search process id i-e 6762 stored in a variable say buffer
nohup tcpdump -ni eth0 -s0 2>&1
Almost right. You need a space to separate the first and second arguments. Also I would change the first \d to \d+, as there's always the possibility that you could have more than 9 background jobs.
if {[regexp {\[\d+\]\s+(\d+)} $line junk pid]}
Also [info exists ...] acts on a variable, not a value:
[info exists pid]
Edit: Add example of final code snippet
There is a missing space in the foreach line. There needs to be a space before the {. And the body of the if statement was not attached.
The parser in Tcl doesn't work in the same manner as some other languages. Line continuations and spaces are important.
So the final code will look like:
foreach line [split $buffer "\n"] {
if { [regexp {\[\d+\]\s+(\d+)} $line junk pid] } \
break
}
if { [info exists pid] } {
puts "PID of nohup is $pid"
}
The if statement could also be (better):
if { [regexp {\[\d+\]\s+(\d+)} $line junk pid] } {
break
}