0

I am trying to execute a set of commands specified in a bash script through my tcl script. I am pretty new to tcl so I could not find a way to do so, I consulted quite a few stack overflow links as well so I now know how to execute a single command from the tcl script.

Right now I am using this to execute a single command -

exec /usr/bin/sshpass ssh -o StrictHostKeyChecking=no $command

But how do I make sure that the script reads one command at a time from the bash script and execute the above command for each of the command?

2
  • Why don't you just send the whole file? Commented Apr 12, 2016 at 13:17
  • That's what I am doing, I am sending the whole file. I just want the commands to be executed one by one so that I may have some kind of interactive output telling me about the status of each command. Commented Apr 14, 2016 at 11:08

1 Answer 1

3
sharad@ss:~$ cat commands.txt 
date
uname -m
sharad@ss:~$ 

sharad@ss:~$ cat my.tcl
set fileHandle [open commands.txt]
set commands [read $fileHandle]
close $fileHandle

foreach command [split $commands "\n"] {
    set command [string trim $command]
    if {$command == ""} {
        continue
    }
    puts "command=$command,result=[exec ssh sharad@localhost bash -c  $command]"
}
sharad@ss:~$ 


sharad@ss:~$ tclsh my.tcl
command=date,result=Tue Apr 12 02:47:28 EDT 2016
command=uname -m,result=Linux
sharad@ss:~$ 
Sign up to request clarification or add additional context in comments.

8 Comments

That should be commands.sh, doesn't make a difference but still.
Well, it may not have any extension at all and it'll still work :) If the intent is to just have a file to store a list of commands and not really treat it as a shell script, .sh extension may be confusing.
Please try to avoid breaking lines before opening braces. It's very bad style.
@PeterLewerin: that's part of coding conventions we follow, in our organization for TCL code. Why do you think it's a bad style? (more chances of error if there's an accidental space after line-break?)
Note that this isn't my personal style choice: it's the universal accepted style (see here, point 6.5). The error you mention is one reason for it, but the main reason is that it makes the code easier to share among programmers. We all have to make compromises with our personal or local styles here on StackOverflow, and this is a compromise you really need to make if you want to be helpful to other Tcl programmers.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.