Expect starts its own tcl shell, so you cannot use aliases defined in your bash environment.
Expect does have the variable $env(YOURBASHVARIABLE)
, which allows Expect to grab your environment variables, but Expect can only modify them internal to the script. However, any modifications you make to the variable will not be kept once the expect script is finished.
If flag is going to be a number, you could use an exit status (e.g., exit 5
) and then use $?
in your script to get the exit status.
Per your update
The expect script doesn't return anything, it just sets an exit code.
What you could do is simply:
$(expect -c '
spawn ssh-copy-id -i '"$SSH_KEY_PATH_PUB $REMOTE_HOST_USER@$REMOTE_HOST_IP"'
expect "*?assword:*"
send "'"$REMOTE_HOST_PASSWD"'\r";
expect {
"Permission denied, please try again."{
send user "Wrong pass"
exit 5
}
}
'); var=$?
This way, var
will be set to your exit status.
Also, you should take note of this:
By convention, environment variables (PATH, EDITOR, SHELL, ...) and
internal shell variables (BASH_VERSION, RANDOM, ...) are fully
capitalized. All other variable names should have at least one
lowercase letter. Since variable names are case-sensitive, this
convention avoids accidentally overriding environmental and internal
variables.
EDIT(mpapis): there is also other use case:
if output="$(expect ...)"
then
echo "it worked: $output"
else
result=$?
echo "it failed($result): $output"
fi
EDIT(twmb)
With the last use case, you have to be careful with what you are returning. It will take all output sent to the user. Unless you have logging turned off (with log_user 0
) and you are controlling exactly what will be outputted in the expect script, you will probably get more information than needed.
Another drag with this is indicated in the comment below;
returned="$(expect -c '
log_user 1 ;# turn to 0 and use send_user to control the exact output
spawn bash
expect "\\$"
send "echo hello\r"
send_user "this will be returned"
expect "\\$" ;# without this line, the script would exit too fast
;# for the "echo hello" to be sent to stdout by bash
;# and thus wont be recorded
exit 6
'
)"; var=$?
echo "var: $var"
echo "returned: $returned"