简体   繁体   中英

tee -a : No such file or Directory

Please have a look at following code.

#!/bin/sh
LOG_FILE=/home/admin/scriptLogs.log
rm -f ${LOG_FILE}
echo "`date`:: Script Execution Started" | tee -a ${LOG_FILE}
DATABASE ACCESS CODE 2>&1
echo "`date`:: Script Execution Successful " | tee -a {$LOG_FILE}
exit 0

It produces following output:

> Tue Feb  7 12:14:49 IST 2017:: Script Execution Started 
> tee:{/home/admin/scriptLogs.log}: No such file or directory 
> Tue Feb  7 12:14:49 IST 2017:: Script Execution Successfull

However, the file is present in the specified location. It also gets appended with data except for the last echo statement. Why such behavior?

Make sure to always quote your variables before expanding them to avoid reinterpretation (read about it here: http://www.tldp.org/LDP/abs/html/quotingvar.html ) Also, you should define LOG_FILE as a string (note the quotes below):

LOG_FILE="/home/admin/scriptLogs.log"

With that said, you have a typo in your script as mentioned by @codeforester

Also, you print that the execution was successful, without checking that it really was.

So you code should look like this:

    #!/bin/sh
    LOG_FILE="/home/admin/scriptLogs.log"
    rm -f "$LOG_FILE"
    echo "`date`:: Script Execution Started" | tee -a "$LOG_FILE"
    DATABASE ACCESS CODE 2>&1
    if [ $? -eq 0 ]; then
      echo "`date`:: Script Execution Successful " | tee -a "$LOG_FILE"
    else
      ...
    exit 0

Note: I have removed the curly brackets, as they are not needed here (although it is not a mistake to do so)

As pointed out by @codeforester, use:

#!/bin/sh
LOG_FILE=/home/admin/scriptLogs.log
rm -f ${LOG_FILE}
echo "`date`:: Script Execution Started" | tee -a ${LOG_FILE}
DATABASE ACCESS CODE 2>&1
echo "`date`:: Script Execution Successful " | tee -a ${LOG_FILE}
exit 0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM