Creating Bash Scripts On Linux – Part 1
Eventually,
as you advance with Linux Mint, you’ll want to start creating your own
automated tasks and programs. These are essentially scripts, Bash Shell scripts
to be exact, and they work in the same way as a DOS Batch file does, or any
other programming language.
A
Bash script is simply a series of commands that Mint will run through to
complete a certain task. They can be simple or remarkably complex, it all
depends on the situation.
Step
1
You’ll
be working within the Terminal and with a text editor throughout the coming
pages. There are alternatives to the text editor, which we’ll look at in a
moment but for the sake of ease, we’ll be doing our examples in Xed. Before you
begin, however, run through the customary update check: sudo
apt-get update && sudo apt-get upgrade.
Step
2
There
are several text editors we can use to create a Bash script: Xed, Vi, Nano,
Vim, GNU Emacs and so on. In the end it all comes down to personal preference.
Our use of Xed is purely due to making it easier to read the script in the screenshots
you see below.
Step
3
To
begin with, and before you start to write any scripts, you need to create a
folder where you can put all our scripts into. Start with mkdir
scripts, and enter the folder cd
scripts/. This will be our working folder and from
here you can create sub-folders if you want of each script you create.
Step
4
Windows
users will be aware that in order for a batch file to work, as in be executed
and follow the programming within it, it needs to have a .BAT file extension.
Linux is an extension-less operating system but the convention is to give scripts
a .sh extension.
Step
5
Let’s
start with a simple script to output something to the Terminal. Enter xed
helloworld.sh. This will launch Xed and create a file
called helloworld.sh. In Xed, enter the following: #!/bin/bash, then on a new line: echo Hello World!.
Step
6
The
#!/bin/bash line tells the system what Shell you’re
going to be using, in this case Bash. The hash (#) denotes a comment line, one
that is ignored by the system, the exclamation mark (!) means that the comment
is bypassed and will force the script to execute the line as a command. This is
also known as a Hash-Bang.
Step
7
You
can save this file, clicking File > Save, and exit back to the Terminal.
Entering ls, will reveal
the script in the folder. To make any script executable, and able to run, you
need to modify its permissions. Do this with chmod +x helloworld.sh. You need to do this with every script you create.
Step
8
When
you enter ls again, you can
see that the helloworld.sh script has now turned from being white to green,
meaning that it’s now an executable file. To run the script, in other words
make it do the things you’ve typed into it, enter:
./helloworld.sh.
Step
9
Although
it’s not terribly exciting, the words ‘Hello World!’ should now be displayed in
the Terminal. The echo command is responsible for outputting the words after it
in the Terminal, as we move on you can make the echo command output to other
sources.
Step
10
Think
of echo as the old BASIC Print command. It displays either text, numbers or any
variables that are stored in the system, such as the current system date.
Try
this example:
echo Hello World!
Today is $(date +%A).
The
$(date +%A) is calling the system variable that stores the current day of the
week.
0 Response to "Creating Bash Scripts On Linux – Part 1"
Post a Comment