Need explanation for a bash script

Based on the below script, please answer the following questions:

  1. What is happening in this script here?
  2. How can you ensure the stability?
  3. How can you ensure the persistency of a script like this on a machine?
  4. Can you identify any possible issues with this script?

///SCRIPT///

  1. #!/bin/bash

  2. dtm=("/var/log" "/home")

  3. log_file="/var/log/disk_monitor.log"

  4. td=80

  5. fn_a() {

  6. local dir=$1

  7. local ol=$(find $dir -name "*.log" -mtime +30)

  8. if [ -n "$ol" ]; then

  9. tar czf $dir/logs_$(date +'%Y%m%d').tar.gz $ol

  10. rm $ol

  11. fi

  12. }

  13. for dir in "${dtm[@]}"; do

  14. fs=$(df $dir | tail -1 | awk '{print $1}')

  15. ug=$(df $fs | tail -1 | awk '{print $5}' | sed 's/%//')

  16. if [ $ug -gt $td ]; then

  17. fn_a $dir

  18. fi

  19. done

  1. Is this a homework? Is this script intended for any hypothetical or a real environment?
  2. What have you tried to describe by yourself so far? What do you think is happening based on your understanding of syntax?
  3. What stability do you have in mind? Stability of execution? Any remarks regarding input/variable sanitation requirement?
  4. Define "persistency".
  5. Many possible issues can be identified, depending on how deep you go into code analysis. Most common mistakes can be found with e.g. ShellCheck
2 Likes