derobert icon

Config manager: tools/install-symlink

derobert | PRO | 03/13/15 11:57:39 PM UTC | 0 ⭐ | 297 👁️ | Never ⏰ | []
Bash |

840 B

|

None

|

0 👍

/

0 👎

#!/bin/bash
 
if [ $# -ne 3 ]; then
    echo "Usage: $0 force-y-or-n point-to symlink-goes-where" >&2
    echo "(order is just like ln -s)" >&2
    exit 1
fi
 
force="$1"
ptr="$2"
link="$3"
 
if [ -h "$link" ]; then
    rm "$link"
elif [ -e "$link" ]; then
    echo -n "Conflicting $link found... "
    if [ "y" = "$force" ]; then
        echo "moving."
        mv -i "$link" "$link.old" || exit 1
    elif [ "n" = "$force" ]; then
        echo "aborted.";
        exit 1
    else
        echo -n "Move out the way? (y/n): "
        read -r -n 1
        echo
        if [ 'y' = "$REPLY" ]; then
            mv -i "$link" "$link.old" || exit 1
        else
            exit 1
        fi
    fi
fi
 
# Ancient machines may not have the -r option, use readlink instead.
# We'll just assume any failure is due to the bad option. If not, it'll
# just fail again...
ln -rs "$ptr" "$link" || ln -s "$(readlink -f "$ptr")" "$link" || exit 1

Comments