check if files exists
am 29.01.2008 18:10:56 von Charlie
I'm a newb to ksh and I've been trying to figure out how to use test
functions to check if a file exists in a directory. Here's an example
of my code:
if [[ -w *.usr ]]
then
(code to execute if any file with the .usr extension exists)
fi
Any help would be appreciated.
Thanks,
Charlie
Re: check if files exists
am 29.01.2008 18:18:02 von Stephane CHAZELAS
On Tue, 29 Jan 2008 09:10:56 -0800 (PST), Charlie wrote:
> I'm a newb to ksh and I've been trying to figure out how to use test
> functions to check if a file exists in a directory. Here's an example
> of my code:
>
> if [[ -w *.usr ]]
> then
> (code to execute if any file with the .usr extension exists)
> fi
[...]
set -- *.usr
if [ -e "$1" ]; then
echo "I found $# usr files: $*"
fi
(note that's the generic sense of "file", it could also be
directories, sockets, pipes...)
The code above is standard sh syntax. I'd recommend you write
scripts in that syntax. "ksh" is an interpreter of that syntax
like most sh on most systems.
ksh has a number of extensions on top of it including [[ ... ]],
but it varies from implementation to implementation and version
to version. So it's generally wiser to stick to the standard and
it's generally far enough for writing shell scripts.
--
Stephane
Re: check if files exists
am 29.01.2008 18:57:38 von Charlie
On Jan 29, 11:18=A0am, Stephane Chazelas
wrote:
> On Tue, 29 Jan 2008 09:10:56 -0800 (PST), Charlie wrote:
> > I'm a newb to ksh and I've been trying to figure out how to use test
> > functions to check if a file exists in a directory. =A0Here's an example=
> > of my code:
>
> > if [[ -w *.usr ]]
> > then
> > =A0 =A0(code to execute if any file with the .usr extension exists)
> > fi
>
> [...]
>
> set -- *.usr
> if [ -e "$1" ]; then
> =A0 echo "I found $# usr files: $*"
> fi
>
> (note that's the generic sense of "file", it could also be
> directories, sockets, pipes...)
>
> The code above is standard sh syntax. I'd recommend you write
> scripts in that syntax. "ksh" is an interpreter of that syntax
> like most sh on most systems.
>
> ksh has a number of extensions on top of it including [[ ... ]],
> but it varies from implementation to implementation and version
> to version. So it's generally wiser to stick to the standard and
> it's generally far enough for writing shell scripts.
>
> --
> Stephane
Stephane,
Thanks, that worked beautifully. I'll also heed your advice on
scripting. I appreciate it.
Charlie