The journey begins!
Get to know the basics like:
- granting the execute right to scripts
- Discovering why we need this all
This not being my first attempt to explain shell scripting to someone I will try and answer the most obvious question first.
This is something that I get asked over and over again, especially from the windows user community. In today’s age of touch interfaces and the emergence of virtual and augmented reality.
Why do we still need a CLI interface?
Because it is repeatable on a system level.
Just think about it.
You write it once and execute it indefinitely. There is no propriatary software or recorder between you and the system.
In a GUI driven operating system the only way to interact with the computer is to click on something. And maybe after clicking on something you type into something.
This is extremely simple to do for a human, but quite hard for a program.
If you would like to repeat the clicks you would first have to find a program that can interact with your special brand of window manager and then explain to him what to do. Most probably this will be handled in the form of click events that will be triggered on a predefined target on the GUI.
This already sounds like a lot of trouble. Now imagine that you have to vary your inputs depending on some arbitrary conditions an you are facing an interesting problem.
On the other hand using a script we could vary our input depending on any number of conditions. For example we could open chrome with a different page if a file exists or not.
I know that this is an arbitrary sample but it shows that we can repeat the same “script“ with a different outcome depending on external factors.
There is a quite extensive definition provided by wikipedia and I encourage you to read it. But for my purposes I understand scripts as the following.
A script is a sequential program written in a scripting language
Now lets take this apart.
Funny enough that wikipedia does not have a dedicated article on this subject matter. There is a redirect for sequential programming but it only links to an article about concurrent programming, which is the polar opposite.
There is actually no uniform definition provided by a credible source that I could find. But you could peace it all together with the following search terms:
Sequential being the core adjective here.
In short I would describe this as
Executing a predetermined sequence of instructions
An except of the wikipedia definition reads:
A scripting or script language is a programming language that supports scripts; programs written for a special run-time environment that automate the execution of tasks that could alternatively be executed one-by-one by a human operator. Scripting languages are often interpreted (rather than compiled).
The rest can be found here.
In my humble opinion a scripting language must be interpreted and not compiled. So that we can make changes and improvements on the fly without the need for a full development environment.
Now after a long expositional dump we will start with the article proper.
First of all this little school will only handle scripting in the shell. This brings uns the term shell scripting which can be defined as
A shell script is a computer program designed to be run by the Unix shell, a command-line interpreter. The various dialects of shell scripts are considered to be scripting languages.
So lets start with the first line of every script file.
Shebang should be the first line of the script file and denotes the interpreter to use.
This will instruct the OS to use the sh interpreter to run the script.
Depending on the interpreter you want to use this line may vary. A very good definition and explanation ca be found at wikipedia.
Everything after this first initial line will be handled in the upcoming tutorials. So for the sake of getting this long article done we will assume that we have written a script and now want to run it. But to do so we have to grant it execution rights.
If we do not give scripts the execute right we are hindering oneself in the long run. Because now we have to specify the execution environment every time we execute the “script”.
Without the execute right we have to
After we grant execution permissions, assuming that the script file starts with a shebang, we only have to do
So how do we do this? Simple. Using the chmod command we can grand execute rights to any script file in two ways.
This will grand execute permissions to my_script.sh.
To remove the execute permission just run
This will grant execute permissions to my_script.sh but keep the edit permission with the owner.
This are the few things we have to know up front to enjoy the coming tutorials.
To better follow I have setup a GitHub repository to hold all the samples and transcripts.