expect error handling and piping output to log file

Posted on 2009-04-22
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 "'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
    LVL 51

    Expert Comment

    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

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

    Accepted Solution

    then your expect script returned 0

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Join & Write a Comment

    This article will show, step by step, how to integrate R code into a R Sweave document
    Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
    An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
    The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

    728 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

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now