简体   繁体   中英

Run sh script in Tcl foreach loop

I have a simple bash script, test.sh, that takes four arguments.

#!/bin/bash
echo "1: $1"
echo "2: $2"
echo "3: $3"
echo "4: $4"

I try to call this from a Tcl script, test.tcl

exec bash test.sh arg1 arg2 arg3 arg4

foreach i {1 2 3} {
    exec bash test.sh arg1 arg2 arg3 arg4
}

The first call to the script outputs as I expect it to, but the calls from the foreach loop never seem to do anything. In fact, the exec command can be replaced with exec ls to make things even simpler; the call outside the loop works fine but the one from inside the loop doesn't do anything.

EDIT As pointed out in the comments, it's probably important to mention I am using a Tcl console that is built into a software package (VMD, visual molecular dynamics). From that terminal interface, I call these scripts "interactively," and see output on the terminal from the exec outside the loop, but not from the one inside the loop.

My work is on hold because of this, any ideas?

The apparent "issue" stems from trying to run these scripts "interactively." If I modify the bash script as

#!/bin/bash
echo "1: $1" > $5
echo "2: $2" >>$5
echo "3: $3" >>$5
echo "4: $4" >>$5

and the tcl script as

exec bash test.sh arg1 arg2 arg3 arg4 file1.txt

foreach i {1 2 3} {
    exec bash test.sh arg1 arg2 arg3 arg4 file2.txt
}

I see both files, file{1,2}.txt , created properly. So it automatically prints to the terminal interface when the script is called outside the loop but not when called inside. This is explained more in the comments above.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM