1

I'm trying to write an expect script that reads commands from a file and executes them, as explained in this topic. Here is my script called script.sh:

#!/usr/bin/expect

set f [open "script_cmds.txt"]
set cmds [split [read $f] "\n"]
close $f

foreach cmd $cmds {
    spawn cmd 
}

expect eof
close

And the file script_cmds.txt, situated in the same folder, looks like this:

mkdir cmdlisttest1
mkdir cmdlisttest2

To compile and run the script on cygwin I use

chmod +x script.sh
d2u script.sh
./script.sh

The output I get reads

spawn cmd
spawn cmd
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

C:\cygwin\home\user>

and then it stops there without exiting. The folders cmdlisttest1 and cmdlisttest2 are not created.

I'm fairly new to expect scripts; can anyone spot the mistake(s)?

Community
  • 1
  • 1
jorgen
  • 3,425
  • 4
  • 31
  • 53

1 Answers1

1

You're executing spawn cmd instead of spawn $cmd.
With $cmd you're reading the value stored in the variable cmd and that's what you want.

whoan
  • 8,143
  • 4
  • 39
  • 48
  • 1
    Apparently that's a line-breaks problem. When you execute `d2u`, does it make the conversion *in place*? I suggest you `tr -d '\r' < script.sh > script_unix.sh` instead of `d2u`, and then execute: `./script_unix.sh` – whoan Dec 27 '14 at 15:05
  • I still can't quite get it to work; since the question has changed I posted a new topic: http://stackoverflow.com/questions/27677866/expect-script-not-able-to-read-mkdir-from-file-and-execute – jorgen Dec 28 '14 at 15:38