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

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

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 ?

You can just use the appropriate "test" option like:

if [[ ! -s $file ]]; then
     echo "file does not exist or is size 0"
fi

From man page of "test"

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.

i'm not sure understand you

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:

    man test
 

to see all the options for the conditional sentencies for files.

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

hope this help you:

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

Hi, i test but i have same problem

> cat /tmp/list

> cat test_w.sh
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
> ksh test_w.sh

To keep the forums high quality for all users, please take the time to format your posts correctly.

First of all, use Code Tags when you post any code or data samples so others can easily read your code. You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags

```text
 and 
```

by hand.)

Second, avoid adding color or different fonts and font size to your posts. Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.

Third, be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.

Thank You.

The UNIX and Linux Forums

Hi, i realized test and i have same problem

> cat /tmp/list

> cat test_w.sh
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
> ksh test_w.sh
>

are you sure the size of file is 0,can you paste the exit of:

ls -l /tmp/list

yes, it's not empty

ls -al /tmp/list
-rw-r--r-- 1 root system 1 Oct 13 14:32 /tmp/list

cat /tmp/list

there are a blank ( white ) line

is it possible to add a condition on the "if" ? ( as [ "a$i" -ne "a" ] ??

VALUE=5000
file=/tmp/list

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

thanks you