-
-
Save Gen2ly/5934824 to your computer and use it in GitHub Desktop.
Link shortcut
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Link shortcut | |
# Display usage if incorrect number of parameters given | |
if [ $# != 2 ]; then | |
echo " ${0##*/} <source> <link> - ln shortcut" | |
exit 1; fi | |
# Source absolute path | |
if [ ! -e "$1" ]; then | |
echo "Non-exist: "$1"" | |
exit 1 | |
else | |
source="$(realpath "$1")"; fi | |
# Linkname absolute path | |
if [ -e "$2" ]; then | |
# existent directory | |
[ -d "$2" ] && link="$(realpath "$2")" | |
# existent non-directory | |
[ ! -d "$2" ] && { echo "Exists: "$2""; exit 1; } | |
else | |
# non-existent directory | |
if [[ "$2" == */ ]]; then { echo "Non-exist: "$2""; exit 1; } | |
# linkname only | |
elif [[ "$2" != */* ]]; then link="$(realpath .)/"$2"" | |
# linkname and parent directory | |
elif [ -d "${2%/*}" ]; then link="$(realpath "${2%/*}")/"${2##*/}"" | |
# non-existent parent directory | |
elif [ ! -d "${2%/*}" ]; then echo "Non-exist: "${2%/*}""; fi; fi | |
# Permission test | |
if [ -d "$link" ]; then | |
if [ ! -w "$link" ]; then | |
[ ! hash sudo 2>&- ] && { echo "Program required: "sudo""; exit 1; } | |
sudo=sudo; fi | |
else [ ! -d "$2" ] | |
if [ ! -w $(dirname "${link%/*}") ]; then | |
[ ! hash sudo 2>&- ] && { echo "Program required: "sudo""; exit 1; } | |
sudo=sudo; fi; fi | |
$sudo ln -sv "$source" "$link" | awk -F ' -> ' '{ print $2 " -> " $1 }' | GREP_COLOR='1;36' grep --color=always " ‘.*$" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment