如何在csh脚本中检查文件是否存在?

14

我正在使用csh脚本来检查任何文件是否存在。

if [ -f /var/opt/temip/conf/.temip_config ]

但是我遇到了以下错误

if [ -f /var/opt/temip/conf/.temip_config ]

if: Expression Syntax.

有人能告诉我如何做这件事吗?


这根本不是有效的Csh语法。这其实是一件好事,因为你应该不会在Csh中编写脚本。你的语法对于sh来说是可以的,而且这也是你应该使用的。但是,如果没有其他脚本的上下文,我们无法确定将第一行更改为#!/bin/sh是否可行。 - tripleee
@tripleee,你能告诉我如何在csh脚本中检查文件是否存在吗? - dcds
1
放弃 csh。在 POSIX sh(为了可移植性),GNU bash,Python或zsh中编写您的脚本。 - Basile Starynkevitch
2个回答

17

来自 manpage

f
    Plain file

你可以使用 if 语句来操作它:

if ( -f file.txt ) then
    echo Exists
else
    echo No such file
endif

根据这个问题和你之前的问题,看起来你对csh的语法工作原理一无所知,因为你一直在使用POSIX shell语法。我强烈建议你要么熟悉一下csh的语法,要么就使用POSIX shell(对于脚本编写来说可能更好)。


15
在CShell中,检查文件是否存在可以使用-e选项。文件名不必像if语句那样“硬编码”,而是可以像这样作为参数:
 if (-e "$filePath") then

这里是Cshell文件查询的完整列表。

-e file           file merely exists (may be protected from user)
-r file           file exists and is readable by user
-w file           file is writable by user
-x file           file is executable by user
-o file           file is owned by user
-z file           file has size 0
-f file           file is an ordinary file
-d file           file is a directory

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接