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

时间:2022-08-16 09:18:09

For checking the existence of any file in csh script I am using

用于检查我正在使用的csh脚本中是否存在任何文件

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

but I am getting below error

但我得到了以下错误

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

if: Expression Syntax.

Can anyone tell me how to do this?

谁能告诉我怎么做?

2 个解决方案

#1


5  

From the manpage:

从联机帮助页:

f
    Plain file

You use it with a if statement:

您将它与if语句一起使用:

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

Based on this question and your previous question, you seem to be rather clueless as to how csh syntax works, since you keep using POSIX shell syntax. I would strongly suggest that you either familiarise yourself with the csh syntax, or just use a POSIX shell (which is probably better for scripting anyway).

基于这个问题和您之前的问题,您似乎对csh语法的工作原理一无所知,因为您继续使用POSIX shell语法。我强烈建议你要么熟悉csh语法,要么只使用POSIX shell(这对于脚本来说可能更好)。

#2


4  

In CShell for checking of the existence of a file use -e option

在CShell中检查文件是否存在-e选项

The name of the file does not have to be "hard coded" into the if statement, but may be a parameter like this:

文件的名称不必“硬编码”到if语句中,但可以是这样的参数:

 if (-e "$filePath") then

Here is a full list of the Cshell file queries.

以下是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

#1


5  

From the manpage:

从联机帮助页:

f
    Plain file

You use it with a if statement:

您将它与if语句一起使用:

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

Based on this question and your previous question, you seem to be rather clueless as to how csh syntax works, since you keep using POSIX shell syntax. I would strongly suggest that you either familiarise yourself with the csh syntax, or just use a POSIX shell (which is probably better for scripting anyway).

基于这个问题和您之前的问题,您似乎对csh语法的工作原理一无所知,因为您继续使用POSIX shell语法。我强烈建议你要么熟悉csh语法,要么只使用POSIX shell(这对于脚本来说可能更好)。

#2


4  

In CShell for checking of the existence of a file use -e option

在CShell中检查文件是否存在-e选项

The name of the file does not have to be "hard coded" into the if statement, but may be a parameter like this:

文件的名称不必“硬编码”到if语句中,但可以是这样的参数:

 if (-e "$filePath") then

Here is a full list of the Cshell file queries.

以下是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