[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


expect error handling and piping output to log file

Posted on 2009-04-22
Medium Priority
Last Modified: 2013-11-10
I need to automate the download of some files from an ftp server using sftp and i'm using expect.  i'm trying to determine the error code after the expect/sftp is done as well as tee the output of the interaction with the server to a log file.  i'm using "tee to put the interaction to a log file.  without the "tee" the error handling (return code from sftp) works just fine.  as soon as i put the "tee" to log file in, the return code is always "0".  How do i tee standard out to a log file as well as get the correct error code?
expect << 'END' 2>&1 | tee -a $LogFile
set timeout -1
spawn sftp $env(UserID)@$env(HostToConnectTo)
expect "UserID@HostName.com's password: "
send -- "$env(Password) \r"
expect "sftp> "
send -- "get $env(FileToTransfer)* \r"
expect "sftp> "
send "bye \r"
if [ $? -eq 0 ]
   echo "SFTP succeded"
   echo "ERROR: SFTP failed"

Open in new window

Question by:nohurt
  • 2
LVL 51

Expert Comment

ID: 24224270
your $? is the status code from tha last command, which is tee
if you want the status of the expect command, use something like;

(expect <<'EoE' 2>&1 ; status=$?) | tee -a logfile

if [ $status -eq o ]; then

Author Comment

ID: 24239052
that didn't work.  it gives me an exit stats of NULL.
Love to see more ideas.
LVL 51

Accepted Solution

ahoffmann earned 375 total points
ID: 24240437
then your expect script returned 0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The SignAloud Glove is capable of translating American Sign Language signs into text and audio.
If you are a mobile app developer and especially develop hybrid mobile apps then these 4 mistakes you must avoid for hybrid app development to be the more genuine app developer.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Suggested Courses
Course of the Month19 days, 6 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question