Advertisement
Guest User

Untitled

a guest
Mar 29th, 2017
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.01 KB | None | 0 0
  1. #!/bin/bash
  2. #Autor: Dominik Mańkowski
  3.  
  4. if [ "$1" = "--help" ]; then
  5. echo "
  6. DESCRIPTION
  7. Program searches for files that are located in the directory
  8. arg1 (and it's subdirectories) and have permissions equal or greater than
  9. arg2. Then the program prints the list of matching files. And then repeats it for
  10. arg3,...,argN. If an error ocurrs (e.g. can't access the folder) the
  11. standard error ouptut (stderr) is redirected to the file error.log.
  12.  
  13. USAGE
  14. script_name arg1 arg2 ... argN
  15. arg1 - directory
  16. arg2,...,argN - set of permissions
  17.  
  18. EXAMPLE OF USAGE
  19. ./1.sh . 600 744
  20.  
  21. EXIT CODE
  22. 0 No errors
  23. 1 Incorrect number of arguments
  24. 2 Directory does not exist
  25. 3 Directory's read bit is set to 0
  26. 4 Directory's execution bit is set to 0
  27. 5 Incorrect argument - permissions' arg length != 3
  28. 6 Incorrect argument - permission is lesser than 0 or greater than 7
  29. "
  30. exit 0
  31. fi
  32.  
  33. if [ $# -lt 2 ]; then # ilosc arugmentow mniejsza od 2
  34. echo "Incorrect number of arguments!
  35. Consider using --help"
  36. exit 1
  37. fi
  38.  
  39. directory="$1"
  40.  
  41. if [ ! -d "${directory}" ]; then
  42. echo "Directory does not exist!
  43. Consider using --help"
  44. exit 2
  45. fi
  46.  
  47. if [ ! -r "${directory}" ]; then
  48. echo "Directory's read bit is set to 0!
  49. Consider using --help"
  50. exit 3
  51. fi
  52.  
  53. if [ ! -x "${directory}" ]; then
  54. echo "Directory's execution bit is set to 0!
  55. Consider using --help"
  56. exit 4
  57. fi
  58.  
  59. shift # przesuwamy argumenty w lewo, zmniejszamy ilość argumentów o 1. $1 to teraz pierwszy argument z prawami dostępu
  60. while (("$#")); do # dopóki liczba argumentów jest niezerowa
  61.  
  62. arg="$1"
  63.  
  64. if [ ! ${#arg} = 3 ]; then # jeżeli ilość znaków argumentu jest różna od 3
  65. echo "Incorrect argument!
  66. Consider using --help"
  67. exit 5
  68. fi
  69.  
  70. checkArguments(){ # jeżeli ten znak to nie cyfra lub ta cyfra jest <0 lub >7
  71. if [ ! "$1" -eq "$1" ] || [ "$1" -lt 0 ] || [ "$1" -gt 7 ]; then
  72. echo "Incorrect argument!
  73. Consider using --help"
  74. exit 6
  75. fi
  76. }
  77.  
  78. u_perm=${arg:0:1} # jeśli argument to 764, to to jest 7
  79. checkArguments $u_perm # przekazujemy znak 7 do funkcji checkAguments (to jest tam "$1")
  80.  
  81. g_perm=${arg:1:1} # to jest 6
  82. checkArguments $g_perm
  83.  
  84. o_perm=${arg:2:1} # to jest 4
  85. checkArguments $o_perm
  86.  
  87. echo "Files with permissions greater equal than $arg:"
  88.  
  89. #linijka poniżej nie zadziała na systemach BSD-owych, np. na macOS - z racji braku identycznej implementacji find na tych systemach
  90. #Na ubuntu czy linuxie jak najbardziej zadziała (czyli na GNU)
  91. #jeśli chcesz to uruchomić na Macu, zainstaluj sobie findutils:
  92. # $brew install findutils
  93. #a następnie zakomentuj linijkę u góry i odkomentuj tę u dołu
  94. #będzie działać tak jak na GNU wtedy :)
  95.  
  96. find "${directory}" -perm -"$arg" -printf "%f: %m\n" 2>> error.log | tee /dev/tty | wc -l | awk '{ printf("Number of matching files: %d\n", $0) }'
  97.  
  98. #wersja na macOS
  99. #gfind "${directory}" -perm -"$arg" -printf "%f: %m\n" 2>> error.log | tee /dev/tty | wc -l | awk '{ printf("Number of matching files: %d\n", $0) }'
  100.  
  101. #po krótce co tu się dzieje: w katalogu directory znajdź pliki o prawach dostępu (opcja -perm) wyższych/równych (stąd minus przed arg)
  102. #i następnie na wyjściu wyświetl w postaci "nazwa_pliku: prawa dostepu\n", a ewentualne informacje o błędach z stderr przekieruj do
  103. #pliku error.log (i nie nadpisuj pliku, tylko dopisz na końcu). Teraz zrób rozwidlenie, tzn. przejmij wyjście z funkcji find,
  104. #jedno przekieruj do terminala (tty), a drugie do funkcji wc. wc -l zlicza ilość linii w tekście który dostanie i wyświetla tę wartość na wyjściu.
  105. #Ale polecenie awk przejmuje to wyjście jako swoje wejście, następnie uruchamia funkcję printf i wyświetla argument jaki dostanie.
  106.  
  107. shift
  108. done
  109.  
  110. if [ -s ./error.log ]; then # jeżeli plik error.log jest niepusty to wyświetl informację
  111. echo ""
  112. echo "Check error.log file for errors"
  113. else # a jeśli jest pusty to go skasuj (niestety za każdym wywołaniem skryptu jest on tworzony na dzień dobry)
  114. rm -f ./error.log
  115. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement