0

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

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Excellent solution Jonathan. Well thought out. Thank you very much for your effort. Next time I'll remember that.
0

Try this:

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

Comments

0

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]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.