derobert

Config manager: tools/install-symlink

Mar 13th, 2015
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 0.84 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. if [ $# -ne 3 ]; then
  4.     echo "Usage: $0 force-y-or-n point-to symlink-goes-where" >&2
  5.     echo "(order is just like ln -s)" >&2
  6.     exit 1
  7. fi
  8.  
  9. force="$1"
  10. ptr="$2"
  11. link="$3"
  12.  
  13. if [ -h "$link" ]; then
  14.     rm "$link"
  15. elif [ -e "$link" ]; then
  16.     echo -n "Conflicting $link found... "
  17.     if [ "y" = "$force" ]; then
  18.         echo "moving."
  19.         mv -i "$link" "$link.old" || exit 1
  20.     elif [ "n" = "$force" ]; then
  21.         echo "aborted.";
  22.         exit 1
  23.     else
  24.         echo -n "Move out the way? (y/n): "
  25.         read -r -n 1
  26.         echo
  27.         if [ 'y' = "$REPLY" ]; then
  28.             mv -i "$link" "$link.old" || exit 1
  29.         else
  30.             exit 1
  31.         fi
  32.     fi
  33. fi
  34.  
  35. # Ancient machines may not have the -r option, use readlink instead.
  36. # We'll just assume any failure is due to the bad option. If not, it'll
  37. # just fail again...
  38. ln -rs "$ptr" "$link" || ln -s "$(readlink -f "$ptr")" "$link" || exit 1
Advertisement
Add Comment
Please, Sign In to add comment