Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- #Autor: Dominik Mańkowski
- if [ "$1" = "--help" ]; then
- echo "
- DESCRIPTION
- Program searches for files that are located in the directory
- arg1 (and it's subdirectories) and have permissions equal or greater than
- arg2. Then the program prints the list of matching files. And then repeats it for
- arg3,...,argN. If an error ocurrs (e.g. can't access the folder) the
- standard error ouptut (stderr) is redirected to the file error.log.
- USAGE
- script_name arg1 arg2 ... argN
- arg1 - directory
- arg2,...,argN - set of permissions
- EXAMPLE OF USAGE
- ./1.sh . 600 744
- EXIT CODE
- 0 No errors
- 1 Incorrect number of arguments
- 2 Directory does not exist
- 3 Directory's read bit is set to 0
- 4 Directory's execution bit is set to 0
- 5 Incorrect argument - permissions' arg length != 3
- 6 Incorrect argument - permission is lesser than 0 or greater than 7
- "
- exit 0
- fi
- if [ $# -lt 2 ]; then # ilosc arugmentow mniejsza od 2
- echo "Incorrect number of arguments!
- Consider using --help"
- exit 1
- fi
- directory="$1"
- if [ ! -d "${directory}" ]; then
- echo "Directory does not exist!
- Consider using --help"
- exit 2
- fi
- if [ ! -r "${directory}" ]; then
- echo "Directory's read bit is set to 0!
- Consider using --help"
- exit 3
- fi
- if [ ! -x "${directory}" ]; then
- echo "Directory's execution bit is set to 0!
- Consider using --help"
- exit 4
- fi
- shift # przesuwamy argumenty w lewo, zmniejszamy ilość argumentów o 1. $1 to teraz pierwszy argument z prawami dostępu
- while (("$#")); do # dopóki liczba argumentów jest niezerowa
- arg="$1"
- if [ ! ${#arg} = 3 ]; then # jeżeli ilość znaków argumentu jest różna od 3
- echo "Incorrect argument!
- Consider using --help"
- exit 5
- fi
- checkArguments(){ # jeżeli ten znak to nie cyfra lub ta cyfra jest <0 lub >7
- if [ ! "$1" -eq "$1" ] || [ "$1" -lt 0 ] || [ "$1" -gt 7 ]; then
- echo "Incorrect argument!
- Consider using --help"
- exit 6
- fi
- }
- u_perm=${arg:0:1} # jeśli argument to 764, to to jest 7
- checkArguments $u_perm # przekazujemy znak 7 do funkcji checkAguments (to jest tam "$1")
- g_perm=${arg:1:1} # to jest 6
- checkArguments $g_perm
- o_perm=${arg:2:1} # to jest 4
- checkArguments $o_perm
- echo "Files with permissions greater equal than $arg:"
- #linijka poniżej nie zadziała na systemach BSD-owych, np. na macOS - z racji braku identycznej implementacji find na tych systemach
- #Na ubuntu czy linuxie jak najbardziej zadziała (czyli na GNU)
- #jeśli chcesz to uruchomić na Macu, zainstaluj sobie findutils:
- # $brew install findutils
- #a następnie zakomentuj linijkę u góry i odkomentuj tę u dołu
- #będzie działać tak jak na GNU wtedy :)
- 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) }'
- #wersja na macOS
- #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) }'
- #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)
- #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
- #pliku error.log (i nie nadpisuj pliku, tylko dopisz na końcu). Teraz zrób rozwidlenie, tzn. przejmij wyjście z funkcji find,
- #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.
- #Ale polecenie awk przejmuje to wyjście jako swoje wejście, następnie uruchamia funkcję printf i wyświetla argument jaki dostanie.
- shift
- done
- if [ -s ./error.log ]; then # jeżeli plik error.log jest niepusty to wyświetl informację
- echo ""
- echo "Check error.log file for errors"
- else # a jeśli jest pusty to go skasuj (niestety za każdym wywołaniem skryptu jest on tworzony na dzień dobry)
- rm -f ./error.log
- fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement