简体   繁体   English

if else语句错误检查

[英]if else statement error checking

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. 我试图进行错误检查,以便如果该文件不是普通文件,它将回显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 当我使用例如fake.txt运行它时

du: cannot access fake.txt': No such file or directory stat: cannot stat fake.txt': No such file or directory stat: cannot stat `fake.txt': No such file or directory date: fake.txt: No such file or directory du:无法访问fake.txt': No such file or directory stat: cannot stat fake.txt':没有这样的文件或目录统计:无法stat`fake.txt':没有这样的文件或目录日期:fake.txt:否这样的文件或目录

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

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. 有关test(1)如何工作的详细信息,请参见相应的手册页。 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. 在此示例中,我简单地使用了一个事实,即如果未满足使用的测试条件,则test(1)会以非零退出代码退出:switch -f测试给定的参数是否存在并且是常规文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM