我想编写一个程序来检查是否存在软链接
#!/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
将link2
,link3
,link4
,link5
。
我是否必须为n
个链接编写相同的脚本n
次,或者这可以在一个脚本中实现?
此外,我希望退出0并退出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