Hey Guys ‘n’ Gals! Welcome to part 2 of the LinuxCandy Shell Scripting Tutorials! Its been quite some time, and I’ve been dying to post this one! Some really good suggestions were given in the last post, and I’ll be doing some more basics in this post.
What will we learn today?
In today’s post, we will learn a little bit about the plethora of text editors available for you in Linux. This post is pretty important, as learning the intricacies of terminal-based editing is a very useful skill when you learn to use shell scripting. When I am done with you guys, you ought to be able to edit files in the terminal, even in your sleep!
Lets Get Cracking Then . . . .
Editing is one of the most common things that you, as a shell scripting programmer will be doing. Most of you guys are probably using the popular gui-based tool called G-edit in Gnome based systems, or K-write if using KDE. However, since your work involves deploying your scripts from the terminal itself, don’t you think it will be better to be able to edit directly from the terminal itself?
I Hear You, So What Next?
When it comes to terminal-based text editing, there are some tools that rule the roost : vi Editor (improved version is called vim or vi-Improved), nano & cat. From what I have seen, for beginners, the easiest would be nano, while vim has a steep learning curve, and is not so easy for beginners to work with. However, vim is quite powerful, and so is nano, and in the end, its a personal choice. I use both nano and vi, and am partial to nano for reasons that will become obvious in the coming sections.
So, vim, eh?
Vi editor, is a really powerful text editor and is pretty much all you need to start shell script writing.
This is how your terminal will look when you type “vi” or “vim” in your shell prompt. This screen welcomes you and provides a set of basic commands that you can use to work with vim. However, as enthusiastic as you may be, refrain from typing them out directly. there are certain modes of vim that you need to know before starting:
ESC key is your best friend…
The Escape Key is the most useful key to help you get out of a sticky situation. In case you find that your key presses are not doing what you think they should, pressing the escape key brings you into the normal mode, where you can decide what to do next.
a. Normal Mode: This is the mode in which vim usually opens. You use this mode to go into the other modes. Pressing the Escape key will bring you to the normal mode.
b. Command Mode: In this mode, you enter the various commands that can be used with vi. The command mode is just an extension of the normal mode. Also called as the last line mode, as the commands you enter in this mode are visible in the bottom most line of the editor. the commands may be saving, exiting or other specific commands.
c. Insert Mode: This is the mode in which you enter the text that forms the document you are creating. All the lines of code are to be entered in this mode.
Now that all that boring stuff is over, lets get to the interesting part. Lets open/create a file in vi
we open the file as shown in this screenshot. Note that vi will open the file if it already exists, and will create an empty new file if it does not exist.
Once it opens, you will find yourself unable to type anything. That is because you are in normal mode. Enter into insert mode by pressing any of the following keys:
i - Simply inserts text at cursor position (Most common button you will use)
I – Inserts the new text at the beginning of the line on which your cursor is positioned.
a - This adds text after the cursor.
A – This one adds your text at the ending of the line on which your cursor is positioned.
o – Opens up a new line below the current line for you to add the new text.
O – Opens up a new line above the current line for you to add the new text.
s - Substitutes the letter underneath the cursor with letter you type to insert text.
S or c – This Deletes the current line and substitutes it with text that you then type.
R or C - This one replaces the current text with whatever text you then type.
Other than these options, you could use the [INSERT] key on your keyboard to enter the text. Take care, however, as the INSERT key acts as a toggle between [INSERT] and [REPLACE], as shown in the 2 screenshots below [Screenshot3,4]
This key first enters into INSERT mode when pressed once, and if pressed once more, starts replacing the text with new text.
Point to Ponder over: Once you start using vi for coding, you will often find yourself correcting the first line because you forgot to enter into inert mode before typing. For example, in C/C++ you would usually start with “#include<*.h>” but when you forget to enter into insert mode, you will find that your line looks like “nclude<*.h>“. Why? Because Vim did not recognize “#”, but took the next letter “i”, as a command to enter insert mode, and the rest is history. Take care about that.
Now lets type up a simple shell script that greets the user. Type out the shell script as shown here in the vi editor in Insert mode.
#!/bin/bash echo hello world echo "Welcome to the Shell Scripting Tutorial" echo "We hope, dear $USER, that you will enjoy your stay"
Now that the text is all typed, we need to save it. Do this by pressing the ESC key to enter normal mode, and then press “:” (colon) to enter the command mode. You will notice that the “:” symbol will show up at the bottom-most line of the window. This means that the vim editor is expecting a command from you.
The Saving/Exiting commands are:
:w - Save the current file.
:w <filenename> – Save the current contents into a new file. (Does not save changes made to original file)
:q – Quit vim
:wq – Save and then Quit vim
:q! – Quit without saving any changes made since last save.
That is the basic stuff completed. Lets now move on to the other popular editor, nano. We will learn more about vim as we go along.
Lets save and exit vim with the command
Here comes Nano!
Open the same text file now in nano. This can be done as you usually would:
When Nano opens up, we can directly set about typing the text without worrying about modes. Just set your cursor to the spot you want, and type away! Also, nano is helpful in that it provides a set of commands at the bottom, for quick reference.
Add the following parts at the end of the script.
echo "your current working directory is `pwd`" echo "Thank you for running this script!"
A little bit about the script: In this Shell Script, we have used the word “$USER” which is a special variable. In shell scripting, a variable’s content can be obtained by using the ‘$’ symbol as a prefix to the variable. For example, “echo $HOME” would display the current user’s home directory path, and “echo $USER” would display the username of the current user who runs the script. That shows how your script can be used in a flexible way, without having to type out specific scripts for each user. Also, note that we have used double quotes for the second echo statement, but not for the first. These work the same way, but using double quotes with echo is generally good programming practice. We will learn about weak and hard quoting in the upcoming parts of this series. BackQuote can be used to nest one command in another command, as shown in the line 5, where “pwd” command is nested in the echo. Go ahead, run this and see what you get.
Yes, both vim and nano right now look as colored as a black and white pic, but you can change all that. both these powerful editors can recognize code and color the different parts in a logical fashion. Here’s how to do this
In normal mode, type
Enabling this feature in nano is a little different:
Firstly, open the file “/etc/nanorc” in your favorite editor.
Find the commented lines(starting with #) that begin with programming language names, as shown here
And uncomment the languages for which you need syntax highlighting. For shell Scripting, uncomment the Bourne Shell Scripts line, which goes
Thats it! Syntax Color Highlighting will now be enabled!
The next time you open a file in nano, if you use a dark color combo, you may not like the color highlighting. This can be remedied by changing the terminal profile preference as shown here
Note that both vi and nano detect code content even in files that have a txt extension! Pretty powerful, eh?
So here’s how to do this in both the editors
In normal mode, type
:set nu! or :set nonu
While opening a file, open it with the -c switch, like below:
nano -c <filename>
You can create a new file by using the command
Looking forward to your comments below!