I have a lot of pots boiling when it comes to things to learn and research more, one of which is to become more competent when it comes to working with a Linux system. A major part of doing so is to learn a hold lot more about bash, and with that that bash built in commands once of which is the Linux test bash built in command.
In a previous Linux post on bash scripts I wrote about special parameters one of which is the \$\? parameter. This parameter will give the exit status of the last command that was called in the shell. With that said what the test command does is it, well, preforms some kind of test and then will exit with a status code of 0 if all goes well with that test, else it will exit with 1.
The test command by itself will not produce any output to the standard output of the bash console, so often it should be used in conjunction with a other commands and bash features with the special parameter that contains the exit status to produce some kind of output. However often when the test command is used it is when making bash scripts and thus it is just used as a way to make some kind of choice when it comes to doing something or not, as such that is likely why it will not produce any output unless something is done to make it do so.
To start out with in this section I will be going over a few simple examples of the Linux test command. Sense the command by itself will not produce any output to the standard output, many of these examples will also involve other bash features that might be worth looking into further also.
I often mention in these sections of posts that all the source code, in this case in the from of bash scripts can be found in my demos Linux repository in Github. That is where you can find the latest revisions of the examples in this post, that in some cases can be a little out of date here. Also that would be a good place to make a pull request with something.
To start out with the test command it might be best to just work out a few simple examples in the bash console. For this example I am just using the equal to expression with the test command. When doing so the exit status for the test command will be 0, or 1, but to see the output I will need to use a command link the Linux echo command.
Another option would be to use the and and or control operators. In the above example in which I am just using the Linux echo command I am using a semicolon control operator as a way to end the test command, and start a new command that is to just echo out the exit status of the last command which was the test. In this example I am just using the || and \&\& control operators for the sake of or and and operations.
One major part of getting into writing bash scripts is to make use of the if statement that is another way to go about using the test command. Although it might be best to start write files when it comes to this sort of thing, it is also possible to work with them write away by just typing something like this into the bash console.
One more way to go about using the test command would be to make use of redirection as a way to write some output to a file. This is yet another feature of bash that is worth looking into more at some time is it will come up a lot when it comes to bash script examples that have to do with creating files.
The test command is one of many commands that are built into the bash command. One way to confirm this would be to use another useful bash built in command called the Linux type command. This is just one thing that I like to check when looking into the various Linux commands to gain a sense of this is the kind of command that should be there to work with in just about all Linux system or not, and with that said the test command is one of those commands as it is built into bash.
Now that I have the very basics of the Linux test command out of the way, In this section I will be going over some of the expressions in greater detail. All the various expressions have to do with preforming some kind of comparison between two values, of checking if a file exists, or something to that effect. Keep in mind that the test command is not typically used by itself, but is used by way of conditional statements when writing bash scripts actually.
In the basic section above I have all ready covered the equal two expression. This is a typical expression that will be used often when writing bash scripts, and in programing in general actually. However in that example I was just using the -eq option which is used to check of two numbers equal each other or not. If you are like me and you are used to how this sort of thing working in javaSscript they you should be wondering about how to go about preforming equality in general when it comes to strings, and other types of values. For this there are other options such as = when it comes to string values rather than numbers.
So then there is testing if two number values equal each other, as well as doing the same for string values. However what about doing so for testing if two values are not equal to each other? When it comes to numbers there is the -ne option for test, and when it comes to string values != is what can be used to do so.
There are then expressions for greater than, less than, greater than or equal to, and less than or equal to. These are typical expressions to use when working out conditional statements, so this will often be used when writing bash scripts that call for the use of them. However there is also just quickly doing a few simple expressions in the bash console just for the sake of confirming how these work.
Greater than examples in the bash console
Less than examples in the bash console.
The test command can be used to check if a folder is there or not, for this I just need to use the -d option when calling the test command. So then this kind of test can be preformed to test if a folder exists or not, and in the event that it does not the mkdir command can be used to create it. However when it comes to just making sure that a path of folders exists in the event that it does not, just the mkdir command can be used alone with the -p option. So then this kind of test will typically just be used in bash scripts for the sake of doing something else when testing for the presence of a folder.
The test command can be used to check for symbolic links, also known as a soft link, by making use of the -h option and then a path to a file to check if it is a symbolic link or not. If you are not yet familiar with symbolic links they are a way of creating a file that is a link to another file or folder. There is much more to read about when it comes to this subject as there is a difference between symbolic links, and hard links. Hard links are another kind of link, but they do not point to a path, rather that actual data of the file in the file system, as such they continue to work even if the source file is deleted.
So then in this section I will be going over some examples of this kind of script, the nature of which is just making a script that will check if a folder contains 1 or more mark down files. In the event that there are more that one mark down files in the folder the script will exit with a status of 0, in the event that there are no mark down files the exit status will be 1. Of course there is writing bash scripts, but there is also writing these kinds of scripts in some other high level language in order to make the script more portable.
So first off there is of course getting into writing bash scripts when it comes to this sort of thing. The basic process of this is to just place bash commands into a file, then make the file executable with the chmod command, or call the file with the bash command. Even if I do just run the script with bash rather than calling it directly I still think it is a good idea to place a shebang that will point to the binary that will be used to run this script, in this case it would be the bash command.
So then to make a bash script that will check a folder for markdown files I will want to use the ls command with a positional parameter that is the folder to check for markdown files. I will then want to pipe that to grep and use that to get a filtered list of files that are just markdown files. The filtered list of markdown files can then in turn be piped into the Linux wc command with the -l option that will be the number of lines, which would then be the number of markdown files. I can then use a from of parameter expansion to set this result to a shell variable, and that variable can then be used with the test command, by way of an if statement to test if the result is greater than zero or not. I can also set a status shell variable and default the code to 1, in the event that the count is grater than 0 then I can set the value of the status variable to, you guessed it 0. At the end of the script I then just need to use the Linux exit command and pass the value of the status variable to it.
Now that I have my bash script file I will just need to give it a file name, and make the file executable. Once that is done I can call it from the bash console, and pass a path to a folder to check as the first and only positional parameter. The script will then wok just like the test command in that it will set the exit status code, but not print anything to the standard output by itself. So once again I will need to do something with the echo command to see the exit status code.
So then this bash script seems to work as I would want it to. I just pass a path to a folder, if there are one or more markdown files in that folder I get a 0 status, else 1. Although this script works great, it will only work in environments that have bash to work with. If I want to make a test like this a little more portable, when it comes to windows mainly, I will want to create this kind of test in some other language then.
Now that I have my script I can use it just like my bash script as I have placed the appropriate shebang for node rather than bash at the top if the source code file.
I have came up with a few simple C language source code files for my post on the Linux gcc command which can be used to compile binaries using C. I have been toying with the idea of writing a few posts on the C language just for the sake of learning at least a little about how to do something with a low level language. So then in this example I am once again creating the same simple test program, only this time it is in C so it can be compiled to its own standard alone binary rather than calling bash, node, or python to run the program.
Aside from that the program seems to work just as well as the others, in fact it is just as fast as bash when I use it sense there is no delay which seems to be the case with node.
I get around to editing and expanding my content now and then, I have some plans together when it comes to expanding this post. However if there is anything else that comes to mind there is the comments section below here than can be used as one way to bring something up with this.