v-delete-fs-directory 897 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #!/bin/bash
  2. # info: delete directory
  3. # options: USER DIRECTORY
  4. #
  5. # The function deletes directory on the file system
  6. user=$1
  7. dst_dir=$2
  8. # Checking arguments
  9. if [ -z "$dst_dir" ]; then
  10. echo "Usage: USER DIRECTORY"
  11. exit 1
  12. fi
  13. # Checking vesta user
  14. if [ ! -e "$VESTA/data/users/$user" ]; then
  15. echo "Error: vesta user $user doesn't exist"
  16. exit 3
  17. fi
  18. # Checking user homedir
  19. homedir=$(grep "^$user:" /etc/passwd | cut -f 6 -d :)
  20. if [ -z $homedir ]; then
  21. echo "Error: user home directory doesn't exist"
  22. exit 12
  23. fi
  24. # Checking destination path
  25. rpath=$(readlink -f "$dst_dir")
  26. if [ -z "$(echo $rpath |egrep "^/tmp|^$homedir")" ]; then
  27. echo "Error: invalid destination path $dst_dir"
  28. exit 1
  29. fi
  30. # Deleting directory
  31. sudo -u $user rm -rf "$dst_dir" # >/dev/null 2>&1
  32. if [ $? -ne 0 ]; then
  33. echo "Error: directory $dst_dir was not deleted"
  34. exit 3
  35. fi
  36. # Exiting
  37. exit