Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #! /bin/bash
- #Autor: DM
- declare -a array=(-b -c -d -f -l -p -s)
- declare -a array2=("block special file" "character special file" \
- "directory" "regular file" "symbolic link" "named pipe file (FIFO)" \
- "socket file")
- if [ "$1" = "--help" ]; then
- echo "
- DESCRIPTION
- Program searches in arg1 \
- directory for files of type arg2.
- Directory's permissions must be set to at least r+x.
- USAGE
- program_name arg1 arg2
- arg1 - directory path
- arg2 - file type
- EXAMPLE OF USAGE
- ./zad1.sh ~/Desktop -d
- POSSIBLE arg2
- -b block special file
- -c character special file
- -d directory
- -f regular file
- -l symbolic link
- -p named pipe file (FIFO)
- -s socket file
- EXIT CODE
- 0 No error
- 1 Incorrect number of arguments
- 2 Directory does not exist
- 3 Directory's read bit is set to 0
- 4 Directory's execute bit is set to 0
- 5 Incorrect file type
- "
- exit 0
- fi
- if [ $# -ne 2 ]; then
- 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 execute bit is set to 0!
- Consider using --help"
- exit 4
- fi
- var=0
- flag=false
- for i in "${array[@]}"
- do
- if [ "$2" == "$i" ]; then
- flag=true
- break
- fi
- ((var++))
- done
- if [ "$flag" = false ]; then
- echo "Incorrect file type.
- Consider using --help"
- exit 5
- fi
- type="$2"
- echo "Directory: ${directory}"
- echo -e "Number of files of type \"${array2[$var]}\": \c"
- foo () {
- ls -l "$2" | grep "$1" | wc -l | awk '{ printf("%d\n", $0) }'
- }
- case "$type" in
- "-b") foo "^b" "$1";;
- "-c") foo "^c" "$1";;
- "-d") foo "^d" "$1";;
- "-f") foo "^-" "$1";;
- "-l") foo "^l" "$1";;
- "-p") foo "^p" "$1";;
- "-s") foo "^s" "$1";;
- esac
Advertisement
Add Comment
Please, Sign In to add comment