One of the things about a Linux system is how easy it is to create and run Shell Scripts, finding good examples on the net isn’t. I needed a way to check if a directory exists before trying to create it and do work within that new directory. There are a lot of on-line communities dedicated to Linux topics. The trouble is that there is a lot of confusing advice. I believe the reason is that there are a lot of well meaning but inexperienced users on those sites giving out advice.
Those that do know what they are talking about, talk over peoples heads and get a little bent when they are not understood, after all it’s so obvious to them, they get it, why don’t you.? This I know because 25 years ago, I use to teach adult learners how to use computers at a community college. Patients is a virtue when imparting your knowledge to those who are feeling overwhelmed to begin with.
So here is what I learned to do for a basic Bash Shell Script to check if a directory exists and if not create it, then change directory locations to the new directory. As a primer, all scripts begin with the header of; #!/bin/bash’ This tells bash that this is a script and to execute each command one at a time until either the reserve word ‘exit’ is encountered, or the end of the file is reached. Bash will try to make sense of every word within the script and exit when an error is encountered.
Fortunately, bash will also indicate where the error was encountered and give some information relating to the encountered error. For text that is not meant to be executed by bash, lead the line with the pound sign; # and follow it with a space for easer reading. Now without further adieu, here is my script:
The first line identifies the file as a shell, the second describes my shell, the third is a command to clear the terminal window of any previous data, and the fourth moves the courser down one line from the absolute top of the terminal window.
#!/bin/bash # myTest script # This script checks to see if a directory exists, and if not creates it clear echo # The next lines convey my intentions: myString=$HOME'/bin' if [[ ! -d $myString ] ]; then mkdir $myString fi done
myString is a variable which is given a value, i.e. initialized with a text string. What is cool is that you don’t even have to tell bash that the variable is a string.
$HOME is understood by bash as being the users home directory, i.e. /home/mike By adding the string ‘/bin’ we give the full name of our intended new directory using a kind of shorthand.
if some condition, then, do something, fi (end if statement.) [] are used to enclose the arguments that are being evaluated by the if statement. ! is the logic NOT. -d tells bash what is being evaluated, this being a directory. So in short, my statement says; if the directory “/home/mike/bin” does NOT exist, then create “/home/mike/bin”. Notice that we don’t evaluate if the directory exists, only if it does not exist. This shortens the code needed saving us from having to add an “else” statement followed by more code.
Create the script and save it in your home folder as “mytest”. Next, you will need to make the script executable. You can do this by selecting the the file from any folder viewer, selecting properties. Under permissions, select “Allow executing file as program.” To run the script, open the terminal window and enter ./mytest
Until next time, happy coding.