unlink.sh 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #!/usr/bin/env bash
  2. ############################################################################
  3. # tools/unlink.sh
  4. #
  5. # Copyright (C) 2008 Gregory Nutt. All rights reserved.
  6. # Author: Gregory Nutt <gnutt@nuttx.org>
  7. #
  8. # Redistribution and use in source and binary forms, with or without
  9. # modification, are permitted provided that the following conditions
  10. # are met:
  11. #
  12. # 1. Redistributions of source code must retain the above copyright
  13. # notice, this list of conditions and the following disclaimer.
  14. # 2. Redistributions in binary form must reproduce the above copyright
  15. # notice, this list of conditions and the following disclaimer in
  16. # the documentation and/or other materials provided with the
  17. # distribution.
  18. # 3. Neither the name NuttX nor the names of its contributors may be
  19. # used to endorse or promote products derived from this software
  20. # without specific prior written permission.
  21. #
  22. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  23. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  24. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  25. # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  26. # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  27. # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  28. # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  29. # OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  30. # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  31. # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  32. # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  33. # POSSIBILITY OF SUCH DAMAGE.
  34. #
  35. ############################################################################
  36. link=$1
  37. # Verify that arguments were provided
  38. if [ -z "${link}" ]; then
  39. echo "Missing link argument"
  40. exit 1
  41. fi
  42. # Check if something already exists at the link path
  43. if [ -e "${link}" ]; then
  44. # Yes, is it a symbolic link? If so, then remove it
  45. if [ -h "${link}" ]; then
  46. rm -f "${link}"
  47. else
  48. # If the path is a directory and contains the "fake link" mark, then
  49. # treat it like a soft link (i.e., remove the directory)
  50. if [ -d "${link}" -a -f "${link}/.fakelnk" ]; then
  51. rm -rf "${link}"
  52. else
  53. # It is something else (like a file) or directory that does
  54. # not contain the "fake link" mark
  55. echo "${link} already exists but is not a symbolic link"
  56. exit 1
  57. fi
  58. fi
  59. fi