LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   ksh check if file exists (using wildcard) problem (https://www.linuxquestions.org/questions/linux-newbie-8/ksh-check-if-file-exists-using-wildcard-problem-156350/)

r18044 03-11-2004 12:02 PM

ksh check if file exists (using wildcard) problem
 
Within my ksh script I am executing the following:

if [ -f *.pnr.* ]
then
.....
fi

if there exists one and only one file that satisifies this condition, the script runs successfully.

if there exists more than one file that satisifies this condition, I receive the following message:

./pnr.ksh[190]: [: /home/pnr/2004.03.10.1000.pnr.ready: unexpected operator/operand

The files that exist in the sub-directory include:

/home/pnr/2004.03.10.1000.pnr.file1
/home/pnr/2004.03.10.1000.pnr.ready

This script works on a solaris box but I'm not sure why it does not work on linux.

Any help is appreciated.

Thanks.

bigearsbilly 03-12-2004 07:41 AM

hmm. remember it's usually pdksh on linux rather than the full blown one.
perhaps the 'test' is a bit different on linux.

do:

Code:

echo "*.pnr.*
if [ -f *.pnr.* ]
then
.....
fi

so we can see what the test is evaluating.



billy

sanojca 02-22-2005 04:54 AM

I've the same problem under Suse Linux Enterprise Server 8 for a script which works on sun.

> echo "*.env"
*.env
> if [ -f *.env ]
2> then
2> echo ja
2> fi
ksh: [: APPSORA.env: unexpected operator/operand

By the way the filename APPSORA.env one file matching to the pattern *.env.

What is wrong with that code which run under sun solaris?

Werner

bigearsbilly 02-22-2005 05:01 AM

weird.
Yes it worked on solaris.

???
try quotes around "*.env" ?

sanojca 02-22-2005 05:08 AM

> if [ -f "*.env" ]
2> then
2> echo yes
2> else
2> echo no
2> fi
no

I tried with quotes. Then it echos "no" even though there exists file in the directory.

Werner

sanojca 02-22-2005 07:52 AM

For my actual migrated code I quit using test operator with wildcards (unless someone has an idea or explanation). I use the workaround:

if (ls *.env >/dev/null)
then
..
fi

Werner


All times are GMT -5. The time now is 11:45 PM.