Advertisement
Guest User

Soe z1

a guest
Mar 28th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.92 KB | None | 0 0
  1. #! /bin/bash
  2. #Autor: DM
  3. declare -a array=(-b -c -d -f -l -p -s)
  4. declare -a array2=("block special file" "character special file" \
  5.     "directory" "regular file" "symbolic link" "named pipe file (FIFO)" \
  6.     "socket file")
  7.  
  8. if [ "$1" = "--help" ]; then
  9.     echo "
  10. DESCRIPTION
  11. Program searches in arg1 \
  12. directory for files of type arg2.
  13. Directory's permissions must be set to at least r+x.
  14.  
  15. USAGE
  16. program_name arg1 arg2
  17. arg1 - directory path
  18. arg2 - file type
  19.  
  20. EXAMPLE OF USAGE
  21. ./zad1.sh ~/Desktop -d
  22.  
  23. POSSIBLE arg2
  24. -b          block special file
  25. -c          character special file
  26. -d          directory
  27. -f          regular file
  28. -l          symbolic link
  29. -p          named pipe file (FIFO)
  30. -s          socket file
  31.  
  32. EXIT CODE
  33. 0           No error
  34. 1           Incorrect number of arguments
  35. 2           Directory does not exist
  36. 3           Directory's read bit is set to 0
  37. 4           Directory's execute bit is set to 0  
  38. 5           Incorrect file type
  39. "
  40.     exit 0
  41. fi
  42.  
  43. if [ $# -ne 2 ]; then
  44.     echo "Incorrect number of arguments!
  45. Consider using --help"
  46.     exit 1
  47. fi
  48.  
  49. directory="$1"
  50.  
  51. if [ ! -d "${directory}" ]; then
  52.     echo "Directory does not exist!
  53. Consider using --help"
  54.     exit 2
  55. fi
  56.  
  57. if [ ! -r "${directory}" ]; then
  58.     echo "Directory's read bit is set to 0!
  59. Consider using --help"
  60.     exit 3
  61. fi
  62.  
  63. if [ ! -x "${directory}" ]; then
  64.     echo "Directory's execute bit is set to 0!
  65. Consider using --help"
  66.     exit 4
  67. fi
  68.  
  69. var=0
  70. flag=false
  71. for i in "${array[@]}"
  72. do 
  73.     if [ "$2" == "$i" ]; then
  74.         flag=true
  75.         break
  76.     fi
  77.     ((var++))
  78. done
  79.  
  80. if [ "$flag" = false ]; then
  81.     echo "Incorrect file type.
  82. Consider using --help"
  83.     exit 5
  84. fi
  85.  
  86. type="$2"
  87. echo "Directory: ${directory}"
  88. echo -e "Number of files of type \"${array2[$var]}\": \c"
  89.  
  90. foo () {
  91.     ls -l "$2" | grep "$1" | wc -l | awk '{ printf("%d\n", $0) }'
  92. }
  93.  
  94. case "$type" in
  95.     "-b") foo "^b" "$1";;
  96.     "-c") foo "^c" "$1";;
  97.     "-d") foo "^d" "$1";;
  98.     "-f") foo "^-" "$1";;
  99.     "-l") foo "^l" "$1";;
  100.     "-p") foo "^p" "$1";;
  101.     "-s") foo "^s" "$1";;
  102. esac
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement