1843947 Members
2401 Online
110226 Solutions
New Discussion

script help

 
SOLVED
Go to solution
Anthony khan
Frequent Advisor

script help

Hi All, I am having a problem in if statement, here's what i have....
filecheck=`find . -name "test*.txt"`

if [ $filecheck -eq 0 ]
then
exit
fi

What I am trying to get is if $filechekc is empty then script exit else it continue, but i am getting the following error

./test[4]: test: argument expected

Can someone tell me what i'm doing wrong

9 REPLIES 9
Carlos Fernandez Riera
Honored Contributor

Re: script help

filecheck=`find . -name "test*.txt"`

if [ $filecheck -eq 0 ]
then
exit
fi


If find does not find anything $filecheck will remaing empty so ... two ways:

if [ "$filecheck" -ne "" ] ...

or better
if [ -n "$filecheck" ]....

See man test.
unsupported
S.K. Chan
Honored Contributor

Re: script help

The test statement in if is not quite right. It should be ..

if [ $filecheck = "" ]
...

which test for empty strings. The one that you use test for numbers.
Paula J Frazer-Campbell
Honored Contributor

Re: script help

Hi

Another way:-

--------------------------------
cd

counter=`ll -R | grep test.file | wc -l`
if [[ $counter = 0 ]]
then

exit


fi
-----------------------------------


HTH

Paula
If you can spell SysAdmin then you is one - anon
Mark van Hassel
Respected Contributor

Re: script help

Anthony,

Another option is to add a line count:

filecheck=`find . -name "test*.txt" | wc -l`

This way you don't have to change your if statement.

HtH,

Mark
The surest sign that life exists elsewhere in the universe is that none of it has tried to contact us
Corthouts Carlo
Valued Contributor

Re: script help

Hi,

The problem you are seeing is that you are comparing a null value with zero which is not possible.

A better way to go about it is doing the following.

if [ $filecheck ]
then
echo "a value exists"
else
echo "nothing"
fi

This way if $filecheck is empty it is false if some value is moved to $filecheck it is always true.

David Diaz
Occasional Advisor

Re: script help

Make sure you export the filecheckc variable.
Also use the "=" instead of "-eq". Scripts act a little funny with the alpha characters instead of the symbols when using numbers.
Hope this helps...

David
At the end of the game the king and the pawn go in the same box
Bill Hassell
Honored Contributor
Solution

Re: script help

You can simplify this a bit:

filecheck=$(find . -name "test*.txt")
[[ "$filecheck" = "" ]] && exit

Note the use of $( ) which is preferred over the archaic form: ` ` (see man sh-posix and man ksh).


Bill Hassell, sysadmin
Rui Soares
Valued Contributor

Re: script help

Anthony,

It doesn't work because you're trying to compare a string with a number (and probably an empty string by the look of the error message).

So I suggest that you add:

| wc -w

To the find command, so it spits out the number of ocurrences found (as it is now, it will only collect the files found).
Afterwards, you can compare numbers to numbers.
Just in case you get nothing from the variable, you should also prefix the $filecheck with a zero, so it has always a value:

if [ 0$filecheck -eq 0 ]

Good luck!

Rumagoso
Be Well
V. V. Ravi Kumar_1
Respected Contributor

Re: script help

hi

try this



if [ $? -eq 0 ]
then

else
do this
fi

regds
ravi
Never Say No