4

我只是shell脚本的初学者。我正在阅读我同事的代码,但我不知道下面的代码是什么意思。任何人都可以帮助我了解以下代码的含义,特别是该RESULT行。

#!/bin/bash
DATETIME=$(date);
LOGFILE="/var/www/ema/services/generate.log";
ENDRESULT="DONE";

RESULT=$(curl -s 127.0.0.1/services/generatereport.php);

if [[ "$RESULT" =~ "$ENDRESULT" ]]; then
    RESULT="Generation Ended";
    echo "["$DATETIME"]"$RESULT >> $LOGFILE;
else
    echo "["$DATETIME"]"$RESULT >> $LOGFILE;
    /var/www/ema/services/generate.sh;  
fi

该脚本的文件名是generate.sh

4

2 回答 2

2

我已经为你评论了代码:)

#!/bin/bash
DATETIME=$(date); // Get current date
LOGFILE="/var/www/ema/services/generate.log"; // Where to save data
ENDRESULT="DONE"; // What to expect at the end of data

RESULT=$(curl -s 127.0.0.1/services/generatereport.php); // Request data from PHP running on localhost

if [[ "$RESULT" =~ "$ENDRESULT" ]]; then // If $RESULT ends with "DONE" then log to file that everything is okay;
    RESULT="Generation Ended";
    echo "["$DATETIME"]"$RESULT >> $LOGFILE;
else // Otherwise write down the error and run some other script.
    echo "["$DATETIME"]"$RESULT >> $LOGFILE; 
    /var/www/ema/services/generate.sh;  
fi
于 2012-12-26T14:33:48.040 回答
2

此行RESULT=$(curl -s 127.0.0.1/services/generatereport.php);使用cURL加载资源,在本例中为文件generatereport.php更新:它基本上是执行命令 curl,它从服务器请求文件。选项 -s 是静默模式,以避免任何错误消息或进度条。从卷曲文档:

-s,--静音

静音或静音模式。不要显示进度表或错误消息。使 Curl 静音。

由于使用的 IP 地址是 127.0.0.1 (localhost),他只是从 localhost 中的文件夹服务执行文件。文件的输出存储在变量 RESULT 中。

下一个 if 语句[[ "$RESULT" =~ "$ENDRESULT" ]];RESULT的结尾与变量ENDRESULT的值(即“DONE”)进行比较,在这种情况下,报告生成已完成,并将该语句存储"Generation Ended"在日志文件中,如“[Date] Generation Ended ” .

日志文件位于/var/www/ema/services/generate.log

在第二种情况下,它也将 的输出存储generatereport.php在日志文件中,尽管这一次它也调用位于/var/www/ema/services/generate.sh

于 2012-12-26T14:34:35.873 回答