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