You either need to transfer the parts list to the local side (i.e., the system that actually runs expect) or you need to move the iteration to the remote side.
If I had to choose between the two approaches, I'd probably use the first option; that would give me far more options for complex handling of unexpected conditions.
Getting the list locally
To move the part list locally, we adapt your script so that it runs the df …
command and writes the output to where we can see it, and use a more complex expect
command to process the output.
exp_send "df -T|grep ext*|awk '{ print \$1 }'\r"
set theParts {}
expect {
-re {[^\s#]+} {
lappend theParts $expect_out(0,string)
exp_continue
}
"#" {
# Just an empty script here
}
}
This expect
will look for two things at once, but with the first one if it finds a sequence of non-space non-hash characters, it appends that sequence (stored by default in that particular “field” of the expect_out
array) to the list in the variable theParts
and keeps waiting. (That's what the exp_continue
does.)
Now that you have a list, you could write it out to a local parts.txt
, but why would you bother? Might as well use foreach
to walk over it directly:
foreach part $theParts {
exp_send "dumpe2fs -h $part\r"
expect "#"
exp_send "e2fsck -n $part\r"
expect "#"
}
Moving the looping remotely
The other approach is to put the looping on the other machine.
exp_send "df -T|grep ext*|awk '{ print \$1 }' >parts.txt\r"
expect "#"
exp_send "for part in `cat parts.txt`; do dumpe2fs -h \$part || break; e2fsck -n \$part || break; done\r"
expect "#"
This is quite a bit shorter, but in the form above not very robust. You can make robust shell scripts, but it takes quite a bit of work (and is much easier in my experience if you write the shell script itself to a file on the remote host and then run it from there).
For your understanding, the looping script I've done above is this (when split over multiple lines and without the backslashes for Tcl, which are only really needed before Tcl metacharacters — in this script, that's $
only):
for part in `cat parts.txt`; do
dumpe2fs -h $part || break
e2fsck -n $part || break
done