uni

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

bck (1543B)


      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 3 || usage
     10 
     11         # Give readable names to each argument.
     12         username=${1}
     13         src=${2}
     14         dst=${3}
     15 
     16         # Error checks.
     17         # In order for `username` to really be a user's name, it
     18         # has to exist in `/etc/passwd`.
     19         test ! -z "$(grep -w "^${username}" /etc/passwd)" ||
     20         err "'${username}' is not in /etc/passwd"
     21 
     22         # The -e options means anything (file, directory, device, etc.).
     23         test -e ${src} || err "${src}: no such file or directory"
     24         test -e ${dst} || err "${dst}: no such file or directory"
     25 
     26         # Create an archive if `dst` is a directory.
     27         test -d ${dst} && tar -cvf "${dst}/${src##*/}.tar" ${src}
     28 
     29         # Append to `dst` only if it is a tar archive. If it's a non-archive
     30         # then we cannot append since that would not make sense.
     31         # `{dst##*.}` means that we want to get only its extension, if any.
     32         test ${dst##*.} = "tar" && tar -rf ${dst} ${src}
     33 }
     34 
     35 usage() { 
     36         # `{0##*/}` means the first argument (i.e the script's name) with
     37         # its path stripped, if it exists. 
     38         echo "usage: ${0##*/} username src dst" 1>&2
     39         exit 1
     40 }
     41 
     42 err() {
     43         echo "${0##*/}: $@" 1>&2
     44         exit 1
     45 }
     46 
     47 # Pass all command line arguments to `main`.
     48 main "$@"