Making your script responsive

Conclusions

The basic version of the quota control script (Listing 5) aims to prevent a partition from "filling up" by combining several types of flow control in ways that try to balance efficiency with equal treatment for all users, according to the actual disk usage in any given moment. As is, however, the script deliberately misses a couple of pieces, partly due to space constraints but also to leave these enhancements as an exercise for the reader. First, the "WATCHING" section is not completely fair: Since the for loop in line 36 starts from the first index every time, over time it will delete slightly more of the first user's files than the others. To fix this, change it to make it start every time from the index following the last one used in the previous execution of the whole "WATCHING" status. (Hint: Use the $LASTINDEX variable in line 41.)

The other thing missing to make the script more efficient is code that makes it move directly from, for example, "CRITICAL" to "WATCHING" and from "WATCHING" to "EMERGENCY". Happy hacking!

PS: To safely test Listing 5, you need a folder full of non-empty files of random size. To generate this automatically, use the tricks described in [4] and [5]:

Infos

  1. "Tutorial – Shell Scripting" by Marco Fioretti, Linux Magazine, issue 219, February 2019, p. 84-88.
  2. "Tutorial – Bash Arrays" by Marco Fioretti, Linux Magazine, issue 220, March 2019, p. 84-89.
  3. Bash test operators: https://www.tldp.org/LDP/abs/html/tests.html
  4. "Generate random number between 1 and 10 with Bash Shell Script": https://stackoverflow.com/questions/8988824/generating-random-number-between-1-and-10-in-bash-shell-script
  5. "Create many files with random content": https://unix.stackexchange.com/questions/199863/create-many-files-with-random-content

The Author

Marco Fioretti (http://mfioretti.com) is a freelance author, trainer, and researcher based in Rome, Italy, who has been working with free and open source software since 1995, and on open digital standards since 2005. Marco also is a board member of the Free Knowledge Institute (http://freeknowledge.eu).

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Decision Making Scripts

    The Bash shell uses different criteria to make decisions. Learn how to teach your shell scripts to make the right choice.

  • Bash vs. Vista PowerShell

    Microsoft’s new PowerShell relies on .NET framework libraries and thus has access to a treasure trove of functions and objects. How does PowerShell measure up to traditional shells like Bash?

  • Tutorials – Shell Scripts

    Letting your scripts ask complex questions and give user feedback makes them more effective.

  • Bash Tuning

    In the old days, shells were capable of little more than calling external programs and executing basic, internal commands. With all the bells and whistles in the latest versions of Bash, however, you hardly need the support of external tools.

  • Bash Alternatives

    Don't let your familiarity with the Bash shell stop you from exploring other options. We take a look at a pair of alternatives that are easy to install and easy to use: Zsh and fish.

comments powered by Disqus
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters

Support Our Work

Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.

Learn More

News