3

I have made the below expect script and I need to log the output of that script.

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

I need to log the output of expect command in ${LOGFILE}. How can It be done?

I have tried adding the below things, it doesn't work. What could be done?

/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
  • 3
    Use of backticks is deprecated use $(…) instead. – ctrl-alt-delor Jul 11 '16 at 8:46
0

It looks like LOGFILE is out of scope, did you export it. See environment variables.

You need to export a variable to the environment, or pass it as an argument.

e.g. Let us say your program is called abc then do:

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

or

LOGFILE=./logdir/logfile ./abc

Alternatively, pass as argument:

./abc "./logdir/logfile"

And have in program:

logfilename="$1"
  • YEah I have the LOGFILE value declared earlier in my script. I didnt add it in my question. LOGFILE="/home/oracle/logs/" You want it to be exported rather than declaring it? – sabarish jackson Jul 11 '16 at 9:19
  • @sabarishjackson That logfile name ends in / so is a directory not a file. – meuh Jul 11 '16 at 10:23
  • Edit the question to show the smallest example that still exhibits the problem, but no smaller. Do not miss out impotent bits. Test your example to check that it still exhibits your problem (not a different one). – ctrl-alt-delor Jul 11 '16 at 11:58
  • @meuh I gave it as a sample. I had the path to the file_name.txt – sabarish jackson Jul 12 '16 at 5:40
0

The below mentioned redirection is working fine now. I didn't add anything new.

/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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.