Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I need to fetch the results of different time ranges and email the results, but i am having trouble with the email part of the script,

how can i send an email with both results? (15min range and cumulative results) here's the line :

 echo -e "Enter Start hour : xx:00:00"#ask user to input base time
    read tI

    echo "Select minute range, Enter 1 to 4"#ask user to select minute range
    echo -e "\t1) XX:15:00 \n\t2) XX:15-30 | XX:30  \n\t3) XX:30-45 | xx:45 \n\t4) XX:45-00 | 1hour"
    read NUM

      if [ "$NUM" == "1" ] ; then
        for Time in "$tI:(0[0-9]|1[0-4]:[0-5][0-9]|$tI:15" ; do #time grep c/o of Caleb, thanks!

      elif [ "$NUM" -eq "2" ] ; then
        for Time in "$tI:(1[5-9]|2[0-9]:[0-5][0-9]|$tI:30)" "$tI:([0-2][0-9]:[0-5][0-9]|$tI:30)" ; do # 2nd time was commulative 

      elif [ $NUM -eq 3 ] ; then
        for Time in "$tI:(3[0-9]|4[0-4]:[0-5][0-9]|$tI:45)" "$tI:([0-3][0-9]||4[0-4]:[0-5][0-9]|$tI:45)" ; do

      elif [ $NUM -eq 4 ] ; then
         newTime=$((tI + 1))
         sTime=`printf '%02d\n' "$tI"`
         fTime=`printf '%02d\n' "$newTime"` # thanks to Gilles and Mark and geekosaur
         for Time in "$sTime:(4[5-9]|5[0-9]:[0-5][0-9]|$fTime:00)" "$sTime:([0-5][0-9]|[0-5][0-9]|$fTime:00)" ; do
   exit 1

exit 1

    function Count()
    echo " " > $textfile
    for ((d=0; d<12; d++))
     dec110=`egrep "$GivenDate $time" * | grep -c "$var ..  ${DECLINE[$d]}" | awk '{SUM += $1} END { print SUM }'`
    .... so on and so fort
    echo -e "tons of to be echoed above /n end of the results to be emailed" >> $emessage

NOW HERE'S MY PROBLEM: ive tried several atempts but non of them seems to be working as they should.

function mail()
   echo "declarations above of this | start of message" > $emessage
   for ((t=0; t<2; t++)) do
   Count "$Time"
     /bin/mail -s "$SUBJECT" "$EMAIL" < $emessage
  echo -e "Email Sent!\n\n"
  cat $emessage

-- this will send out 1 email but only shows the result of the first grep,

function mail()
   echo "Hi Team," > $emessage
   Count "$Time"
     /bin/mail -s "$SUBJECT" "$EMAIL" < $emessage
  echo -e "Email Sent! \n\n"
  cat $emessage

-- would send out 2 separate email if i choose option 2-4.

Note : without the email part this script is working fine.

share|improve this question
I have no idea what you're actually trying to do with that script. Post a real half-working script, describe in English what it should do and where it doesn't do what it should do. – Gilles Jul 9 '11 at 10:48
Whoa, editing the question to clarify is one thing, but this is a completely different question. Now you have an answer for the first version of the question and one for the second version, that's confusing. The code you've posted is confusing too, it's made of bits and pieces that don't fit together (exit 1 followed by more code, random-looking indentation, omitted parts, …). Read how to ask (follow the links on the right, as well). Don't throw us a wall of code, you need to meet us halfway and put up a coherent question. – Gilles Jul 10 '11 at 13:05
up vote 3 down vote accepted

First, don't make your function names the same as commands found on normal systems. mail is a standard unix command, I completely misunderstood your script because I thought you were using it.

Second, you probably don't need to write that stuff to a message file at all. Instead take the  mail function out of all the places in your script. Instead just dump the contents to STDOUT. Use echo, grep or whatever other tools you use to collect the content you want to send.

Then wrap the entire part of your script that involves output to be mailed inside {} so that all the output is pulled together and pipe THAT to mail instead.

echo "Enter start hour"
read var # stuff to do that won't get output to mail
echo "Heading for mail:"
grep stuff...
echo "Next heading:"
grep more_stuff...
} | send_to_administrator

function send_to_administrator () {
    /bin/mail -s "$SUBJET" "$EMAIL"
    echo "Confirm mail sent."
share|improve this answer

`echo $var` is the same as $var (unless $var begins with a - or ends with a newline character or (with some shells) contains backslashes). Furthermore, unless you did intend for the value of $var to be used as a list of glob patterns, you must write "$var" (unless you know that the value won't contain any special characters — but it's simpler to just always use double quotes around variable substitutions). If you want to run two iterations of the loop, one where bar is the value of var and one where bar is the value of var2, then just write

for bar in "$var" "$var2"; do
  func "$bar"
share|improve this answer
Hi @Gilles, i used what you said and it worked, i still need some held regarding functions. here's the line : elif [ "$NUM" -eq "2" ] ; then for Time in "var1" "var2" ; do mail done function mail() { for ((t=0; t<2; t++)) do Count "$Time" done /bin/mail -s "$SUBJECT" "$EMAIL" < $msg echo "Email Sent" } -- will send out 1 email but only show the result of the first grep. function mail() { Count "$Time" /bin/mail -s "$SUBJECT" "$EMAIL" < $msg echo -e "Email Sent" } -- would send out 2 separate email. how can i send an email with both results? – afbr1201 Jul 10 '11 at 7:46
@user774187 Without formatting (no newlines, no indentation) this is completely unreadable. Edit your question to add this code. And make it a runnable piece of code, beginning with elif makes no sense. – Gilles Jul 10 '11 at 9:11
i totally edited the question. i hope you could understand what i am having trouble with. – afbr1201 Jul 10 '11 at 10:01

main is a function. they are called the same way you call then in main

share|improve this answer
There's no main here. This is not a C program. – Gilles Dec 24 '13 at 17:22

Your Answer


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.