Вопрос

i'm using expect to automate some bash scripts. All goes right until i need to execute a sourced script; then i get this error:

couldn't execute "source setNAME.sh": no such file or directory
while executing
"spawn "source setNAME.sh""
(file "./setNAME.exp" line 2)

I use expect in a basic way (spawn/expect/send/interact); the problem is with spawn. Let me show a very easy example:

Script to read a name, echo the value and export to the environment as 'NAME':

/home/edu/scripts [] cat setNAME.sh
#!/bin/bash
echo "Your name?"
read name
export NAME=$name
echo "Provided NAME is $name"

If you execute with 'source' then the input value will be exported as environment variable. Just what i need:

/home/edu/scripts [] source setNAME.sh
Your name?
Dennis
Provided NAME is Dennis

/home/edu/scripts [] echo $NAME
Dennis

Let's to use expect to avoid interaction, setting 'edu' as the name:

/home/edu/scripts [] cat setNAME.exp
#!/usr/bin/expect
spawn setNAME.sh
expect "Your name?"
send edu\r
interact

/home/edu/scripts [] setNAME.exp
spawn setNAME.sh
Your name?
edu
Provided NAME is edu

/home/edu/scripts [] echo $NAME
Dennis

But the name, obviously, is still Dennis: 'edu' is shown at sh script output but not exported as NAME because expect script spawns the setNAME.sh script WITHOUT source (or dot before script).

The problem is that I CAN'T DO THIS because i get the error commented at the beginning.

Any ideas to solve/afford this need ?

In my case the script is much more complex and other solutions are also invalid, for example things like this:

source script.sh << EOF
<option1>
<option2>
...
EOF

That's the reason i'm trying to use expect...

Thanks a lot!

Это было полезно?

Решение

source is a shell builtin, not a system command, so you first need to spawn a shell then send the source command to it. That shell will then have the value in the NAME variable

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top