1

I wrote a shell script which calculates the execution time of itself but the problem I'm facing is its not able to send the time difference via email. Please Help.

Here is the Script

#!/bin/bash

start=$(date +%s.%N)

ls /etc > /tmp/x

rm /tmp/x

executiontime(){
end=$(date +%s.%N)
diff=$(echo "$end-$start" | bc)
echo "Inside function $diff"
}

#Email Function
Sender="Sendmail"
Recpt="[email protected]"
Sub="Test"
Message="sendmail function $diff"

#Sendmail Function
sendmailfun() {
sendmail -F $Sender -it <<END_MESSAGE
To: $Recpt
Subject: $Sub

$Message
END_MESSAGE
}

executiontime
echo "outside function $diff"
sendmailfun

When I run this script the output on the cmd line is like follows

root@itpl-lptp-008:/home/incaendo/Downloads# sh executiontest.sh 
Inside function .008025182
outside function .008025182

But the email I get after executing the above script just have the line as follows

sendmail function i.e. without the $diff value. why so? Is there anyway I can send the time difference via email.

1 Answer 1

2

The statement

Message="sendmail function $diff"

evaluates once when the script is executed. Then diff is "", so Message is sendmail function. The statement is not reexecuted after you set diff to a new value, so sendmail function is what gets emailed.

Wouldn't it be simpler to just run

time myscript.sh | mail -s "Test" [email protected]

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .