Wednesday, August 22, 2018

Starting Shell Scripting

In a high level overview, shell scripts can be created as text files with any text editor, either command line  like nano, vi, cat,etc which can be accessed only with terminal windows or with GUI tools like gedit and other third party tools.

The following command will create a new file called file_name
using the cat command:
 cat > file_name
line 1
line 2
line 3
< Ctrl + D will save the file >

The following command will copy the string Hello World to the
hello.c file:
echo "Hello World" > hello.c

The command echo with > overwrites the content of the file. If content already exists in the file, it will be deleted and new content will be added in the file. In a situation, when we need to append the text to the file, then we can use the echo command as follows:

echo "Hello World" >> hello.c 

The following command will display the content of the file on screen:
 cat hello.c

Once the file is created, it is generally given an extension of .sh to distinguish it with other files.It is not mandatory to give an extension in Linux, but it is just an ease of convenience.

 Shell scripts contains series of commands which can be executed one after other.And we can embed complex logic to change the flow of execution.


Let's create a shell script.

#!/bin/bash
# This is comment line
echo "Hello World"
ls
date

The #!/bin/bash line is called the shebang line. The combination of the characters #
and ! is called the magic number. The shell uses this to call the intended shell such as
/bin/bash in this case. This should always be the first line in a Shell script.

Any line starting with #, will be treated as a comment line. An exception to
this would be the first line with #!/bin/bash

The echo command will print Hello World on the screen. The ls command will display directory content on the console. The date command will show the current date and time

We can execute the newly created file using.
bash hello.sh

Or we can directly execute it using
./hello.sh

If it displays as permission denied.Add execute permissions to it using
chmod +x hello.sh 

Let's see a bit more advanced script where we will use variables(storage locations for values at runtime).




#!/bin/bash
echo "Hello $LOGNAME, Have a nice day !"
echo "Your are working in directory `pwd`."
echo "You are working on a machine called `uname -n`."
echo "List of files in your directory is."
ls # List files in the present working directory
echo "Bye for now $LOGNAME. The time is `date +%T`!"

Here, you can observe I have used some strings with starting $, they are termed as variables where we will store any values for the session.Some are available with Operating System.As mentioned in my previous post.

$LOGNAME refers to current user through which script is launched.

uname is the command display different OS specific details based on its parameters. Here, we used -a  to display machine name.

To discuss bit more details about shell scripting, it is an interpreted language.And the interpreter we used is bash.

When we use a compiler-based language, we compile the complete source code, and
as a result of compilation, we get a binary executable file. We then execute the binary
to check the performance of our program.

On the contrary, when we develop the Shell script, such as an interpreter-based
program, every line of the program is input to Bash shell. The lines of Shell script are
executed one by one sequentially. Even if the second line of a script has an error, the
first line will be executed by the shell interpreter.

Shell scripts have certain advantages over compiler-based programs, such as C or
C++ language. However, Shell scripting has certain limitations as well.You can get more details about the difference in any forum.




No comments:

Post a Comment