Premkumar Waghmare
Premkumar Waghmare

Reputation: 1

Single variable but multiple values

I want to write a program which will check if soft links exist or not

#!/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

There will be link2, link3, link4, link5.

Do I have to write the same script n number of times for n number of links or can this be achieved in one script?

Also I want to have the exit 0 and exit 1 so that I can use for monitoring purpose.

Upvotes: 0

Views: 54

Answers (1)

Walter A
Walter A

Reputation: 19982

You can use a function:

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

Upvotes: 1

Related Questions