In a Linux system it pays to know a thing or two about bash scripts. A bash script is a way to take a bunch of commands and place them in a text file, the text file can then be called with the bash command, or be made executable by adding a bash shebang at the top of the file, and then using the chmod command to set the permissions of the file so that it is executable, thus the script can be called directly.
In any case bash scripts are a way to take a task consisting of one or more commands that one might find themselves repeating often and pull the string of commands into a file that can then be called just once. There are then ways to edit various configuration files to make it so that the bash scripts can be called from a terminal window with just a single command name. Although it might be outside the scope of this post there is also the idea of making the script run each time a system starts, or to act as some kind of service.
In this post I will be going over the very basics of bash scripts though, and will be linking off into other topics as needed. So this is a getting started post, but also a general index post on all things bash script related that I have written about thus far.
In this section I will be going over a very basic example of a bash script that just makes use of the Linux echo command to log hello world to the console. This section is of course intended for people who are completely new to writing and editing bash scripts, if you have progress beyond the hello world of bash script files you might want to skip over this to get to the good stuff later on in this post.
First off I will want to have a plain text file that I will be calling with the bash command. I start off these files with the shebang for the bash command. This is not needed if I intend to call bash first always, however if I want to make the script executable I will want to have it there. After the shebang I just call the echo command, and place the string Hello world after that closed off in double quotes.
So then I just need to save this text as something like hello.sh, and then I am ready to call the script from the command line.
One way to call the script would be to call the bash command and pass the path to the file as the first argument.
What is nice about this is that I do not have to bother with the shebang syntax at the top of the file if I do not want it there for whatever the reason because I am calling bash directly. I also do not have to worry about permission settings for the file as long as I can read file file at least.
Another way to call the script would be to make the script executable with the chmod command. Once I do that I can call the script directly because of the shebang that I placed at the top of the file. there are several ways to go about doing this will chmod, but I have come to find that I like to use the octal syntax for it. I could get into detail about that here, but that would be off topic, for now maybe it is best to just know that the values 755 will make the file executable for the owner of the file, but not others, and that the value 777 wil make the script executable for everyone.
One important thing to understand when getting started with bash scripts, and in Linux in general is to understand piping. In Linux and post of not all other posixs systems it is possible to pipe the standard output of one command as the standard input of another command.
So in this section I will be going over some examples of piping and bash scripts. I might also touch based on a lot of useful commands in the process also.
So for starters lets take into account the ls command. It is fairly simple command that will list the contents of the current working folder, or any folder given as an argument. However there is also a number of options for the ls command such as the list all option, and an option that will append a slash to any and all items in a listing that are folders rather than files.
So I can use the ls command to get a list of all files and folders in the home folder of the current user, with a slash for all folders like this.
I can then pipe that result to grep to filter out all items that are folders then.
So say I want to have a way in which I can repeat this by just calling a single file. For this I just need to pull the command into a bash file.
I can then save this as something like list-files.sh, and then do the usual chmod 755 or call with bash to get this result each time by just calling the single file.
I could then pipe the result of this to yet even another command, to filter things even further. In any case the basic idea is there. Bash scripts do not have to just be instances where I am calling just a single command. Commands can be used together one after another to create a final result.
So lets take the script that I worked out before, and just filter things threw yet another command. This next command is the wc command which stands for word count. This command can be used to get the word count of a file as one might suspect, however there are a few more useful options such as the -l option that will count the lines of a file, or the standard input.
Even more examples might be call for when it comes to piping, but you should get the basic idea. The thing about this is to just read over what a command can do when it comes to what a command will take from the standard input, and how the output of a command can be formated.
When making a bash script variables can be used as a way to store values that can then be used again in a later part of a script one or more times. In this section I will be going over the basics of variables, and other related topics such as how to capture the output of a command as a value for a variable and how to do basic math operations.
One way to create a variable is to just type a name for the variable followed by equals, and then the value as a string value.
Every time I use a Linux command there is a way to make use of one or more options. For example the ls command can just be called by itself, and when doing so it will display the contents of the current working folder. However I can pass a path to a folder other than where I currently am in the command line, and also some additional options that will for example show all items in a folder including hidden files.
When writing bash scripts I can make use of one or more positional arguments, and in this section I will be going over a few examples of how to go about making use of such arguments.
6 - While loops
Like many other languages there are ways to go about writing loops, and one such kind of loop to work with is a while loop. A while loop starts off with the the while beyond followed by an expression that will result in zero or one in square brackets. Then between do and done keywords one just needs to place whatever commands they want to run for each instances of the loop while the condition is true, and step any variables that might need to be updated.
Functions can also be defined in a bash script when I want to define a string of commands, or some kind of block of code that I want to repeat over and over again elsewhere in a script.
There is a great deal more to write about when it comes to bash scripts in general with respect to all the little basic details there are needed to know how to go about writing them. In time I might get around to expanding this post more with additional examples that showcase everything that there is to work with when it comes to the bash script syntax at least.