loading...

Linux Mint – Introduction to scripting

How to Create MySQL Users Accounts and Grant Privileges

Now that you are beginning to grasp how to enter shell commands, it’s a good time to introduce scripting. Although this tutorial will not go into scripting in great detail, covering the very basics of it here will help you study scripting should you decide to do so.

Note

If you do decide to further your scripting skills (it’s a very useful skill for you to learn), there are many tutorials available that are dedicated to this very subject. One such tutorial is Linux Shell Scripting Cooktutorial, Second Edition, Shantanu Tushar and Sarath Lakshman, Packt Publishing

Bash scripts are actually just text files. So, to get started, you’ll need to fire up a text editor. You can use whichever text editor you’d like, such as nano (discussed earlier in this chapter) or even gedit, which is a graphical text editor that ships with Mint. Scripts are executed from the terminal, so you may want to use nano to edit your scripts as you will end up in the terminal at some point during this process anyway.

To get started, type the following command into your text editor:


#!/bin/bash

What you just typed is known as a hashbang. This is the first line that should be typed into any script you create. In the hashbang, we declare which interpreter we’d like to use to execute the commands that will follow. In most Linux distributions, Bash is primarily the only option, so you’re unlikely to diverge from this instruction.

Next, we can type any command we would like in the lines following. You can have a single command per line or as many commands as you wish.

For example, you can add the ls command to the next line. Now, your script will look as follows:


#!/bin/bash

ls

To execute the script, save it and then mark it as executable. To do so, exit the text editor, and execute the following command:


chmod +x myscript

Be sure to change myscript to whatever you named your script. Now, to execute it, simply type the following command:


./myscript

In the preceding command, we executed the script that we created. However, rather than just simply typing the name of the script, we prefixed it with a period and forward slash. The reason we did this is because by default, when you type a command (a script is treated as a command), the interpreter will look for the command to be located in folders such as /bin, /usr/bin, /usr/local/bin, and so on. By prefixing the name of the script with a period and forward slash, we’re telling the terminal emulator that we want to execute a command (script) that is stored in our current working directory. If you missed the period and forward slash, you would have received the following error:


bash: myscript: command not found

Once you execute the script, you should see the contents of your current directory. This is because the script simply executes the ls command. Unfortunately, this isn’t very useful, is it? It’s much easier to type ls than it is to type ./myscript. This script saves us no time at all. We can make a more useful script by removing ls from within the script and replacing it with the output of the sensors command. For example, try the following script instead:


#!/bin/bash

watch -n 0.1 sensors

Save the script with a simple name (such as cputemp) and mark it executable, as described earlier. Now, each time you execute ./cputemp, you’ll see an almost real-time representation of your CPU temperature. Now, this is useful.

From here, Bash scripting becomes more and more complex. In fact, there’s little that separates Bash from actual programming languages as you have access to utilize variables, branching statements, and many other advanced techniques to create programs that can automate complex tasks.

Comments are closed.

loading...