Home > Net >  Expect/TCL Commandline Interact with same CLI where the script started from
Expect/TCL Commandline Interact with same CLI where the script started from

Time:07-05

Using Expect/TCL and wanting to send a specific command to the same CLI I started the expect script from.

Not one of the following worked:

send "MyScript -bla blubber -super cmd.txt \r"

exec "MyScript -bla blubber -super cmd.txt \r"

exec "/Path/To/MyScript -bla blubber -super cmd.txt \r"

send "/Path/To/MyScript -bla blubber -super cmd.txt \r"

MyScript is the script i need and bla etc are additional informations for the call.

I need the expect script to use the same user and same directory like the one I started the expect script from.

Thx, G

CodePudding user response:

To get the directory that contains the currently running script, use:

variable containingDirectory [file dirname [file normalize [info script]]]

This should be placed outside any procedure. It will not work if you use a bash heredoc for the script, or rather it works per se, but the answer is completely useless; that's a case where things get rewritten behind the scenes to be in a totally different location so don't do that.

Once you have the directory, you can form names with either:

set filename $containingDirectory/MyScript

or:

set filename [file join $containingDirectory MyScript]

They're exactly equivalent on POSIX platforms.

CodePudding user response:

When using exec, you need to pass each part of the command as an individual argument, and you shouldn't include an end-of-line character:

exec MyScript -bla blubber -super cmd.txt

If you want to use expect functionality, you can spawn a shell:

spawn sh
send "MyScript -bla blubber -super cmd.txt \r"

Both of these will run with the same current directory, environment, and user as when the expect script was executed, unless you purposely change that by using cd and/or set env(ENVVAR) something commands.

They will also both start a new subprocess, so they can't change anything in the process of the expect script.

  • Related