code / scripts

Lines748 Shell528 Python72 Lua67 Bourne Again Shell62 make15
1 others 4
Markdown4
(46 lines)
1 #!/bin/sh
2 # Symlink is a small script to basically make `ln -s` less of a pain in the ass.
3 # This script automagically figures out which direction you mean, and allows you to
4 # pass in two actual paths (which works with tab completion) and specify if you mean
5 # to make a relative or absolute link (relative is assumed by default).
6 #
7 # Usage: symlink [-a|--absolute] path1 path2
8 #
9 absolute="n"
10 if [ "$1" = "-a" ] || [ "$1" = "--absolute" ]; then
11 absolute="y"
12 shift
13 fi
15 if [ $# -ne 2 ]; then
16 echo "Usage: symlink [-a|--absolute] <target or link> <target or link>"
17 exit 1
18 fi
20 if [ -e "$1" ] && [ -e "$2" ]; then
21 printf "Both \x1b[1m%s\x1b[0m and \x1b[1m%s\x1b[0m already exist. Please delete one of them before proceeding.\n" "$@"
22 exit 1
23 elif ! [ -e "$1" ] && ! [ -e "$2" ]; then
24 printf "\x1b[31mWarning: Neither \x1b[1m%s\x1b[22m nor \x1b[1m%s\x1b[22m exist!\x1b[0m\n" "$@"
25 choice=$(printf "%s\n" "1. $1 -> $2" "2. $2 -> $1" "3. Whoops that was a mistake" | fzy -p "Which do you mean? " | awk -F. '{print $1}' || exit 1)
26 case $choice in
27 1) linkname="$1"; target="$2";;
28 2) linkname="$2"; target="$1";;
29 *) exit 1;;
30 esac
31 elif [ -e "$1" ]; then
32 linkname="$2"; target="$1"
33 else
34 linkname="$1"; target="$2"
35 fi
37 if [ $absolute = "y" ]; then
38 target="$(realpath "$target")"
39 else
40 target="$(realpath --relative-to="$(dirname "$linkname")" "$target")"
41 fi
43 printf "Linking \x1b[1m%s\x1b[0m -> \x1b[1m%s\x1b[0m Is this okay?" "$linkname" "$target"
44 ask -y || exit 1
45 echo
46 ln -sv -T "$target" "$linkname"