Reputation: 3239
What I am trying to do:
I want every directory in the home folder to contain a shared folder where I will put some shared files for everyone to read. I also stored the shared folder in home. The directory structure looks like this:
home
---user1
------shared
------someFolder
---user2
------someFolder
---shared
I want to make sure I am not inserting a link to the shared folder inside itself. I also want to check if the folders have a link to the shared folder. If it already has a link then do nothing. If it does not have a link then create one.
Here is my code:
for d in */ ; do
if [ "$d" != "shared/" ]
then
shared_exists=false
for e in "$d"*/; do
#echo "$e"
if [ "$e" = $d"shared/" ]
then
shared_exists=true
fi
done
if [ "$shared_exists" = true ]
then
echo "shared exists in $d"
else
echo "Shared does not exist in $d"
sudo ln -s /home/shared/ /home/"$d"
fi
fi
done
Is this the correct way or is there a better way of doing this?
Upvotes: 1
Views: 71
Reputation: 784898
You can refactor that code to this much shorter code:
shopt -s extglob nullglob
cd /home
for d in !(shared)/; do
[[ ! -e "$d"shared ]] && ln -s "$PWD/shared" "$d"shared
done
Upvotes: 2