shell scripting
DESCRIPTION
basic idea about shell scripting...TRANSCRIPT
NATIONAL INSTITUTE OF TECHNOLOGY
JAMSHEDPUR
Computer Science and Engineering Dept.
SHELL SCRIPTING
by
KUMAR SOURABH
c.s.e
241/10
INDEX:
• What is Shell
• How Shell Works
• How Shell Better
• Type of Shell
• What is Shell Script
• Why We Write Shell Script
• How We Run Shell Script
• Variable in Shell
• Problem Statement
Shell is a agency that sits between the user and the UNIX
system
Shell is an command language interpreter that read
commands from the standard input device (keyboard) or
from a file and execute them
Shell is not part of system kernel, but uses the system kernel to execute programs, create files etc.
WHAT IS SHELL?
WHAT IS SHELL ?
USER2
HOW SHELL WORKS?
Shell is a agency that sits between the user and the UNIX system
Computer understand the language of 0's and 1's called binary language. which is difficult for all of us, to read and write. So In OS there is special program called Shell. Shell issues the prompt and waits for us to enter a command
After a command is entered , the shell scans the command line for meta- character and expands abbreviation (like * in rm *)to recreate a simplified command line
It then passes command line to kernel for execution
Now the shell waits for the command to complete and normally it can’t do any work while the command is running
After command execution is complete , shell returns to its waiting role to start the next cycle . Now we are free to enter another command
However we can change this behavior and instruct not to wait
HOW SHELL WORKS?
Our
command
or
Shell script
Linux
Shell
Converted to
Binary language
by Shell
Now Linux
Kernal
Understad
Our
Request
ls
date
time BASH 00101000
10101011
01010010
LINUX
KERNEL
Example:
Linux Shell is similar to Windows command prompt
but are much more powerful.
For example:
On Linux it’s quite feasible to have multiple shells
installed, with different users able to pick the one they
prefer. :input and output can be redirected using <and >
$ ls -l > lsoutput.txt
HOW SHELL IS BETTER
Sh – simple shell
BASH – Bourne Again Shell
KSH – Korne Shell
CSH – C Shell
SSH – Secure Shell
To use a particular shell type the shell name at the
command prompt.
Eg $csh – will switch the current shell to c shell
To view the available shells in the system, type cat
/etc/shells at the command prompt
To view the current shell that is being used, type echo
$SHELL at the command prompt
TYPE OF SHELL-1
TYPE OF SHELL-2
Shell Name Developed
by Where Remark
BASH ( Bourne-
Again SHell )
Brian Fox
and Chet
Ramey
Free
Software
Foundation
Most common shell in
Linux.
CSH (C SHell) Bill Joy University of
California
The C shell's syntax and
usage are very similar to
the C programming
language.
KSH (Korn SHell) David Korn AT & T Bell
Labs
Used in Commercial
Application
Shell Script is series of command written in plain
text file.
Shell script is just like batch file in MS-DOS but
have more power than the MS-DOS batch file.
shell script is also known as shell program
WHAT IS SHELL SCRIPT
Shell script can take input from user, file and output
them on screen.
Useful to create our own commands.
Minimizes typing of repetitive command
Save lots of time.
Can schedule jobs to run in the system
To automate some task of day today life.
System Administration part can be also automated.
WHY TO WRITE SHELL SCRIPT
HOW TO WRITE SHELL SCRIPT
$ vi first
#!/bin.sh
#
# My first shell script
#
clear
echo “Time nd Tide wait 4 no one"
Explanation:
Script Command(s) Meaning
$ vi first
vi editor
#
# My first program
#
# followed by any text is considered as
comment
echo “Time nd Tide wait 4 no one” To print message or value of variables
on screen, we use echo command
Syntax:
echo "Message"
HOW TO RUN SHELL SCRIPT
Run our script as
Syntax: ./your-shell-program-name
For e.g.
$ ./first
In Linux (Shell), there are two types of variable:
System variables-: Created and maintained by Linux
itself and defined in CAPITAL LETTERS.
User defined variables (UDV)-: Created and
maintained by user. This type of variable defined in
lower letters.
VARIABLE IN SHELLS
SYSTEM VARIABLES :
System Variable Meaning
1. BASH=/bin/bash Our shell name
2. HOME=/home/kumar Our home directory
3. PWD=/home/students/Common Our current working directory
4. USERNAME=kumar User name who is currently login to PC
5. SHELL=/bin/bash Our shell name
TEST OPERATORS
Test Works in 3 Ways:
o Numeric comparison
o String comparison
o File tests
EXAMPLE OF NUMERIC COMPARISON
$ a=5; b=7; c=7.2
$test $a –eq $b ; echo $?
1
$test $a -lt $b ; echo $?
0
$test $c –gt $b ; echo $?
1
$test $c –eq $b ; echo $?
0
EXAMPLE OF FILE TESTS
$ ls –l emp.txt
-rw-rw-rw- …
$ [ -f emp.txt ] ; echo $?
0
$ [ -x emp.txt ] ; echo $?
1
$ [ -d emp.txt ] || echo “file is not a directory”
file is not a directory
STRING TEST
Test
True if
S1=s2 String s1=s2
S1!=s2 String s1 is not equal
to s2
-n stg String stg is not null
string
-z stg String stg is null
string
Conditions (Case)
case expr in Option1) stmt ;; Option2) stmt ;; *) stmt ;; esac Every option should be terminated with a double semicolon. Denotes default case Case should be termniated with esac
ARRAY
Initialising an array A[0] = 10 A[1] = Hi
Using an array ${A[0]} : retrieves 10
Here arrays can contain data belonging to different data types Uninitialised index in arrays will have null value by default
FUNCTION
Local Function Written at the command prompt Lasts for the current session alone
Global Function Written in .bashrc file Available as long as the definition is there in .bashrc file
Function in shell script Available with in the file alone
ALIAS
Alias – Alternate name for an entity Entity here refers to command We can give another name or alias name for a command either at the command prompt or in the .bashrc file. The former will be temporary and will vanish if the session ends and the latter will be permanent as long as the definition exists in the .bashrc file
Alias and Unalias
alias newname=oldname
Eg alias copy=cp
Then we can use copy in the same way we use cp
command
Eg copy file1 file2 //copies content of file1 to file2
To remove alias use unalias command
unalias copy
After this we cannot use copy to perform copying
function
export : EXPORTING SHELL VARIABLES
Reference:
1. A Practical Guide to Ubuntu Linux
by MARK G SOBEL
2. Linux Foundation Organisation
Thank you!!