0

I am attempting to error check so that if the file is not an ordinary file it will echo File is not an ordinary file.

#!/bin/csh

echo Enter file name
set filename = $<

if(-f $filename)then

        if(-z $filename)then
        rm $filename    

        else
        clear
        echo $filename
        du -sh $filename
        stat -c %h $filename
        stat -c %U $filename
        date -r $filename

        endif
else
echo File is not an ordinary file
endif

when I run this with for example fake.txt it comes up with

du: cannot access fake.txt': No such file or directory stat: cannot statfake.txt': No such file or directory stat: cannot stat `fake.txt': No such file or directory date: fake.txt: No such file or directory

what am I missing?

REDO:

#!/bin/csh

echo Enter file name
set filename = $<

endif
test -f $filename

if ( $status == 0 ) then
        if(-z $filename)then
        rm $filename

        else
        clear
        echo $filename
        du -sh $filename
        stat -c %h $filename
        stat -c %U $filenam
        endif
else
  echo "File is not an ordinary file."
endif
Josamoda
  • 77
  • 1
  • 13

1 Answers1

0

There is a much easier way to determine if a file is a regular file (and exists):

#!/bin/csh

echo "Enter filename: "
set filename = $<
test -f $filename

if ( $status == 0 ) then
  echo "File is an ordinary file."
else
  echo "File is not an ordinary file."
endif

# end of file.

Details on how test(1) works can be found from an appropriate manual page. In this example I've simply used the fact that test(1) exits with a non-zero exit code in case the test condition used is not fulfilled: switch -f tests that the argument given exists and is a regular file.

  • There are some typos in your redo, for example you're referring to $filenam instead of $filename. Also the -z $filename test is redundant, there is no need for that (-z tests wether or not the variable is zero length and surely if it indeed is such, there is no need for you to delete a file which doesn't have a name). –  Nov 13 '13 at 08:45