Automating Tasks with Shell Scripts
Sometimes, you’ll find yourself repeating the same few commands over and over, perhaps with slight variations. You reach the point of frustration, and say, “That’s it; I’m scripting this.” Being a CLI wizard, you do the following:
- Run
tail -n 20 ~/.bash_history > myscript.sh
to create a file that contains the last 20 Bash commands you ran. - Then run
bash myscript.sh
to execute it.
Although this isn’t the recommended procedure (we’ll get to that in this chapter), it’s a perfectly valid way to create and run a Bash script.
This chapter is a Bash scripting crash course. Like any programming crash course, it is completely useless unless you actually follow along, type in all the code yourself, and run it in your own Linux environment. In addition to showing you the subset of Bash’s syntax which is considered modern and best-practice, we’ll give you...