Saturday, March 3, 2018

Learn Shell Scripting In One Day


Let’s learn “Shell Scripting". So, in this post I wanted to talk about “What is a shell script?" & "learner guide  to shell scripting with examples". Before jumping into learning on how to code and execute shell scripts, let’s talk a bit about “Linux” and history of Linux.

Linux is technically the kernel, so, there are other utilities such as GNU tools, GCC compiler etc,. which would ideally make a complete operating system (OS). That is the reason it is called GNU (GNUs not Unix) Linux not simply Linux. Ideally one must say 'GNU Linux' not just 'Linux'.  GNU Linux is one of the widely used OS in market, because it is open-source, so anyone could download/re-use the kernel, publish a new Linux distribution under the guidelines of GPL & Free Software Foundation.

Learn Shell Scripting -- Part 2



Conditional operations using “if…else…fi” statement

We could use “if” statement when it is required to perform a conditional based operation. So, if the condition is true then process a set of commands, otherwise, process some other commands/action. The syntax of this is:

if [ <conditional statement> ]
then
< statements >    <--------- these statements gets executed if condition is true.
else
< statements >   <--------- these statements gets executed if condition fails.
fi

Learn Shell Scripting -- Part 3


Let’s look into some of the commonly used built-in standard shell variables

In this section we are going to see some of the standard shell built-in variables which we generally use in shell scripting.


# echo $? →  this would show the exit status of the previous command run in shell (0 - success, anything other than 0 is considered as failure)

# echo $$ →  this shows current shell ID (when run inside a script this would print the PID assigned to the shell).

# echo $@ OR # echo $* →  this prints the arguments passed when called for execution.

# echo $# →  this would show up total number of arguments passed.

# echo $! →  this would report PID of previous background process.

# echo $_ → this would print last argument being used in the previous command in shell.