LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-13-2020, 07:17 AM   #1
hatfani
Member
 
Registered: Oct 2019
Distribution: centos7
Posts: 52

Rep: Reputation: Disabled
Exclamation Implement if statement in mail command


Dear all,

Every day I create a mynumbers.csv file which including numbers, for example:
09976543
09976542
09976541

some days it's empty.
I want a script to print the csv file only if it has numbers inside and send it to my mail.
So I built a little script that will print the csv file only if it's more than 0 bytes:
Code:
a=$/my_destination/mynumbers.csv

if [ -s $a ]; then
echo My files: &&cat $a
fi

the thing is that I want to implement it inside my mail command without creating a txt file. I want something like that:
Code:
echo (print the csv file according the if statement) | mail -s "mine" myemail@gmail.com
I know I can make it like that:
Code:
echo my files: $a | mail -s "mine" myemail@gmail.com
but then when the file is empty (it has one empty line) it prints that one empty line and thenit makes one line space which I don't want and makes me crazy.
I thought I could some how take the if command and make it as a variable, but I didn't manage, and I didn't find anywhere that it's possible. maybe someone has another suggestion?
 
Old 02-13-2020, 07:39 AM   #2
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
Why don't you put the echo ... | mail command in the if [ -s $a ]?

Alternatively, you can use echo $( any command pipeline ) | mail.

Last edited by berndbausch; 02-13-2020 at 07:40 AM.
 
Old 02-13-2020, 07:56 AM   #3
hatfani
Member
 
Registered: Oct 2019
Distribution: centos7
Posts: 52

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by berndbausch View Post
Why don't you put the echo ... | mail command in the if [ -s $a ]?

Alternatively, you can use echo $( any command pipeline ) | mail.
Because I have more things in the email that I want to add in the Echo command.
 
Old 02-13-2020, 08:12 AM   #4
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
Well, add these things inside the if...then branch. This way you are certain that the numbers file is not empty.
 
Old 02-13-2020, 09:10 AM   #5
hatfani
Member
 
Registered: Oct 2019
Distribution: centos7
Posts: 52

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by berndbausch View Post
Well, add these things inside the if...then branch. This way you are certain that the numbers file is not empty.
I don't know what is branch...? do you have any links for explanation?
 
Old 02-13-2020, 04:04 PM   #6
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
An if has a then branch and, optionally, an else branch.
Code:
if [ -s mynumbers.csv ]
then
  echo $(echo other text; cat mynumbers.csv) | mail hatfani
fi
Perhaps better:
Code:
if [ -s mynumbers.csv ]
then
  echo other text | cat - mynumbers.csv | mail hatfani
fi
or
Code:
if [ -s mynumbers.csv ]
then
  echo text1 > tempfile
  cat mynumbers.csv >> tempfile
  echo text2 >> tempfile
  mail hatfani < tempfile
fi
 
Old 02-16-2020, 02:28 AM   #7
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,792

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
A code block you can redirect from/to a file, and connect to a pipe.
Connect the if-fi block to a pipe:
Code:
if [ -s "$a" ]; then
  echo "My files:"
  cat "$a"
fi | mail ...
For demonstration - does not make good sense here.

Connect an explicit { block } to a pipe:
Code:
if [ -s "$a" ]; then
  {
  echo "My files:"
  cat "$a"
  } | mail ...
fi
Also a ( sub shell ) is a code block.
Variables defined in a sub shell are lost in the main shell.
If a { block } is connected to a pipe then the shell automatically converts it to a sub shell.

Perhaps you want to go for a { block } that contains the if-fi...

Last edited by MadeInGermany; 02-16-2020 at 02:33 AM.
 
  


Reply

Tags
csv, echo, mail, script



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Running mysql from ssh & query statement has a Text Header in the SELECT statement? djlerman Linux - Server 6 11-19-2013 06:33 PM
Perl switch statement throwing error like Bad case statement (invalid case value?) kavil Programming 2 10-07-2010 04:50 AM
[SOLVED] Shell script for adding a statement in a file after a particular statement Aquarius_Girl Programming 4 06-28-2010 03:07 AM
Problem with if statement in a find -exec statement romsieze Programming 2 10-02-2008 12:38 AM
Case statement with If statement cbo0485 Linux - Newbie 4 11-07-2007 08:05 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:55 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration