Ray
Ray

Reputation: 21

Unix Shell Script does not send intended email

I have a shell script like this. The purpose of this script is to tail+head out a certain amount of data from file.csv and then send it to email [email protected]. DataFunction seems to work fine alone however when I try to call DataFunction within the email function body. It seems it sends a empty email with the correct Title and destination. The body of the email is missing which should be the data from DataFunction. Is there a workaround for this ? Thank you in advance.

#!/bin/bash
DataFunction()
{
tail -10 /folder/"file.csv" | head -19
}
fnEmailFunction()
{
echo ${DataFunction}| mail -s Title [email protected]
}
fnEmailFunction

Upvotes: 0

Views: 72

Answers (3)

Jonathan Leffler
Jonathan Leffler

Reputation: 753455

You are echoing an unset variable, $DataFunction (written ${DataFunction}), not invoking the function.

You should use:

DataFunction | mail -s Title [email protected]

You may have been trying to use:

echo $(DataFunction) | mail -s Title [email protected]

but that is misguided for several reasons. The primary problem is that it converts the 10 lines of output from the DataFunction function into a single line of input to mail. If you enclosed the $(DataFunction) in double quotes, that would preserve the 'shape' of the input, but it wastes time and energy compared to running the command (function) directly as shown.

Upvotes: 1

The2ndSon
The2ndSon

Reputation: 327

I tried @0xAX's answer and couldnt get it to work properly within a bash script. You simply need to save the output of DataFunction() in some variable. You can simply use these three lines to achieve this.

    #!/bin/bash

    VAR1=`tail -10 "/folder/file.csv" | head -19`
    echo $VAR1 | mail -s Title [email protected]

Upvotes: 0

0xAX
0xAX

Reputation: 21817

Try this:

mail -s "Title" "bob@123com" <<EOF 
${DataFunction} 
EOF

Upvotes: 0

Related Questions