Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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="tarun@example.com"
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.

share|improve this question
add comment

1 Answer 1

up vote 2 down vote accepted

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" tarun@example.com
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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