27
Shell Scripting (Bash shell only) by - Akshay

Easiest way to start with Shell scripting

Embed Size (px)

Citation preview

Page 1: Easiest way to start with Shell scripting

Shell Scripting (Bash shell only)

by - Akshay

Page 2: Easiest way to start with Shell scripting

Agenda

In this part of presentation, We will cover…

• Introduction to shell programming,

• How to read/write/execute a script.

Page 3: Easiest way to start with Shell scripting

Assumptions

Before starting with this you should know-

• How to use text editor such as vi/vim,

• Basic Linux commands.

December 15

Page 4: Easiest way to start with Shell scripting

What is SHELL ?

December 15

• Shell is just an interface to access an operatingsystem

• Shell reads command from user and tells Linux OSwhat users want.

Page 5: Easiest way to start with Shell scripting

Shell

December 15

• Simply, the shell is a program that takesyour commands from the keyboard andgives them to the operating system toperform.

• In the old days, Shell was the only userinterface available on a Unix/Linuxcomputer. Nowadays, we have GUIs inaddition to the shell.

Page 6: Easiest way to start with Shell scripting

Some commands to play with shell

December 15

Tip: To find all available shells in your system type following command:

$ cat /etc/shells

Note that each shell does the same job, but each understand a different command syntax and provides different built-in functions.

****In MS-DOS, Shell name is COMMAND.COM which is also used for same purpose, but it's not as powerful as our Linux Shells are!

.

Page 7: Easiest way to start with Shell scripting

What is my current SHELL

December 15

Tip: To find your current shell type following command$ echo $SHELL

Hint: Bash which stands for Bourne Again SHell, an enhanced version of the original Bourne shell program, sh, written by Steve Bourne

Tip: To find process-id of current shell.

$ ps -p $$

Page 8: Easiest way to start with Shell scripting

Further information

December 15

To read more information –

$man bash

$info bash

Page 9: Easiest way to start with Shell scripting

Mr. Shell

December 15

Page 10: Easiest way to start with Shell scripting

What is Shell Script & Why should I learn scripting ??

December 15

Shell Script

basically scripts are collections of commands that arestored in a file. The shell can read this file and act on thecommands as if they were typed at the keyboard.

We have thousands of commands available for thecommand line user, Can we remember them all? Theanswer is, “No”. The real power of the computer is itsability to do the work for you. To get it to do that, weuse the power of the shell to automate things. We writescripts.

Page 11: Easiest way to start with Shell scripting

What are scripts good for?

December 15

A wide range of tasks can be automated. Here are some of the things I automate with scripts:

• On every 1st day of a month, linux-servers backup theirconfigurations and copy it to a “Window/Storage machine“, anddelete old backups on successful execution so that we always havelatest backup with us. This is performed by a script.

• A script on a single click automatically does health check-up onmultiple servers and reports the status with colors (Red-> Critical,Yellow-> Warning , Green -> OK) from all servers. A sample report isattached .(Click here to see)

• Script can sends us an email message if a process is down.

Page 12: Easiest way to start with Shell scripting

Choice is yours

December 15

OR

Page 13: Easiest way to start with Shell scripting

How to write shell script

December 15

Following steps are required to write shell script:

(1) Use any editor like vi to write shell script.(2) After writing shell script set execute permission for your script as follows-

Syntax:chmod permission your-script-name

Examples:$ chmod +x your-script-name$ chmod +x abcd.sh

Page 14: Easiest way to start with Shell scripting

How to execute your script.

Execute your script asSyntax:bash your-script-namesh your-script-name

ksh your-script-name./your-script-name

Examples:$ bash abc.sh$ sh abc.sh$ ./abc.sh

December 15

Page 15: Easiest way to start with Shell scripting

First Script

December 15

Before starting any thing , I will recommend every one to create a Linux Virtual machine on your laptop/PC . VM will be safe for testing . Do not try any script on production servers (If you face any difficulty while creating VM , let me know after this session )

Page 16: Easiest way to start with Shell scripting

First Script

• Login to your Linux machine, create a test directorywhich will hold all of our test scripts.

• In my case, I have created a directory called “test” ,which will hold my test scripts .

December 15

Page 17: Easiest way to start with Shell scripting

First Script

• To create a shell script, you use a text editor.

$ vi my_script

• Press “i” to enter in “insert mode” of vi editor

December 15

Page 18: Easiest way to start with Shell scripting

First Script

• Write logic of your script .

• Press “Esc” key twice to exit from “insert mode”.

• Type :wq! and hit the entre key to save your scriptand exit from vi-editor.

• Type “chmod +x my_script” to make it executable.

December 15

Page 19: Easiest way to start with Shell scripting

Explanation

#!/bin/bash

# My first script

echo "Hello World!"

echo "How are you.“

• The first line of the script is important. This is a special clue given to theshell indicating what “shell” is used to interpret this script. In this case, itis /bin/bash.

• The second line is a comment. Everything that appears after a "#" symbol isignored by bash.

• The last two lines are echo commands. This command simply prints what itis given on the display.

December 15

Page 20: Easiest way to start with Shell scripting

Explanation

December 15

We just covered “echo” command ; It is used to print given text on screen , along with this you can also use echo command to make Alarm which will makes a BEEP sound, you can also use echo command to print text in attractive colors.

Since , this session is limited to only introduction , we will learn this options in another session .

Colored output

Alarm with echo command

Page 21: Easiest way to start with Shell scripting

How to give your keyboard inputs to script ??

Read command is used to pass your keyboard inputs to script .

• Syntax:read your-variable-name

• Examples:$ read my_variable

Now to read the value stored in a variable use “echo” command.

• Syntax:echo $your-variable-name (Note: “$” before variable name)

• Examples:$ echo $my_variable

December 15

Page 22: Easiest way to start with Shell scripting

Second Script

• Now we will make a second script which will ask userto give some inputs and then it will print those inputsto screen.

#!/bin/bash

# My second script

echo “What is in your mind…."

read your_input

echo

echo "Your input was : $your_input"

December 15

Page 23: Easiest way to start with Shell scripting

How to hide your inputs ??

December 15

In the same direction , suppose ,script has asked you to enter your password

and you do not want it to be visible on screen while typing…. In this kind of

situation , everything will be same and you just need to put “-s” (suppress)

option with read command. .

#!/bin/bash

# My 3rd script

echo "Please enter your password"

read -s my_password

echo

echo "Yeah ! i have hacked your password : $my_password"

Page 24: Easiest way to start with Shell scripting

• In same direction , you can also set a timeout forusers to give there inputs, i.e. if your does not give hisinput in given time then , script will stop its executionafter timeout.

• Change command read your_input to read -t 10 your_input inthe 2nd script we made in this session , and see thedifference.

$ read –t time_in_second name_of_varibale

Example read -t 10 abc (timeout after 10 seconds)

December 15

Page 25: Easiest way to start with Shell scripting

Echo + read = (read –p)

• As we saw in previous 2 scripts , while giving inputs toscript from keyboard we used read and echocommand in pair. We can save one line if use “-p”(small “P”) with the read command.

$echo “What is in your mind”

$read my_input

$read –p “What is in your mind” my_input

December 15

Page 26: Easiest way to start with Shell scripting

Feel free to contact me

Akshay SiwalEmail :

[email protected]

December 15

Page 27: Easiest way to start with Shell scripting

Thank You

December 15