0

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?

JasonN
  • 11
  • 3
  • What actually is your question? Right now, it's a bit “cool story, bro” as I can't see anything to actually answer. What isn't working? What do you see when it doesn't work? What did you expect/hope to see? – Donal Fellows Oct 12 '20 at 15:44
  • I have not ran it yet as I don't have the package downloaded. I was hoping to get a clearer understanding of what a basic tcl program in windows would look like. I just hope to automate a password prompt so I don't have to automatically enter it in the terminal manually every time – JasonN Oct 12 '20 at 15:48
  • A more modern shebang line is `#!/usr/bin/env tclsh` -- that can replace your first 3 lines. – glenn jackman Oct 13 '20 at 22:23

1 Answers1

0

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 (i.e. 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?
glenn jackman
  • 238,783
  • 38
  • 220
  • 352
  • Hi @glenn, my program outputs the following: 'The operation completed successfully. while executing "spawn sampleShell.sh" (file "compare.tcl" line 6)' sampleScript.sh code: echo 'Hello, world.' >foo.txt However, there is no foo.txt that is created in my local file folder where the scripts are. Can you help? – JasonN Oct 13 '20 at 15:59
  • It's hard to format code in comments. Please put that information, as well as your updated expect script, up in the question. – glenn jackman Oct 13 '20 at 17:25