uni

University stuff
git clone git://git.christosmarg.xyz/uni-assignments.git
Log | Files | Refs | README | LICENSE

searching (3155B)


      1 #!/bin/sh
      2 
      3 # ΧΡΗΣΤΟΣ ΜΑΡΓΙΩΛΗΣ - [REDACTED]
      4 
      5 main() {
      6         mode=${1}
      7         days=${2}
      8         # Command line arguments must be 2.
      9         test $# -eq 2 || usage
     10         # Check if `1` and `2` are numbers, otherwise exit.
     11         isnumber ${mode} || err "'${mode}' is not a valid number"
     12         isnumber ${days} || err "'${days}' is not a valid number"
     13         # In order for `mode` to really be a valid mode, `0 <= x <= 7777` must
     14         # satisfied, since permission modes can range from 0 to 7777.
     15         test ${mode} -ge 0 && test ${mode} -le 7777 || err "'${mode}' is not a valid mode"
     16 
     17         # Pretty ugly loop.
     18         sum1=0; sum2=0; sum3=0; sum4=0; sum5=0
     19         # Exit if `cont` is "n".
     20         while [ ! "${cont}" = "n" ] ; do
     21                 read -erp "Directory name: " dir
     22 
     23                 # Exit if nothing was typed.
     24                 test ! -z "${dir}" || err "please give a directory name"
     25                 # Check if `dir` really is a directory.
     26                 test -d "${dir}" || err "'${dir}' is not a directory"
     27 
     28                 echo "Files with permissions ${mode}: "
     29                 find "${dir}" -perm ${mode} 2> /dev/null
     30                 sum1=$((${sum1}+$(find "${dir}" -perm ${mode} 2> /dev/null | wc -l)))
     31                 echo ""
     32 
     33                 echo "Files modified within the last ${days} days: "
     34                 find "${dir}" -mtime -${days} 2> /dev/null
     35                 sum2=$((${sum2}+$(find "${dir}" -mtime -${days} 2> /dev/null | wc -l)))
     36                 echo ""
     37 
     38                 echo "Subdirectories accessed within the last ${days} days: "
     39                 find "${dir}" -type d -atime -${days} 2> /dev/null
     40                 sum3=$((${sum3}+$(find "${dir}" -type d -atime -${days} 2> /dev/null | wc -l)))
     41                 echo ""
     42 
     43                 echo "Files which all users have read access to: "
     44                 ls -l "${dir}" 2> /dev/null | grep "^-r..r..r.."
     45                 sum4=$((${sum4}+$(ls -l "${dir}" 2> /dev/null | grep "^-r..r..r.." | wc -l)))
     46                 echo ""
     47 
     48                 echo "Subdirectories which others have write access to: "
     49                 ls -l "${dir}" 2> /dev/null | grep "^d.w..w..w."
     50                 sum5=$((${sum5}+$(ls -l "${dir}" 2> /dev/null | grep "^d.w..w..w." | wc -l)))
     51                 echo ""
     52 
     53                 read -erp "Do you want to continue (y/n)? " cont
     54                 test "${cont}" = "n" &&
     55 
     56                 printf "Total entries found for:\n1. %s\n2. %s\n3. %s\n4. %s\n5. %s\n" \
     57                         "${sum1}" "${sum2}" "${sum3}" "${sum4}" "${sum5}"
     58         done
     59 
     60 }
     61 
     62 usage() {
     63         # `{0##*/}` means the first argument (i.e the script's name) with
     64         # its path stripped, if it exists. 
     65         echo "usage: ${0##*/} perms days" 1>&2
     66         exit 1
     67 }
     68 
     69 err() {
     70         echo "${0##*/}: $@" 1>&2
     71         exit 1
     72 }
     73 
     74 isnumber() {
     75         # If the argument does not begin *and* end in a number, then
     76         # it's not a number. This also excludes negative numbers, but
     77         # we shouldn't accept negative numbers anyway.
     78         test ! -z "$(echo "${1}" | grep "^[0-9]\+$")"
     79 }
     80 
     81 # Pass all command line arguments to `main`.
     82 main "$@"