Check content of file

Hi All,

I m very new to unix...i jus want to chk the content of file.

ma requirement is
if file has a content then
display it
else
dont display or something

pls specify which loop shalli use either for or while??

You mean if file is empty, then don't display it's contents? :smiley:

ya some think like that:D

Then:

cat file

i need a script not comands!!!!!!!!! :stuck_out_tongue:

What do you think scripts are made of?

echo cat file > filename
chmod +x ./filename
./filename
if [[ ! -s $file ]];
then
   cat $file
else
      echo "Empty or not exist"
fi

read the below url and check for -s

File test operators

thx fr writin......
where to give the file name in ur code??

-s : is file test operator. Replace with the test operator you need.
filename : is file name. Replace with your file name.
if [ -s filename ]
then
      # Perform your action here.
fi
if [[ ! -s TCM_HC_ERR_Report.txt ]]
then
cat TCM_HC_ERR_Report.txt
else
echo "Empty or not exist"
fi

this s ma code ...its compiling but no output.......

Please use CODE TAGS when posting code fragments.

Compiling? Not sure what you are talking about. I think below article will help you:-

Creating and Running a Script

please read man test

If you have the file "TCM_HC_ERR_Report.txt" in the same directory which is the script, it must work, on the other hand...you should declare a variable, for example "file" and to assign to this the name of file. It's better to assign the whole path of file, for example, /home/xxx/xxx/TCM_HC_ERR_Report.txt.

Does the output send any kind of error? :confused::confused:

buddy
if i gave the code like this

if [ [ ! -s TCM_HC_ERR_Report.txt ] 
then
cat TCM_HC_ERR_Report.txt
else
echo "Empty or not exist"
fi

its throwing error<< test: unknown operator !>>>

if i gave the code like this

if [[ ! -s TCM_HC_ERR_Report.txt ]]
then
cat TCM_HC_ERR_Report.txt
else
echo "Empty or not exist"
fi

i m getting error like this..
cool.sh: [[: not found
Empty or not exist
but i have that file in that dirrectory....
pls help

If you are using sh you have to use:

if [ ! -s "TCM_HC_ERR_Report.txt" ]  
then    
   cat TCM_HC_ERR_Report.txt 
else    
    echo "Empty or not exist" 
fi

thx fr quick reply...
My file(TCM_HC_ERR_Report.txt) has content but the output is showing 'Empty or not exist"

Yes, you have to change the condition.

[ ! -s "TCM_HC_ERR_Report.txt" ] && echo "file empty" || cat TCM_HC_ERR_Report.txt

Replace

if [ ! -s "TCM_HC_ERR_Report.txt" ]  
then    
   cat TCM_HC_ERR_Report.txt 
else    
    echo "Empty or not exist" 
fi

with

if [ -s TCM_HC_ERR_Report.txt ]  
then    
   cat TCM_HC_ERR_Report.txt 
else    
    echo Empty or not exist 
fi

.
The double-quotes aren't necessary when you are having a constant file name. However, these may be required if the file name has spaces,new-lines,etc.
This is assuming that you have a file by that name in the directory which is the working directory just before the test.

1 Like

Also, give the absolute path (from the root) for the file.

1 Like