记录 Expect 命令的输出

记录 Expect 命令的输出

我已经制作了下面的期望脚本,我需要记录该脚本的输出。

SOURCE_FILE=`ls -l *.txt --time-style=+%D | grep ${DT} | grep -v '^d' | awk '{print $NF}' `
if [ -n "${SOURCE_FILE}" ]
then
    cp -p ${SOURCE_FILE} ${T_FILES}
    /usr/bin/expect<<EOD

        set timeout 60
        spawn   sftp $ES_SFTP_USER@$ES_SFTP_HOST_NAME:$R_LOCATION
        expect "*?assword:"
        send "$password\r"
        expect "sftp>"
        send "put /opt/AppServer/ES_TEST/todays_report/*.txt\r"
        expect "sftp>"
        send "bye\r"
        expect EOD
    EOD
else
    echo "No Files to copy" >> ${LOGFILE}
fi

我需要将 Expect 命令的输出记录在${LOGFILE}.如何做呢?

我尝试添加以下内容,但不起作用。可以做什么?

/usr/bin/expect<<EOD >> ${LOGFILE} 2>&1

set timeout 60
spawn   sftp $ES_SFTP_USER@$ES_SFTP_HOST_NAME:$R_LOCATION
expect "*?assword:"
send "$password\r"
expect "sftp>"
send "put /opt/AppServer/ES_TEST/todays_report/*.txt\r"
expect "sftp>"
send "bye\r"
expect EOD
EOD

答案1

看起来 LOGFILE 超出了范围,您是否导出了它?请参阅环境变量。

您需要将变量导出到环境中,或将其作为参数传递。

例如,假设您的程序被调用abc,然后执行以下操作:

LOGFILE=./logdir/logfile
export LOGFILE
./abc

或者

LOGFILE=./logdir/logfile ./abc

或者,作为参数传递:

./abc "./logdir/logfile"

并在程序中有:

logfilename="$1"

答案2

下面提到的重定向现在工作正常。我没有添加任何新内容。

/usr/bin/expect<<EOD > ${LOGFILE}

set timeout 60
spawn   sftp $ES_SFTP_USER@$ES_SFTP_HOST:$R_LOCATION
expect "*?assword:"
send "$password\r"
expect "sftp>"
send "put /opt/AppServer/ES_billing_report/todays_report/*.txt\r"
expect "sftp>"
send "bye\r"
expect EOD
EOD

相关内容