if/else with an empty file...


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting if/else with an empty file...
# 1  
Old 10-28-2012
if/else with an empty file...

Hi there,

I'm just starting out with shell scripting and I'm trying to make a test where the script will basically run yum check-update to find out if the server has any available and if it does it e-mails me but if not it will just stop there.

I have it working if there are actually updates to be applied it works well. However, when I'm checking to see if the file is empty it seems to be sending out the e-mail anyway. The script I'm using is:
Code:
#!/bin/bash
/usr/bin/yum check-update | tail -n +3 > newUpdates.txt

SUBJECT="$HOSTNAME needs to be updated"
EMAIL="someemail@domain.com"
EMAILMSG="/root/newUpdates.txt"

if test "$EMAILMSG" = null
then
        rm -rf /root/newUpdates.txt
        exit
else
        /bin/mail -s "$SUBJECT" "$EMAIL" < $EMAILMSG
        rm -rf /root/newUpdates.txt
fi

So I was just running the command and then trying to strip out the first few lines that I'm sending to my temp file because it will have stuff like "Connecting to RedHat Network, etc, etc" and then if it's blank I just want to exit, but if not it should just list the updates.

Can anyone give me some guidance here??
# 2  
Old 10-28-2012
Replace
Code:
if test "$EMAILMSG" = null

with
Code:
if [[ ! -s $EMAILMSG ]]

.
# 3  
Old 10-28-2012
okay, that worked well. I'll have to test it on another box that has some updates because I updated my test machine to test the 2nd half. Smilie

Can you explain the syntax there for me? Originally I was trying something kind of like that within a single set of [] but what exactly is that one doing?
# 4  
Old 10-28-2012
[[ ]] or [ ] act like "words" in shell. The word they almost exactly represent is "test".

So if you look up test documentation you find a lot of stuff you can test for.

Code:
 [  -s  /path/to/somefile ]

is the equivalent to
Code:
 test -s /path/to/somefile

and -s means 'Does the file exist and is the file size greater than zero (not empty)?'
! in front of the -s reverses the meaning because ! is the "not" operator.

Check out the other -[options] when you get a chance.
# 5  
Old 10-28-2012
fantastic! thanks.

---------- Post updated at 02:08 PM ---------- Previous update was at 11:38 AM ----------

One last question...

what about if I wanted to look for a specific line of text in the output file. For example, if it said "No updates available" then exit but if not, run the mail command. Is that possible with something like grep -a ?
# 6  
Old 10-28-2012
You can do something like below:-

Code:
if [ `grep -i "no updates available" <output file> | wc -l` -eq 0 ]
then
      exit
else
      # Send Mail
fi

# 7  
Old 10-28-2012
One could leave out test, backticks, pipe and count:
Code:
if grep -q "No updates available" outfile
then
  exit
else
  echo "send mail"
fi

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to reformat output if input file is empty, but not if file has data in it

The below awk improved bu @MadeInGermany, works great as long as the input file has data in it in the below format: input chrX 25031028 25031925 chrX:25031028-25031925 ARX 631 18 chrX 25031028 25031925 chrX:25031028-25031925 ARX 632 14... (3 Replies)
Discussion started by: cmccabe
3 Replies

2. Shell Programming and Scripting

Empty out the file

Hi All, I have a piece of perl code in which I DON'T want to delete a file rather empty out the contents, here is the code - if ( unlink("$opt_b") == 1 ) { print_log( "$opt_b deleted", 1 ); }else { print_log( "Could not delete $opt_b:$!", 1 ); ... (5 Replies)
Discussion started by: jacki
5 Replies

3. Shell Programming and Scripting

while file is empty

how can i use while loop ? while file is empty do.... if not empty do ..... in bash (1 Reply)
Discussion started by: Trump
1 Replies

4. UNIX for Dummies Questions & Answers

Getting same exit status for empty and non empty file

Hi All, I am checking for a empty input file to do some further action , but I am getting exit status 0 in both the cases , for empty and non empty file both. The value of $? is coming 0 in if part also and else part too. #!/bin/ksh if ]; then echo "data" # exit 0 echo "$?" else... (4 Replies)
Discussion started by: mavesum
4 Replies

5. Shell Programming and Scripting

how to see if the file is empty or not?

hi how can I determine, if a file is empty or not?I am using read line clause. The script should be like: while read line do if(file is empty) then; ...... done < $blacklist (1 Reply)
Discussion started by: tjay83
1 Replies

6. UNIX for Dummies Questions & Answers

Trying to empty file using > but the file size increasing when next append

AIX 5.3 / KSH I have a Java application which creates a log file a.log. I have a KSH script which does the following action cp a.log /directory2/b.log > a.log After this the file size goes to 0 as per "ls -l" Then next time when the application writes into this file, the file size... (4 Replies)
Discussion started by: firdousamir
4 Replies

7. UNIX for Dummies Questions & Answers

how to find empty folders without using -empty

hi all: my solaris FIND does not support find myFolder -type d -empty how can i find the empty folders? thanks! (7 Replies)
Discussion started by: lasse
7 Replies

8. Shell Programming and Scripting

File is not empty?

How do I check to make sure a file is not zero bytes? Something like: if then ????? (7 Replies)
Discussion started by: lesstjm
7 Replies

9. Shell Programming and Scripting

How to empty a file(already created)

What is the command to empty an already existing file. please provide me.i used Touch cmd to empty the file.but it changing the time only. (4 Replies)
Discussion started by: laknar
4 Replies

10. UNIX for Advanced & Expert Users

empty file in hp-ux

Hi, I need your help. How can I create an empty filename with a specific size, in hp-ux? Regards, Mizi (2 Replies)
Discussion started by: Mizi
2 Replies
Login or Register to Ask a Question