Beginner’s guide to bash scripting
Sometimes tasks can be redundant. You might find there’s so much to do that you might miss some critical tasks that require more attention than others. Also, who likes manual work? I know I don’t. This is why you need scripting.
Scripting is defining a set of tasks to a computer and telling it what to do and how to do it. It’s a lot different from coding ; coding is creation of programs while scripting is more about control and automation. There are many scripting languages but I’ll focus on bash scripting.
If you’ve been on tryhackme, they define bash scripting as a “scripting language that runs within the terminal on most Linux distros, as well as MacOS”. You put together a sequence of bash commands in a script and the shell runs it, making it alot easier to run tasks that come off as repetitive in most cases.
So what’s there to know for starters?
- A shebang — It is used to specify the interpreter with which the given script will be run by default. So if you use #!/bin/bash, the interpreter should be in the bash shell.
- Making it executable — to run a bash script, it has to have executable permissions. Do this with ‘chmod +x script.sh’. To run it, use the ./ notation ‘./script.sh’
- Command line arguements — values that are passed to a script or program when it is executed. They allow you to provide input or options to a script directly from the terminal. They can be accessed by positional and special parameters
- Control structures — use of conditional statements ie if, else, elif and loops ie for, while which allow a bash script to flow through data efficiently.
To demonstrate, I’ll showcase a few scripts I wrote. There’s a platform called Exercism which has a lot of exercises you can use to practice your scripting skills. It also has a lot of tracks on other programming language which can really help if you’re looking to learn any programming language.
Here’s an easy one for a start:
This exercise requires a script that checks the number of arguments passed to it. It enforces a specific usage pattern by accepting just one command line argument. Here’s the script;
As always, we make it executable by ‘chmod +x error_handling.sh’ in the terminal then run the script
This is the output of the script. The if statement checks to see if I passed just one argument in the script. If it’s one, it outputs a welcome message. If not, it gives me an error message, telling me the correct way to use the script.
Easy enough yeah? Let’s try another exercise
This exercise requires a script that when executed with a number as an argument, it checks if that number is divisible by 3, 5, or 7, and constructs a string based on those conditions to print out “Pling”, “Plang”, “Plong”, or the original number.
Whichever way you look at it, scripting is something every IT professional is gonna need in their arsenal. If you get enough practice and go beyond a beginner level, scripting could help you with;
- Work with files, folders and directories. File operations like reading, writing or appending, handling directories by navigating them or manipulation of files
- Regular expressions and pattern matching using tools like grep or cut
- Error handling or debugging through error messages, exit codes and handling unexpected scenarios
- Optimization and perfomance ensuring efficiency
- Security considerations to avoid vulnerabilities such as injection. You could use scripting to validate input or sanitization
- Integration and automation in tasks such as scheduling, sysadmin and intergration with other programming languages or tools
Practice makes perfect, so keep learning and keep applying as much as you can. Happy scripting