Help in Shell script to report size of files=0


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help in Shell script to report size of files=0
# 1  
Old 06-06-2013
Help in Shell script to report size of files=0

Hello, I am trying to write a shell script in unix which looks for the last two files in a folder, checks their size and reports(via mail) if either of their file size is =0.

Code:
cd /tmp
list last two files: ls -ltr | tail -2
check if size of any of these two files is 0
if true, then send a mail(can use mailx I suppose) else exit

could you please suggest me the ways of achieving this as I am very new to shell scripting..Thanks in advance!

Kiran.
# 2  
Old 06-06-2013
Code:
#! /bin/bash

for file in $(ls -rt | tail -2)
do
    if [ ! -s $file ]
    then
        echo " $file has size 0 ;-) " | mailx -s "This is subject" mailid@domain.com
    fi
done

This User Gave Thanks to balajesuri For This Post:
# 3  
Old 06-06-2013
Anseer for query

Hello,

Could you please use the following code.
please give your mail address as well as PATH will be the path where you want to test the files.

Please let me know in case you have any queries.

Also please don't place the script within same directory.
Code:
cat test.ksh
PATH=/home/test_user/bin/singh_testing
cd $PATH
 
zero=0
# zero shows variable here.
 
a=`ls -ltr | tail -2 | awk '{print$5}'`
echo $a
set -A value ${a}
 
for i in ${value[@]}
do
if [[ ${i} -eq ${zero} ]]
then
echo "Non zero file found."
mailx -s "file size found NON 0 please check" Ravinder_Singh1@test.com
fi
done
echo "Script done"



ksh test.ksh

Thanks,
R. Singh

Last edited by Franklin52; 06-06-2013 at 03:03 AM.. Reason: Please use code tags
# 4  
Old 06-06-2013
Thankyou for the responses..I have used the following and it is working now.
However, is there a way to include checking the same criteria in a different directory as well ?

present code:
==========
Code:
#!/bin/ksh
typeset SERVER=$(hostname)
MAIL="kiran@test.com"
LOGDIR=/tmp/log1/
cd $LOGDIR
for file in $(ls -rt | tail -2)
do
    if [ ! -s $file ]
    then
        echo " $file has size 0 under $LOGDIR" | mailx -s "Error in executing script on $SERVER" $MAIL
    fi
done

===========
As you can see, presently I am checking in /tmp/log1..can I also add to check for this condition in /tmp/log2 as well ?

Thanks.

Last edited by Franklin52; 06-06-2013 at 03:03 AM.. Reason: Please use code tags
# 5  
Old 06-06-2013
Use nested for-loops.
# 6  
Old 06-06-2013
Reply

Kindly try to use the following code.
Code:
#!/bin/ksh
typeset SERVER=$(hostname)
MAIL=kiran@test.com

set -A LOGDIR
 
LOGDIR[0]=/tmp/log1/
LOGDIR[1]=/tmp/log2
 
cd $LOGDIR

for j in LOGDIR[@]
do
cd $j
 
for file in $(ls -rt | tail -2)
do
if [ ! -s $file ]
then
echo " $file has size 0 under $LOGDIR" | mailx -s "Error in executing script on $SERVER" $MAIL
fi
done

done




Thanks,
R. Singh

Last edited by Franklin52; 06-06-2013 at 03:04 AM.. Reason: Please use code tags
This User Gave Thanks to RavinderSingh13 For This Post:
# 7  
Old 06-06-2013
Thanks to you all..used nested for loops and it is working now. Smilie
Will test it and comeback if any further modifications are required Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script to report file size, pid and also kill the process

Hi All, Looking for a quick LINUX shell script which can continuously monitors the flle size, report the process which is creating a file greater than certain limit and also kill that process. Can someone please help me on this? (4 Replies)
Discussion started by: vasavimacherla
4 Replies

2. Shell Programming and Scripting

shell script for getting the file size

Hi can some one please help me how i can get the output i require: My text file "sample.txt" contains the text like below Filesystem Size Used Avail Use% Mounted on /dev/mapper/vg_fedora-lv_root 15G 2.6G 12G 19% /hari Filesystem Size ... (3 Replies)
Discussion started by: harimhkr
3 Replies

3. Shell Programming and Scripting

Shell script to calculate the size of files

Dear all, Please help me to write a script that can calculate the size of files. For example: I have a directory which contain thousands of files. I need to know the size of files that their name begin with abc_123 Thank all!! (4 Replies)
Discussion started by: hainguyen1402
4 Replies

4. Shell Programming and Scripting

Shell script that will compare two config files and produce 2 outputs 1)actual config file 2)report

Hi I am new to shell scripting. There is a requirement to write a shell script to meet follwing needs.Prompt reply shall be highly appreciated. script that will compare two config files and produce 2 outputs - actual config file and a report indicating changes made. OS :Susi linux ver 10.3. ... (4 Replies)
Discussion started by: muraliinfy04
4 Replies

5. Homework & Coursework Questions

shell script that can create, monitor the log files and report the issues for matching pattern

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Write an automated shell program(s) that can create, monitor the log files and report the issues for matching... (0 Replies)
Discussion started by: itian2010
0 Replies

6. UNIX for Dummies Questions & Answers

Unix shell script for finding top ten files of maximum size

I need to write a Unix shell script which will list top 10 files in a directory tree on basis of size. i.e. first file should be the biggest in the whole directory and all its sub directories. Please suggest any ideas (10 Replies)
Discussion started by: abhilashnair
10 Replies

7. UNIX for Dummies Questions & Answers

shell script to find files by date and size

Hi, I have a directory PRIVATE in which I have several directories and each of these have several files. Therefore, I need to find those files by size and date to back up those files in another directory. I don't know how to implement this shell script using ''find''. appreciate any... (1 Reply)
Discussion started by: dadadc
1 Replies

8. Shell Programming and Scripting

bash script working for small size files but not for big size files.

Hi, I have one file stat. Stat file contents are as follows: for example. H50768020040913,00260100,507680,13,0000000643,0000000643,00000,0000 H50769520040808,00260100,507695,13,0000000000,0000000000,00000,0000 H50770620040611,00260100,507706,13,0000000000,0000000000,00000,0000 Now i... (1 Reply)
Discussion started by: davidpreml
1 Replies

9. Shell Programming and Scripting

Size of an array in sh shell script

Is there a way to find out the size of an array in sh shell script? Thanks. (1 Reply)
Discussion started by: trivektor
1 Replies

10. Shell Programming and Scripting

Get file size in c shell script?

Hi, I want to use an 'if statement' that will check if a certian file is greater in size than a certain value given by the user, but cannot get it to work. Do you have any ideas how this can be done? Your help is appreciated! (6 Replies)
Discussion started by: Dado
6 Replies
Login or Register to Ask a Question