Verify File exists and execute command

Hi,

I am trying to verify that a file exists within an alternate directory. If the file exists, it will execute a copy command...if it does not, it should exit the script.

I tried the <test> command and the [ -f $filename] but keep coming up with syntax errors.

I am coding in C Shell and the file name is actually a $variable.

basically, I have:

test -f /alt/dir/$filename
 
if ($#status == 0) then <cp...>
else echo "does not exist"
endif
exit

Thanks!

if [ -f yourfile ]
then
cp
else
echo "does not exist"
fi

or

test "$file"!='filename' && (echo does not exist; exit 1)

Try with $status instead of $#status .

Or directly:

if ( -f "/alt/dir/$filename" ) ...

Check this article: Csh Programming Considered Harmful

I tried those options, but when I use the [ -f $filename ] - I get a 'Missing ]' error and when I use the

if (test -f $filename) - I get a 'if Syntax error'.

I use the $status method...but it doesn't do the 'cp' command.

I don't know where the problem is...

I wrote:

if ( -f "/alt/dir/$filename" )

not

if ( test -f "/alt/dir/$filename" )

Thanks for the advice - job works!