Linux shell scripting using GNOME-Shell

Shell Scripting for beginners Part1 covered the execution of a Shell Script on WINDOWS platform with a comprehensive illustration. In this article let us practice shell scripting on LINUX platform. This is done using GNOME-shell for scripting and vim editor for modification and saving of shell scripts.

Open the terminal:  Go to Applications → Accessories → Terminal


Pre-requisite: Installation of GNOME-shell

COMMAND:

sudo apt-get install gnome-shell

OUTPUT: It takes a few seconds

[sudo] password for dell:

Reading package lists… Done

Building dependency tree…………..

After this install the vim editor using the command

sudo apt-get install vim


Creation and execution of a simple Shell Script

Let us start by creating a directory called shellscripts using mkdir command and start working there

Navigate to home directory using cd ~ command

cd ~
mkdir shellscripts
cd shellscripts

Now we are inside the shellscripts directory. Now create a new shell script called test.sh using the command:

vim test.sh

After giving vim command it creates a new file called test.sh. Press “INSERT” key to start scripting. The shell script starts with a “shabang”. In this program we display the username using $USER variable and echo command as below:

#!/bin/sh
clear
echo "Hello, $USER."

The screen should look as below:

Now save the file using ESC+w+q+! command

Grant permissions for execution of test.sh using the chmod command as below:

chmod +x test.sh

Execute the shell script using:

./test.sh

The output is as below:

Shell Scripting for beginners with a simple example Part 1

This article gives a practical exposure to beginners about shell scripting on UNIX platform. Shell script is a script for the shell command-line interpreter. Shell programming is fun and is used to automate highly repetitive time-consuming manual tasks like environment setup, post processing and configuration value changes that involve file manipulation. We can run shell scripts by installing Cygwin and use Emacs as the editor.

We point to the interpreter (i.e) the shell in the very first line of the script. This is called shebang. #! bin/sh is the default shebang if nothing is mentioned.

Writing a simple Shell Script:

Step1:  In the shell command prompt, navigate to your home directory

sh-3.2$ cd $HOME

cd means change directory. $HOME variable refers to a directory on the operating system containing the user’s files. It can be represented in short using ~ symbol.

Step2:  In the home directory create a new file named date.sh

Step3: Modify the access rights on the file so that you can edit/save it using chmod command.

sh-3.2$ chmod 777 date.sh

Everyone has read, write and execute permissions on the file

sh-3.2$ chmod 755 date.sh

Everyone can read and execute this file but I alone should be able to modify it.

Step 4: Now let us create a simple shell script to display the date and hostname.

We store the hostname value in a variable called HOST. We use the date function to display date in the format %m-%d%-%Y. And echo command is used for displaying/printing the output on the command line.

#! /bin/sh

HOST=$(hostname)

echo “——————————————————————-”

echo “Date:$(date +”%m-%d-%Y”)              Hostname:$HOST

echo “——————————————————————-”

Step 5: Run this shell script using the command ./date.sh from the home directory where you saved it.

OUTPUT:

sh-3.2$ ./date.sh

——————————————————————-

Date:12-14-2010              Hostname:dellwin7-PC

——————————————————————-