Basics of Shell Scripting

Shell Linux Bash scripting

Basics of Shell Scripting 

  • To get a Linux shell, you need to start a terminal.
  • To see what shell you have, run: echo $SHELL.
  • In Linux, the dollar sign ($) stands for a shell variable.
  • The ‘echo‘ command just returns whatever you type in.

The pipeline instruction (|) comes to rescue, when chaining several commands.

 Linux commands have their own syntax, Linux won’t forgive you whatsoever is the mistakes. If you get a command wrong, you won’t flunk or damage anything, but it won’t work.

#!/bin/sh – It is called shebang.

It is written at the top of a shell script and it passes the instruction to the program /bin/sh.

To learn more on Linux Shell Scripting please visit our Linux Shell Scripting course here.

About shell Script

Shell script is just a simple text file with “.sh” extension, having executable permission.

Process of writing and executing a script

  1. Open terminal.
  2. Navigate to the place where you want to create script using ‘cd‘ command.
  3. Cd (enter) [This will bring the prompt at Your home Directory].
  4. touch hello.sh (Here we named the script as hello, remember the ‘.sh‘ extension is compulsory).
  5. vi hello.sh (nano hello.sh) [You can use your favourite editor, to edit the script].
  6. chmod 744 hello.sh (making the script executable).
  7. sh hello.sh or ./hello.sh (running the script)

 

You can use Azure to create your own Linux servers. You can read Azure Introduction from here.

 

Writing the First script:

#!/bin/bash
# My first script echo
"Hello World!"

#!/bin/bash (is the shebang.)
# My first script (is comment, anything following '#' is a comment)
echo “Hello World!” #(is the main part of this script)

Output:

Writing the Second script:

#! /bin/bash
echo "Hello $USER"
echo "Hey i am" $USER "and will be telling you about the current processes"
echo "Running processes List"
ps

Output:

Writing the Third script:

Moving to, write our third and last script for this article. This script acts as an interactive script. Why don’t you, yourself execute this simple yet interactive script and tell us how you felt.

#! /bin/bash
echo "Hey what's Your First Name?";
read a;
echo "welcome Mr./Mrs. $a, would you like to tell us, Your Last Name";
read b;
echo "Thanks Mr./Mrs. $a $b for telling us your name";
echo "*******************"
echo "Mr./Mrs. $b, it's time to say you good bye"

Output: