Uma reescrita para reduzir a duplicação
#!/bin/bash
# Make script executable with: chmod u+x brew.sh
# Ask for the administrator password upfront.
sudo -v
# Keep-alive: update existing 'sudo' time stamp until the script has finished.
while true; do sudo -n true; sleep 60; kill -0 "$$" || exit; done 2>/dev/null &
# Create '.other'-folder
echo "--> ~/.other"
mkdir -p ~/.other 2>/dev/null
echo ""
create_dir() {
local pathname=$1 destination=$2 permissions=$3
local dirname=$(basename "$pathname")
echo "--> $pathname"
if [ -L "$pathname" ] && [ "$(dirname "$(readlink "$pathname")")" = "$destination" ]; then
echo "$pathname is already a symbolic link to $destination/$filename"
return
elif [ -d "$pathname" ]; then
echo "Directory $pathname exists. Moving it to $destination..."
mv "$pathname" $destination/
else
echo "Directory $pathname doesn't exist Creating it in $destination..."
mkdir -p "$destination/$dirname"
fi
chmod "$permissions" "$destination/$direname"
echo "Linking $destination/$dirname to $pathname ..."
(
cd "$(dirname "$pathname")"
ln -s "$destination/$dirname"
)
echo
}
create_file() {
local pathname=$1 destination=$2 permissions=$3
local filename=$(basename "$pathname")
echo "--> $pathname"
if [ -L "$pathname" ] && [ "$(dirname "$(readlink "$pathname")")" = "$destination" ]; then
echo "$pathname is already a symbolic link to $destination/$filename"
return
elif [ -a "$pathname" ]; then
echo "File $pathname exists. Moving it to $destination..."
mv "$pathname" $destination/
else
echo "File $pathname doesn't exists. Creating it in $destination..."
touch "$destination/$filename"
fi
chmod "$permissions" "$destination/$filename"
echo "Linking $destination/$filename to ~/.bash_history..."
(
cd "$(dirname "$pathname")"
ln -s "$destination/$filename"
)
echo ""
}
create_dir ~/.Trash ~/.other 755 # TRASH
create_file ~/.bash_history ~/.other 600 # BASH_HISTORY
create_file ~/.bash_sessions ~/.other 644 # BASH_SESSIONS
create_dir ~/.local ~/.other 755 # .LOCAL
create_dir ~/.config ~/.other 755 # .CONFIG
create_file ~/.bashrc ~/.dotfiles 644 # etc ...
Notas:
-
mkdir -p
criará o diretório, se ele não existir - verifique se o diretório / arquivo já não é um link simbólico.
- se arquivos específicos exigirem permissões específicas, não há escolha a não ser especificá-lo.