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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting To check whether the file is empty or not in shell script (sh)
# 1  
Old 12-31-2015
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.
Code:
function funcFLSanityCheck
{
	
	if [ ! -s $varFLSentFileListPath/$varFLSentFileName ]
	then
		echo "${varFLSentFileListPath}/${varFLSentFileName} either not available or is a empty file"	
		exit 20
	fi
	
}

Note: I have written the script in sh (/bin/sh)

Thanks in Advance n Happie New Year to All..

Last edited by Arun1992; 12-31-2015 at 03:08 PM.. Reason: forgot to mention some points
# 2  
Old 12-31-2015
What exactly is not working?
# 3  
Old 12-31-2015
If I run the commands separately all the commands are giving me the correct ouput.. But if I run it inside the script it is showing as " / either not available or is a empty file " as output.. But I am having a file in the path and also the file is not empty..
# 4  
Old 12-31-2015
Are you sure those variables are set?
# 5  
Old 12-31-2015
Yes both the variables are set
# 6  
Old 12-31-2015
Quote:
Originally Posted by Arun1992
If I run the commands separately all the commands are giving me the correct ouput.. But if I run it inside the script it is showing as " / either not available or is a empty file " as output.. But I am having a file in the path and also the file is not empty..
Using set -x just after the shebang (#!/bin/sh) will show output for every line executed. That might help you to debug.
# 7  
Old 12-31-2015
I agree with RudiC and Aia; it looks very much like the variables being used in your function are not defined in the environment in which the function is being run.

What operating system are you using? /bin/sh on some systems is a true Bourne shell (that handles function definitions differently) than bash, ksh, and zsh that are installed as /bin/sh on some other systems.

And, how are you invoking your function?

We look forward to seeing the output you get when you run your script with tracing enabled as Aia suggested.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Web Development

Javascript to check field is empty then execute rest of script

I have found this bit of code that nearly does what I want. Basically 3 input fields, I want to copy t2 to t3 as it's typed but only if t1 contains data AND t3 is empty: <input type="text" id="t1" /> <input type="text" id="t2" /> <input type="text" id="t3" /> <script> var t2 =... (4 Replies)
Discussion started by: barrydocks
4 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. Shell Programming and Scripting

perl script to check if empty files are created and delete them and run a shell script

I have a local linux machine in which the files are dumped by a remote ubuntu server. If the process in remote server has any problem then empty files are created in local machine. Is there any way using perl script to check if the empty files are being created and delete them and then run a shell... (2 Replies)
Discussion started by: hussa1n
2 Replies

4. 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

5. Shell Programming and Scripting

Need to check for empty file in C shell script

I am running a C shell script. I have an output file from a previous step and I need to run "something" in the next step to check if the file is empty. If the file is empty, then the job script should finish EOJ. If the file is not empty then the job script should abend. Please help Thanks. (4 Replies)
Discussion started by: jclanc8
4 Replies

6. Shell Programming and Scripting

Bash-Shell: If-Clause to check if file is empty

Hello, I want to checkl whether my file has text in it or not. if ; then ... if ; then ... But none of these work Can someone help me? ---------- Post updated at 09:00 AM ---------- Previous update was at 08:55 AM ---------- The code-tags caused an displayerror,... (5 Replies)
Discussion started by: ABE2202
5 Replies

7. 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

8. 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

9. 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

10. 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
Login or Register to Ask a Question