Advertisement
regzarr

lab2 OS

Feb 24th, 2020
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 0.67 KB | None | 0 0
  1. if [[ $# < 2 ]]; then
  2.   me=`basename "$0"`
  3.   echo "Usage: ./$me <output file> <arg1> <arg2> ... <argN>"
  4.   exit 1
  5. fi
  6.  
  7. # count even numbers in c
  8. # sum all numbers in s
  9. # compute digit count for s in variable d
  10. filename=$1
  11. shift
  12.  
  13. c=0
  14. s=0
  15.  
  16. for number in "$@" ; do
  17.   (( s += number ))
  18.   if [[ $((number%2)) -eq 0 ]]; then
  19.     (( c += 1 ))
  20.   fi
  21. done
  22.  
  23. d=${#s}
  24.  
  25.  
  26. if [ -f $filename ] && [ ! -L $filename ]; then
  27.   # write c, s and d to file
  28.   echo "count of even numbers is $c" >> $filename
  29.   echo "sum of digits is $s" >> $filename
  30.   echo "digit count for s is $d" >> $filename
  31. else
  32.   # error
  33.   echo "File does not exist or is not a regular file!"
  34.   exit 1
  35. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement