Close or Esc Key

Arduino Projects   |   Raspberry Pi   |   Electronic Circuits   |   AVR   |   PIC   |   8051   |   Electronic Projects

Getting Started with Shell Scripting - Linux Part 4

Written By: 

Hari Prasaath K.

In the previous tutorial, a Raspberry Pi desktop running Raspbian (a Debian based Linux Distribution) was successfully, setup. Now it's time to start shell scripting and get the hands dirty with the very first shell script.

On first time booting the Raspbian after installation, a pop up window will show up with welcome message.

Raspbian Welcome Window after installation

Fig.1: Raspbian Welcome Window after installation

Click Next and set Country, Language and Time Zone in the next window.

Setting Language, Country and Time zone in Raspbian Welcome Window

Fig. 2: Setting Language, Country and Time zone in Raspbian Welcome Window

Click 'Next' and set a password. The default username is 'pi' and password is 'raspberry'. Set any password of your choice.

Setting Root Login and Password in Raspbian Welcome Window

Fig.3: Setting Root Login and Password in Raspbian Welcome Window

Click 'Next' and connect to a Wi-Fi network if available. Finally Reboot Raspberry Pi.

    Raspbian Setup Complete

Fig. 4: Raspbian Ready to Use Now After Reboot

It is important to set the keyboard layout before anything. The default keyboard layout in Raspbian is English (UK) or language of the country specified in the welcome window. If using US keyboard (which is most commonly used around the world), navigate to Preferences -> Keyboard and Mouse. In the pop up window, click on Keyboard tab and then, Keyboard Layout. Select Layout to English (US). This is a very important step otherwise, the keyboard will type wrong characters on the Linux Console if set to a different keyboard layout. Done with that, let us get back to Linux.

In early versions of Linux, a user can work on Linux only through the Shell. Linux programmers, system users and system administrators used to enter shell commands (set of system commands on Linux) through a Command Line Interface. With the development of various desktop environments for Linux, the CLI for the Shell went hidden within the graphical desktop environments.

In modern Linux distributions, when Linux boot up, it creates five or six virtual consoles. Some Linux distributions can have more virtual consoles. The virtual consoles are terminal sessions that runs in Linux system memory. The terminal sessions are identified by the term 'teletypewriter' and are abbreviated as 'tty'. Different terminal sessions are identified by the term tty followed by a number. Like tty2 denotes terminal session 2 or teletypewriter 2. The virtual consoles can be seen as Command Line Interfaces outside the Linux GUI.  

In Raspbian, there are five virtual consoles or terminal sessions running after boot up. These virtual consoles can be loaded by pressing keys Ctrl + Alt + F2 to Ctrl + Alt + F6. Like on pressing Ctrl + Alt + F2, virtual terminal 2 (identified by tty2 on the terminal window) gets loaded. The user can return to graphical interface on Raspbian by pressing Ctrl + Alt + F7. On loading virtual console, command line interface for the respective terminal session occupies the entire screen where, user is prompted to enter root login and password. The default login (user) on Raspbian is 'pi' and default password is 'raspberry' unless it is changed in the welcome window. It should be noted that in virtual consoles, on typing password, nothing is showing typing on the screen unlike in graphical environments where '*' is commonly shown typing on screen for each character of the password. On entering correct root login and password, the terminal session is initiated where shell commands can be written and executed. This command line interface is called Linux Console.

The command line interface for a terminal session can also be started through Graphical Terminal Emulators. These are CLI interfaces within the Linux GUI. These are generally non-login console terminals where user does not need to enter login name and password. For example, Raspbian has LXterminal as the default graphical terminal emulator that can be launched by navigating to Accessories -> Terminal. Other popular graphical terminal emulator packages are Eterm, Final Term, Guake, GNOME terminal, Konsole Terminal, LillyTerm, mrxvt, rxvt, rxvt-unicode, ROXTerm, st, Sakura, Terminology, Terminator, tilda, Wterm, UXterm, xterm, xfce4 Terminal, Yakuake etc.   

In this series, the default graphical terminal emulation package of Raspbian i.e. LXterminal is used for entering the shell commands. It can be launched by navigating to Accessories -> Terminal.

Why Shell Scripting-

Shell Commands are the system commands in Linux system that direct the Linux operating system to perform various OS related tasks. The sequence of commands can be combined in a single file that would otherwise have to be passed to the system from a keyboard one at a time. These files are called shell script. Shell scripts are very useful in automating OS related tasks and functions. Shell scripts are written with similar logic as any other programming language. The commands coded in a shell script are interpreted by the Shell utility of the Linux system. The shell scripts are similar to batch files in DOS operating system and EXEC in IBM’s Mainframe VM operating system. So, shell scripting is a way to communicate with the Linux Operating System.

While high level languages have several restrictions, shell scripting is OS level programming with developer given the ultimate power to manage and control anything within the Linux system. Shell scripting can be used to write and implement system boot scripts, scripts automating operating system services and functions, managing application programs, system administration, server maintenance and many more things. While languages like C or C++ are good for lower level operations like manipulating data structures, invoking system calls etc, shell scripts are to directly manage system tasks, files, directories, databases and network functions.

While programming languages like C, C++, Java etc are compiler based languages, shell scripting is interpreter based. A code written in C or C++ is compiled to a binary executable file which is then run to execute the program. The compilation of a C or C++ source code will fail if there is an error in any line of code. The shell executes shell scripts one line after the other. So, if the second line of a shell script has an error, the first line will be already executed by the shell.  

The shell scripts are simple text files which can be easily accessed and viewed by a system administrator (user). These scripts are portable and can be transferred to any Unix or Unix-like system.    

What are Shell Scripts -

Shell scripts are simply text files (with .sh extension) that contain sequence of shell commands. These files can be interpreted by the Shell utility of the Unix/Linux system.

When to use shell scripts -

Shell scripting offer several advantages. They are easy to use. They provide OS level programming that gives direct access to the system services and resources. It requires no additional setup or tools unlike high level programming languages. The shell scripting is great tool for automating system tasks that may be repeatedly used or may be part of routine system administration.

However shell scripting is limited to system administration. It is not comparable to a high level programming language. Each line of shell script creates a new process in the Unix/Linux operating system. That is why, these scripts are slower in execution. The binary executable compiled from a C or C++ program file runs as a single process in the system. Secondly, Shell scripting is only for Unix/Linux based systems. It is not a cross platform programming language.

Shell scripting is also not suitable in many situations like for extensive mathematical operations, manipulating data structures, generating or manipulating graphics, extensive file operations, direct access to hardware resources (the device drivers are usually written in C or C++ for direct access of hardware devices), programming socket I/O or ports and wherever libraries or interfaces with legacy code are required. Since, shell scripts can be accessed and viewed by any system administrator, they are not suitable to write any closed source application. 

Various Shells -

There are many shell programs (shell utilities) available for Linux/Unix systems. Some of the popular shell programs include Sh, Csh, Ksh, Tcsh, Bash, Zsh, Pdksh. In Linux, Bash is the default shell. Since Raspbian is a Debian based Linux distribution, it also has Bash shell as the default shell.  It must be noted that syntax of all the shells is almost similar. There are only some minor differences in syntax for different shells. So, having worked on any shell, it is not difficult to script on any other shell utility.

Bash Shell, LXterminal, Raspbian -

In Raspbian, Bash is the default shell and LXterminal is the graphical terminal emulator package available as default. The LXterminal can be launched by navigating to Accessories -> Terminal. This opens a separate window within the Raspbian GUI which is the Linux Console. The shell commands can be written and executed in this console. The commands written and passed in the console are interpreted by the Bash shell.

Open LXterminal and write the following command –

echo $SHELL

Note that, $SHELL is in capital letters. The command will return the name of the shell as follow –

Bash Shell on Raspbian

Fig. 5: Checking Shell in Raspbian

Now let us check the current version of the bash shell in the Raspbian. Write the following command in terminal –

bash –version

The command returns the current version of the shell as follow –

Bash Shell Version on Raspbian

Fig. 6: Checking Bash Shell Version of Raspbian

 In the next tutorial, learn about bash commands and write your first shell script. 

.