簡體   English   中英

Symlink檢查-Linux Bash腳本

[英]Symlink check - Linux Bash Script

我正在嘗試創建一個腳本,該腳本在目錄中進行搜索以找到指向不存在的對象的符號鏈接。

我的目錄中有一個文件,該文件的符號鏈接已刪除,但是由於某些原因,當我運行以下腳本時,它說文件存在。

#!/bin/bash
ls -l $1 |
if [ -d $1 ]
  then
    while read file
    do
            if test -e $1
            then
                    echo "file exists"
            else
                    echo "file does not exist"
            fi
    done
 else
    echo "No directory given"
fi

謝謝

檢查此頁面 它對斷開的鏈接進行測試。 它使用-h運算符標識符號鏈接,並使用-e運算符檢查存在性。

從該頁面:

linkchk () {
    for element in $1/*; do
      [ -h "$element" -a ! -e "$element" ] && echo \"$element\"
      [ -d "$element" ] && linkchk $element
    # Of course, '-h' tests for symbolic link, '-d' for directory.
    done
}

#  Send each arg that was passed to the script to the linkchk() function
#+ if it is a valid directoy.  If not, then print the error message
#+ and usage info.
##################
for directory in $directorys; do
    if [ -d $directory ]
    then linkchk $directory
    else 
        echo "$directory is not a directory"
        echo "Usage: $0 dir1 dir2 ..."
    fi
done

exit $?

您可以使用以下方法測試鏈接是否有效:

[[ -f "$link" ]] && echo "points to a valid file"

要檢查是否確實是鏈接,請使用-L

[[ -L "$link" ]] && echo "it's a link"

似乎有一個名為symlinks的程序,除其他功能外,它symlinks滿足您的需求。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM