Skip to content

Instantly share code, notes, and snippets.

@Gen2ly
Created July 5, 2013 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Gen2ly/5934824 to your computer and use it in GitHub Desktop.
Save Gen2ly/5934824 to your computer and use it in GitHub Desktop.
Link shortcut
#!/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