Advertisement
Guest User

shit

a guest
Feb 10th, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.88 KB | None | 0 0
  1. #!/bin/bash
  2. #------------------------------------------------------------------------------
  3. # [Dylan Foster] Swap or Not
  4. # The purpose of this assignment is to compare the names of two
  5. # files and swap the files if they are not identical.
  6. #------------------------------------------------------------------------------
  7.  
  8. file1=$1
  9. file2=$2
  10.  
  11. function debug {
  12. echo "*******************************"
  13. echo "* $file1" "is currently this *"
  14. echo "* $file2" "is currently this *"
  15. echo "*******************************"
  16. }
  17.  
  18. # Function to show the usage message
  19. function show_usage {
  20. echo "USAGE: > bash swap.sh file1 file2"
  21. }
  22.  
  23.  
  24. # Function to show inability to swap
  25. function show_error {
  26. echo "Cannot perform swap, please check files"
  27. echo "------------------------------------------------------------"
  28. exit 1
  29. }
  30.  
  31. # Function to swap files
  32. function swap {
  33. if [ "$file1" != "$file2" ]; then
  34. #debug
  35. local TMPFILE=tmp.$$
  36. mv "$file1" $TMPFILE
  37. mv "$file2" "$file1"
  38. mv $TMPFILE "$file2"
  39. echo "$file1" "swapped with" "$file2" >> log.txt
  40. echo "$file1" "swapped with" "$file2"
  41. echo "------------------------------------------------------------"
  42. exit 0
  43. else
  44. show_usage
  45. show_error
  46. exit 1
  47. fi
  48. }
  49.  
  50. # If no arguments are passed, prompt the user for arguments
  51. if [ $# -eq 0 ]; then
  52. #debug
  53. echo -n "Enter two files you want to swap > "
  54. read file1 file2
  55. debug
  56. fi
  57.  
  58. # If only the first file is supplied
  59. if [ $# -eq 1 ]; then
  60. echo "The second filename was not supplied"
  61. show_usage
  62. show_error
  63. fi
  64.  
  65. # If more than two files are input
  66. if [ $# -gt 2 ]; then
  67. echo "More than two files are supplied"
  68. show_usage
  69. show_error
  70. fi
  71.  
  72.  
  73. # Checks to see if the files exist
  74. if [[ ! -e "$1" ]]; then
  75. echo "$1" "does not exist"
  76. #debug
  77. show_error
  78. elif [[ ! -e "$2" ]]; then
  79. echo "$2" "does not exist"
  80. #debug
  81. show_error
  82. fi
  83.  
  84. # Check if files are both readable
  85. if [[ ! -r "$file1" ]]; then
  86. echo "$file1" "is not readable"
  87. show_error
  88. elif [[ ! -r "$file2" ]]; then
  89. echo "$file2" "is not readable"
  90. show_error
  91. fi
  92.  
  93. # Check if files are both writable
  94. if [[ ! -w "$file1" ]]; then
  95. echo "$file1" "is not writable"
  96. show_error
  97. elif [[ ! -w "$file2" ]]; then
  98. echo "$file2" "is not writable"
  99. show_error
  100. fi
  101.  
  102. # Check if files are both executable
  103. if [[ ! -x "$file1" ]]; then
  104. echo "$file1" "is not executable"
  105. show_error
  106. elif [[ ! -x "$file2" ]]; then
  107. echo "$file2" "is not executable"
  108. show_error
  109. fi
  110.  
  111.  
  112. #------------------------------------------------------------------------------
  113. # Executes swap method if all checks are clear
  114.  
  115. swap
  116.  
  117. #------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement