I’m just a beginner in shell script. I’m reading the code of my co-worker and I don’t know what is the meaning of the below code. Can any one help me on what is the meaning of the below code, specially the RESULT line.
#!/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
the filename of this script is generate.sh
This line
RESULT=$(curl -s 127.0.0.1/services/generatereport.php);is using cURL to load a resource, in this case the file generatereport.php. UPDATE: It is basically executing the command curl, which requests a file from a server. The option -s is silent mode, to avoid any error messages or progress bar. From the curl documentation:Since the IP address used is 127.0.0.1 (localhost), he is simply executing the file from the folder services in localhost. The output of the file is stored in the variable RESULT.
The next if statement
[[ "$RESULT" =~ "$ENDRESULT" ]];, compares the end of RESULT with the value of the variable ENDRESULT, which is “DONE”, in that case the report generation has finished, and it stores the sentence"Generation Ended"in a logfile, as “[Date]Generation Ended“.The logfile is located in
/var/www/ema/services/generate.logIn the second case, it stores the output of
generatereport.phpin the log file as well, although this time it also calls the shell script file located at/var/www/ema/services/generate.sh