0% found this document useful (0 votes)
31 views

The Linux Shell Scripting

Uploaded by

yashodha.p
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

The Linux Shell Scripting

Uploaded by

yashodha.p
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Shell Programming

What Is a Shell?
● A shell is a program that takes commands typed by the
user and calls the operating system to run those
commands.

● A shell is a program that acts as the interface between


you and the Linux system, allowing you to enter
commands for the operating system to execute.

● Shell accepts your instruction or commands in English


and translate it into computers native binary language
Why Use Shells?
● You can use shell scripts to automate administrative
tasks.

● Encapsulate complex configuration details.

● Get at the full power of the operating system.

● The ability to combine commands allows you to create


new commands

● Adding value to your operating system.


Kind of Shells
● Bourne Shell

● C Shell

● Korn Shell

● Bash Shell

● Tcsh Shell
Changing Your Default Shell
● Tip: To find all available shells in your system type following
command:
$ cat /etc/shells

● The basic Syntax :


chsh username new_default_shell

● The administrator can change your default shell.


What Shell Does

Your Command Linux Converted to Binary


or Shell Script Shell Language By Shell

Now Linux Kernel


Understand Your Request
Example
ls 0100111001001
date BASH 1110011000111
cat file.txt 1111110111101

Linux Kernel

Shell is an command language interpreter that executes


commands read from the standard input device (keyboard) or
from a file.
The Shell as a Programming
Language

1. You can type a sequence of commands and allow


the shell to execute them interactively.

2. You can store those commands in a file that you can


then invoke as a program (shell script).
Why Shell Script ?

• Shell script can take input from user, file and output them
on screen.

• Useful to create our own commands.


Save lots of time.

• To automate some task of day today life.

• System Administration part can be also automated.


Practical examples where shell
scripting actively used:

1. Monitoring your Linux system.


2. Data backup and creating snapshots.
3. Find out what processes are eating up
your system resources.
4. Find out available and free memory.
5. Find out all logged in users and what
they are doing.
6. Find out if all necessary network
services are running or not.
Create a script
● As discussed earlier shell scripts stored in plain text file,
generally one command per line.
◦ vi myscript.sh

● Make sure you use .bash or .sh file extension for each script.
This ensures easy identification of shell script.
Setup executable permission
● Once script is created, you need to setup executable
permission on a script. Why?

◦ Without executable permission, running a script is


almost impossible.
◦ Besides executable permission, script must have a
read permission.

● Syntax to setup executable permission:


◦ $ chmod +x your-script-name.
◦ $ chmod 755 your-script-name.
Run/Execute a script
● Now your script is ready with proper executable
permission on it. Next, test script by running it.
◦ bash your-script-name
◦ sh your-script-name
◦ ./your-script-name

● Examples
◦ $ bash bar
◦ $ sh bar
◦ $ ./bar
Variables in Shell

In Linux (Shell), there are two types of variable:

◦ System variables - Created and maintained by Linux


itself. This type of variable defined in CAPITAL LETTERS.

◦ User defined variables (UDV) - Created and maintained


by user. This type of variable defined in lower letters.
User defined variables (UDV)
● To define UDV use following syntax:
◦ variable name=value
◦ $ no=10

● Rules for Naming variable name


1. Variable name must begin with Alphanumeric character or underscore
character (_), followed by one or more Alphanumeric character.
2. Don't put spaces on either side of the equal sign when assigning value
to variable.
3. Variables are case-sensitive.
4. You can define NULL variable
5. Do not use ?,* etc, to name your variable names.
Print or access value of UDV
● To print or access UDV use following syntax :
◦ $variablename.

● Examples:
◦ $vehi=Bus
◦ $ n=10

◦ $ echo $vehi
◦ $ echo $n
Cont…
● Don’t try
◦ $ echo vechi
◦ it will print vehi instead its value 'Bus‘.

◦ $ echo n
◦ it will print n instead its value '10‘.

● You must use $ followed by variable name.

You might also like