LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 08-20-2012, 03:10 PM   #1
cgeekwannabe
LQ Newbie
 
Registered: Jul 2012
Posts: 17

Rep: Reputation: Disabled
How do I get expect to send a response back that my calling program can use?


I have a script (xxx.sh) that runs on RHEL and uses expect.
It does an ssh over to another RHEL, logs in and runs a script (yyy.sh) Pretty simple so far. For simplicity's sake, let's say all yyy.sh does is

"ls -ltr rrr*"

So what I need is for the $? from the last command (i.e. the execution of the script) to be available.

I put this line

send "export NEW_STAT=$?\r"

in my expect script to trap the status of yyy.sh running.

What I did was write another little script that does this

if [[ ${NEW_STAT} -eq 0 ]]
then
{
echo "success"
exit 0
}
else
{
echo "failure"
exit 1
}
fi

And ran it after another

expect "#"

My thinking is that when this script returns, I can do an expect on either success or failure and handle it correctly. The problem is that my export command isn't working. In fact, it doesn't seem to be running at all. I know this because the first line is the auxiliary script was "echo ${NEW_STAT}" which was showing nothing and should be either 0 or 1.

I've googled all day trying to figure this out; someone must have done this before...expect is so great at what it does, I hope there's a simple solution for this.
 
Old 08-20-2012, 03:40 PM   #2
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541

Rep: Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065
When you execute a shell program that program executes in a new process (with a unique PID). When it exits, anything it has done to the environment is lost (which is why you don't see the export). The exit status of that program is returned to the calling process (your shell).

Now, that's if you simply type the name of the program and hit the carriage return. If you run a program with a leading dot-space, called a Dot Command, it will execute in your current PID and whatever it does to the environment will be available at exit.

You can also include an exit line with a value that you'd like returned to the shell (note that this will most likely kill your shell if you use a Dot Command to execute it):
Code:
echo "This is something."
exit 10
Then do
Code:
echo $?
10
If you had a shell program, prog, that includes an "export" (but not an exit value) you would execute it with
Code:
. prog
Then
Code:
echo ${NEW_STAT}
to see that value.

Hope this helps some.

Last edited by tronayne; 08-20-2012 at 03:46 PM.
 
Old 08-20-2012, 04:22 PM   #3
cgeekwannabe
LQ Newbie
 
Registered: Jul 2012
Posts: 17

Original Poster
Rep: Reputation: Disabled
Thank you for your assistance.
Now let me go a bit deeper into what I am trying to accomplish here.

We have a number of bash scripts that are running on various linux servers, the purpose of which is to automate some processes which are done manually on a period basis. These scripts have been organized into Use Cases which have been converted into schedules which are to be invoked by some schedulling software. The scheudlling software has the ability to log in and run the scripts and have a return code sent back to the controlling software so that dependencies can be determined.

What I have been tasked to do is to simulate this. So what I've done is set up a skeleton script that can run step by step a job. The one I'm working on is 22 steps and runs on 3 different servers, all of them RHEL.


The skeleton script is pretty simple. You tell it what step to start from and it runs each step in turn and checks the return code of the previous step before continuing.

I had to come up with a way to log in remotely to two of the servers in the picture so that the scripts I needed to run there could be run. That was what I thought was going to be difficult. As it turns out, expect handles it quite well.


I do something like this (this is from memory as I am not at the office right now)

CMD="$1\r"
SERVER=$2
/usr/bin/expect - <<EOF
spawn ssh user@$SERVER
expect "password"
send -- "MyPassword"
expect "#"
send "sudo su - newuser"
expect "password"
send -- "MyPassword"
# I am now newuser and can run jobs as newuser
expect "#"
send -- CMD
# and here's where the problem is
EOF

I know that the command (script) that is being called is running and giving me a return code. What I need is for expect to make that RC available so that when it's done and logged out, my calling script knows what it was. So I created a little script that would check the value of the return code and write out something to the screen (stdout) that expect could see and handle. I guess the problem is, in essence, how to get the RC from CMD into my check script. I'm pretty sure that under bash (or most other shells), if I run a script or command, $? contains the return code from the last command executed and can be exported and made available to subsequent shells. But it seems to me that for some reason, expect is NOT letting me export the value out.

I just tried it on Fedora Core VM and export LAST_CC=$? makes $LAST_CC available to a program that echos the value out .
I'll play around with this on FC tonight and see if I can figure it out because it doesn't make sense to me.
 
Old 08-20-2012, 09:05 PM   #4
speck
Member
 
Registered: Nov 2001
Location: US
Distribution: Slackware 14.2
Posts: 375

Rep: Reputation: 115Reputation: 115
Is there a reason why you're using heredocs instead of running the actual Expect script from within the shell script (passing your variables as arguments to the Expect script)?
 
Old 08-20-2012, 11:27 PM   #5
cgeekwannabe
LQ Newbie
 
Registered: Jul 2012
Posts: 17

Original Poster
Rep: Reputation: Disabled
Not sure what you mean by heredocs. I guess I should have said that the code I showed is part of a bash shell script which is invoked with the script name, the command (remote script) to be run and and server upon which it is to be run. The one thing I did notice was that the user I su(ing)to is using the "C" shell by default so I have to invoke a bash shell ... I wonder if that is what is screwing things up.
 
Old 08-21-2012, 02:32 AM   #6
speck
Member
 
Registered: Nov 2001
Location: US
Distribution: Slackware 14.2
Posts: 375

Rep: Reputation: 115Reputation: 115
Your "<<EOF" and "EOF" are heredocs.

Try something like the following, with one shell script and one expect script:

Code:
#!/bin/sh

SERVER=$2

/path/to/expect_script.exp $SERVER

### Capture return code from expect_script.exp
RC=$?
Code:
#!/usr/bin/expect

set SERVER [lindex $argv 0]

spawn ssh user@$SERVER
expect "password"
send -- "MyPassword"
expect "#"
send "sudo su - newuser"
expect "password"
send -- "MyPassword"
# I am now newuser and can run jobs as newuser
expect "#"
send -- CMD
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Expect script: how do i send function key F12 in an expect script alix123 Programming 4 09-01-2013 09:06 PM
[SOLVED] Expect syntax issue when calling a bash sed command that uses special character '}' YortheHunter Programming 6 08-10-2012 09:31 AM
Calling expect scripts from other expect scripts sevapopov Linux - Software 3 04-03-2008 09:33 PM
bash/ksh: Automatically send response to a program jimieee Programming 6 01-11-2007 05:45 PM
Trouble calling 'expect' from bash raypen Linux - Software 1 06-01-2006 08:05 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 04:05 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration