Visit Our UNIX and Linux User Community


test file empty


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting test file empty
# 1  
Old 10-13-2009
test file empty

hi,

I test number on a file, but I have a problem when my file is empty, because the test isn't realized , do you have a idea to realize treatment when file is empty

my test script shell

VALUE=5000
for i in `cat /tmp/list | awk ' FS="|" { print $2 } '`
do
if [ "${i}" -eq "${VALUE}" ]
then
echo titi
else
echo toto
fi
done
# 2  
Old 10-13-2009
Code:
VALUE=5000
if [  -s /tmp/list ]
then
     for i in `cat /tmp/list | awk ' FS="|" { print $2 } '`
     do
        if [ "${i}" -eq  "${VALUE}" ]
        then
             echo titi
        else
              echo toto
        fi
    done 	
else
           echo "ERROR in file </tmp/list"
           exit 1
fi

# 3  
Old 10-13-2009
thanks

---------- Post updated at 05:54 AM ---------- Previous update was at 05:23 AM ----------

it's possible to analyse a non-null file, and a file with a line without character or information ?
# 4  
Old 10-13-2009
You can just use the appropriate "test" option like:
Code:
if [[ ! -s $file ]]; then
     echo "file does not exist or is size 0"
fi

From man page of "test"
Quote:
-s FILE
FILE exists and has a size greater than zero
If you need more precise things on regular expressions you could use grep, egrep or grep -E and check it's exit code ($?) or instead use awk or or ... whatever you want to do.

There are lots of other options that might come handy.
# 5  
Old 10-13-2009
i'm not sure understand you

Code:
name_file="the_nameOfFile"

if [ -f ${name_file} ]
then
     echo "${name_file}: exist"
     if [ -s ${name_file} ]
     then
           echo "${name_file}:exist and is not empy"
     else
           echo "${name_file}:exist and is empty"
     fi
else
           echo "${name_file}:NOT exist"
fi

use:
Code:
    man test

to see all the options for the conditional sentencies for files.
# 6  
Old 10-13-2009
sorry, i 'm not very precise

if
>cat /tmp/list
200O|1000

> cat test_i.sh
VALUE=5000
for i in `cat /tmp/list | awk ' FS="|" { print $2 } '`
do
if [ "${i}" -eq "${VALUE}" ]
then
echo titi
else
echo toto
fi
done

If i execute test_i.sh

> ksh -x test_i.sh
+ alias ll=/usr/bin/ls -l
+ VALUE=5000
+ cat /tmp/list
+ awk FS="|" { print $2 }
+ [ 1000 -eq 5000 ]
+ echo toto
toto

If i empty file /tmp/list

>cat /tmp/list
>ksh -x test_i.sh
+ alias ll=/usr/bin/ls -l
+ VALUE=5000
+ cat /tmp/list
+ awk FS="|" { print $2 }

My need --> I would like that script make echo toto if i have anything on my file /tmp/list


thanks you for your help
# 7  
Old 10-13-2009
hope this help you:

Code:
VALUE=5000
file=/tmp/list

#validate if file exists and is not empty file
if [ -s ${file} ]
then
   for i in `cat ${file}| awk ' FS="|" { print $2 } '`
   do
    if [ "${i}" -eq "${VALUE}" ]
    then
      echo titi
    else
      echo toto
    fi
   done
else
   #this means: the file not existe or is empty
    echo toto
fi


Previous Thread | Next Thread
Test Your Knowledge in Computers #635
Difficulty: Medium
The iPhone 6 and 6S were available in three sizes at launch: 32, 64, and 128 GB.
True or False?

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

How to check weather a string is like test* or test* ot *test* in if condition

How to check weather a string is like test* or test* ot *test* in if condition (5 Replies)
Discussion started by: johnjerome
5 Replies

3. Shell Programming and Scripting

Test on string containing spacewhile test 1 -eq 1 do read a $a if test $a = quitC then break fi d

This is the code: while test 1 -eq 1 do read a $a if test $a = stop then break fi done I read a command on every loop an execute it. I check if the string equals the word stop to end the loop,but it say that I gave too many arguments to test. For example echo hello. Now the... (1 Reply)
Discussion started by: Max89
1 Replies

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

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

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

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

8. UNIX for Dummies Questions & Answers

What's wrong with this line: if ${TEST:?} ; then echo empty; fi

I want to print "empty" if $TEST is empty. TEST= if ${TEST:?} ; then echo empty; fi But it doesn't work. What's wrong? (2 Replies)
Discussion started by: meili100
2 Replies

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

10. Shell Programming and Scripting

if test for empty and non-existing file

How to write this condition in ksh? if myfile is empty or myfile does not exist then do action1 fi is this OK? if ] -o ] then then do action1 fi Thanks. (3 Replies)
Discussion started by: GNMIKE
3 Replies

Featured Tech Videos