How can I make the for command check to see if a file is empty before executing?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How can I make the for command check to see if a file is empty before executing?
# 1  
Old 07-29-2006
How can I make the for command check to see if a file is empty before executing?

Here is the command in question
Code:
for f in $(<uploads); do

.

I only want this to execute if uploads is not empty. If uploads is empty I want the script to quit, actually before the for command. If its not apparent uploads is a text file.

Chris
# 2  
Old 07-29-2006
Use the 'test' command to check if the file has size.

For example, this will print each word in the file on a separate line.

Code:
if test -s uploads ; then for f in $(<uploads); do echo $f; done; fi

# 3  
Old 07-29-2006
Well this is my actual script
Code:
dir=/home/httpd/vhosts/chris*/web_users/chris2
email=info@domain.com
hours=6

let t=$hours*60
cd $dir

find . ! -name '.*'  -type f -cmin "-$t" > holder
cut -c 3-70 holder > uploads
for f in $(<uploads); do s=`echo ${PWD##*/}/$f`; z=`ls -la "$f" | awk '{ printf("%10d\n", $5)}'`; x=`ls -la "$f"| awk '{printf $6}'`;w=`ls -la "$f" | awk '{printf $7}'`;q=`ls -la "$f" | awk '{printf $8}'`;y=`md5sum "$f"| awk '{print $1}'`;
#echo -e "$s\t\t$z\t$x\t$y"
printf "%-30s%+12s %3s %-2s %-6s%-1s\n" $y $z $x $w $q $s  >> status
done
mail -s "Uploads!" -c "$email" susant <  status
cat status

rm -rf status
rm -rf holder
rm -rf uploads

How can I intergrate something like that before my for command?

Chris
# 4  
Old 07-29-2006
This should work. Don't miss the terminating 'fi' toward the bottom.

Code:
dir=/home/httpd/vhosts/chris*/web_users/chris2
email=info@domain.com
hours=6

let t=$hours*60
cd $dir

find . ! -name '.*'  -type f -cmin "-$t" > holder
cut -c 3-70 holder > uploads
if test -s uploads ; then
for f in $(<uploads); do s=`echo ${PWD##*/}/$f`; z=`ls -la "$f" | awk '{ printf("%10d\n", $5)}'`; x=`ls -la "$f"| awk '{printf $6}'`;w=`ls -la "$f" | awk '{printf $7}'`;q=`ls -la "$f" | awk '{printf $8}'`;y=`md5sum "$f"| awk '{print $1}'`;
#echo -e "$s\t\t$z\t$x\t$y"
printf "%-30s%+12s %3s %-2s %-6s%-1s\n" $y $z $x $w $q $s  >> status
done
mail -s "Uploads!" -c "$email" susant <  status
cat status

rm -rf status
fi
rm -rf holder
rm -rf uploads

Nathan
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

To check whether the file is empty or not in shell script (sh)

Hi All, I need to check a file whether it exists and also whether it is empty or not. I have a code for this but it is not working as per my requirement. Can anyone pls suggest me on this. function funcFLSanityCheck { if then echo "${varFLSentFileListPath}/${varFLSentFileName}... (7 Replies)
Discussion started by: Arun1992
7 Replies

2. Shell Programming and Scripting

Empty file check

Hi gurus , I have two files and i want to perform different action based on the condition if both or either is empty If then Do something elif then do something elif then do something else do something fi I have tried the below bt its not... (4 Replies)
Discussion started by: r_t_1601
4 Replies

3. UNIX for Dummies Questions & Answers

Check if file is empty with variables, bash

Hello again! I have some trouble with scripting in bash. In the following script I read from a folder with the files line0_Ux.xy line1_Ux.xy line2_Ux.xy . . . Some of the files are empty. For those I would like to print a "0" in list. I think the problem with the code is that... (4 Replies)
Discussion started by: bjoern456
4 Replies

4. Shell Programming and Scripting

error while executing make file

hi i am facing a strange error... whenever i try to execute make file it gives the following error bash-3.00$ make usa0300uz1128 sh: syntax error at line 1: `end of file' unexpected mksh: Fatal error: The command `if ; then echo "${COMMON}/src/rules/rules_11.2.0.3.2012Q3"; elif... (1 Reply)
Discussion started by: aishsimplesweet
1 Replies

5. Shell Programming and Scripting

Check if a text file is empty or not (using ls -s)

Hello, I want to make a script which says if a text file is empty or not. I tried two ways of making it, but I have problems with both of them. Now I think that the better way is the ls -s solution (considering that an empty text file has a 0 weight, because "cat file.txt" fails when file is... (4 Replies)
Discussion started by: Link_
4 Replies

6. Shell Programming and Scripting

check if file is empty

How do I check if a file is empty in a sh script I want to test in my shell script if the output file is empty and if it is do one thing and if it isnt empty do another? any ideas? (8 Replies)
Discussion started by: stolz
8 Replies

7. UNIX for Dummies Questions & Answers

to check if file is empty or not and return a non zero value

Hi All, I am new to unix worldd . I need to check a file1 if its empty or not. If its empty then return a non zero value say 99 could you pls let me know the perl script for this. (2 Replies)
Discussion started by: mavesum
2 Replies

8. UNIX for Dummies Questions & Answers

How to check if a file is empty?

Hi Masters..... I have problem !!! I need to check number of records in a file and if it is zero or file is empty i need to do some task. if ; then echo "File s empty" else echo "Not empty" fi so how to check this condition. I used wc -l < filename.txt => 1 for zero records same result... (1 Reply)
Discussion started by: shreekrishnagd
1 Replies

9. UNIX for Dummies Questions & Answers

How to check for empty file in Perl?

Hi, May I know how to check for empty file in Perl. Iam missing something, somewhere. #!/usr/bin/perl my $open_dir = '/path/'; my $file; my $ma = "abc_.*.\.psv\$" opendir(VAR, $open_dir) or die "Can't open $oepn_dir: $!\n"; while( defined ($file = readdir VAR) ) #read all... (1 Reply)
Discussion started by: deepakwins
1 Replies

10. UNIX for Dummies Questions & Answers

Check for Empty Command Argument

I have a script that when called can have 1 or 2 command arguments. If only 1 command argument is passed into the script how can I check that the second argument is null? I am working in Korn shell in a UNIX environment. Example of script call with 2 arguments: % statreport 0300 1430 ... (6 Replies)
Discussion started by: Nysif Steve
6 Replies
Login or Register to Ask a Question