简体   繁体   中英

Write ActiveState Tcl Program for Windows that expects a password and enters it

I want to write a shell program in windows that runs another shell script and expects a password prompt from the Git bash terminal and inputs it. This is what I have so far:

#!/bin/sh
# \
exec tclsh "$0" ${1+"$@"}
package require Expect

spawn sampleScript.sh
expect "Password:"
send "pass123"

sampleScript.sh code:

echo 'Hello, world.' >foo.txt

my program outputs the following:

'The operation completed successfully. while executing "spawn sampleScript.sh" 
(file "compare.tcl" line 6)'

However, there is no foo.txt that is created in my local file folder where the scripts are. Can you help?

The key with expect programs is to let the spawned program exit gracefully. As it currently stands, after your expect script sends the password, it immediately exits, and that kills the spawned program too early.

  • If you don't need to interact with the sampleScript (ie just let it run to completion), the last line in the expect script should be

    expect eof
  • Otherwise, use

    interact

Read How to create a Minimal, Reproducible Example -- your updated code does not reproduce the error you're seeing

  • Tcl code:
    1. when you send something, you usually need to "hit Enter": send "password\\r"
    2. Did you add expect eof to the Tcl script? If not, you might be killing sampleScript.sh before it has a chance to create the output file
  • sampleScript.sh: Is that really your sample script? Where's the password prompt?

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