单变量但多个值

时间:2018-02-18 12:51:44

标签: bash variables

我想编写一个程序来检查是否存在软链接

#!/bin/bash
file="/var/link1"
if [[ -L "$file" ]]; then
    echo "$file symlink is present";
    exit 0
else
    echo "$file symlink is not present";
    exit 1
fi

link2link3link4link5

我是否必须为n个链接编写相同的脚本n次,或者这可以在一个脚本中实现?

此外,我希望退出0并退出1,以便我可以用于监控目的。

1 个答案:

答案 0 :(得分:1)

您可以使用以下功能:

checklink() {
   if [[ -L "$1" ]]; then
      echo "$1 symlink is present";
      return 0
   else
      echo "$1 symlink is not present";
      return 1
   fi
}

file1="/var/link1"
file2="/var/link2"
file3="/var/link3"
file4="/var/link4"

for f in "${file1}" "${file2}" "${file3}" "${file4}"; do
   checklink "$f" || { echo "Exit in view of missing link"; exit 1; }
done
echo "All symlinks checked"
exit 0
相关问题