【问题标题】:How can I make a bash script to tell if files in a give folder are symlinks or a normal files?如何制作一个 bash 脚本来判断给定文件夹中的文件是符号链接还是普通文件?
【发布时间】:2014-05-15 13:24:15
【问题描述】:

在过去的 5 个小时里,我一直在寻找一个错误,其中符号链接文件被重新标记为普通文件(我不知道怎么做!)。我想在我的构建脚本中添加一个验证步骤,以确保不会再次发生这种情况。

在工作环境中我得到这个:

> ls -l
... [Info] ...  File1.h -> ../../a/b/File1.h 
... [Info] ...  File2.h -> ../../a/b/File2.h 
...

在损坏的环境中我得到了这个:

> ls -l
... [Info] ...  File1.h
... [Info] ...  File2.h
... 

如何制作一个健壮的脚本来遍历文件夹中的文件并确保每个文件都是一个符号链接?

【问题讨论】:

  • 如果你这样做[ -L "File1.h" ] && echo "this is a link"会得到什么?

标签: macos bash unix posix sh


【解决方案1】:

您可以使用-h-L 来检查文件是否为链接:

if [ -L "$your_file" ]; then
   echo "this is a link"
fi

或者更短,

[ -L "$your_file" ] && echo "this is a link"

来自man test

-h 文件

FILE存在并且是符号链接(同-L)

-L 文件

FILE存在并且是符号链接(同-h)

【讨论】:

    【解决方案2】:

    这是我使用的完整代码。它还递归搜索。

    for file in $(find Headers/  -name '*.h'); do
        if [ ! -L "$file" ]; then
            echo "Error - $file - is not a symlink.  The repo has probably been corrupted"
            exit 666 # evil exit code for an evil bug
        fi
    done
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-01
      • 1970-01-01
      • 2014-07-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多