【问题标题】:how to check existence of any file in csh script?如何检查 csh 脚本中是否存在任何文件?
【发布时间】:2015-03-05 01:42:15
【问题描述】:

为了检查我正在使用的 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 你能告诉我如何在 csh 脚本中检查文件是否存在吗?
  • 放弃csh。用 POSIX sh(为了可移植性)或 GNU bash 或 Python 或 zsh 编写脚本

标签: linux csh


【解决方案1】:

来自the manpage

f
    Plain file

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

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

基于这个问题and your previous question,您似乎对csh 语法的工作原理一无所知,因为您一直使用POSIX shell 语法。我会强烈建议您要么熟悉 csh 语法,要么只使用 POSIX shell(无论如何这可能更适合编写脚本)。

【讨论】:

    【解决方案2】:

    在 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-04-13
      • 1970-01-01
      • 2013-07-08
      • 2015-10-18
      • 2018-12-08
      • 2018-11-13
      • 2018-11-17
      • 2012-06-04
      相关资源
      最近更新 更多