uni

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

createpvs (2751B)


      1 #!/bin/sh
      2 
      3 # ΧΡΗΣΤΟΣ ΜΑΡΓΙΩΛΗΣ - [REDACTED]
      4 
      5 main() {
      6         # We could allow more than 4 command line arguments
      7         # since it's not going to make a difference, but for
      8         # safety, we'll exit if they're not exactly 4.
      9         test $# -eq 4 || usage
     10 
     11         # Give readable names to each argument.
     12         rootdir=${1}
     13         ndbdirs=${2}
     14         ndatadirs=${3}
     15         username=${4}
     16 
     17         # Error checks.
     18         test -d "${rootdir}" || err "${rootdir}: no such directory"
     19         isnumber ${ndbdirs} || err "'${ndbdirs}' is not a number"
     20         isnumber ${ndatadirs} || err "'${ndatadirs}' is not a number"
     21 
     22         # In order for `username` to really be a user's name, it
     23         # has to exist in `/etc/passwd`.
     24         test ! -z "$(grep -w "^${username}" /etc/passwd)" ||
     25         err "'${username}' is not in /etc/passwd"
     26 
     27         # The actual exercise.
     28         makedirs "dbfolder" ${ndbdirs}
     29         makedirs "datafolder" ${ndatadirs}
     30 }
     31 
     32 usage() {
     33         # `{0##*/}` means the first argument (i.e the script's name) with
     34         # its path stripped, if it exists. 
     35         echo "usage: ${0##*/} root_dir num_dbdirs num_datadirs username" 1>&2
     36         exit 1
     37 }
     38 
     39 err() {
     40         echo "${0##*/}: $@" 1>&2
     41         exit 1
     42 }
     43 
     44 isnumber() {
     45         # If the argument does not begin *and* end in a number, then
     46         # it's not a number. This also excludes negative numbers, but
     47         # we shouldn't accept negative numbers anyway.
     48         test ! -z "$(echo "${1}" | grep "^[0-9]\+$")"
     49 }
     50 
     51 # Make directories in the following format: dir1, dir2, dir3...
     52 # If there are already directories with the same name (e.g dir1, dir2), we need
     53 # to number the new ones taking the existing ones into account (i.e dir3, dir4....).
     54 makedirs() {
     55         # Number of directories to create.
     56         n=${2}
     57 
     58         # Get the last directory, if any, so that we know what the last number was.
     59         lastdir=$(ls ${rootdir} | grep "${1}" | sed "s/${1}//g" | sort -V | tail -1)
     60 
     61         # If we didn't find any, we'll start numbering them from 1.
     62         test -z ${lastdir} && lastdir=0
     63 
     64         # If `n` was already 0 we won't do anything.
     65         while ! [ $(expr ${n}) = 0 ]; do
     66                 # `lastdir` now holds the next number.
     67                 lastdir=$(expr ${lastdir} + 1)
     68 
     69                 # Make a directory using the format described above
     70                 # and change the owner to `username`.
     71                 mkdir "${rootdir}/${1}${lastdir}" &&
     72                 chown ${username}: "${rootdir}/${1}${lastdir}"
     73 
     74                 # Decrement by 1 until we reach `n = 0` so that
     75                 # we can exit the loop.
     76                 n=$(expr ${n} - 1)
     77         done 
     78 }
     79 
     80 # Pass all command line arguments to `main`.
     81 main "$@"