shell scripting
DESCRIPTION
basic idea about shell scripting...TRANSCRIPT
![Page 1: Shell scripting](https://reader034.vdocuments.net/reader034/viewer/2022052307/54b2ed3c4a795936078b4588/html5/thumbnails/1.jpg)
NATIONAL INSTITUTE OF TECHNOLOGY
JAMSHEDPUR
Computer Science and Engineering Dept.
![Page 2: Shell scripting](https://reader034.vdocuments.net/reader034/viewer/2022052307/54b2ed3c4a795936078b4588/html5/thumbnails/2.jpg)
SHELL SCRIPTING
by
KUMAR SOURABH
c.s.e
241/10
![Page 3: Shell scripting](https://reader034.vdocuments.net/reader034/viewer/2022052307/54b2ed3c4a795936078b4588/html5/thumbnails/3.jpg)
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
![Page 4: Shell scripting](https://reader034.vdocuments.net/reader034/viewer/2022052307/54b2ed3c4a795936078b4588/html5/thumbnails/4.jpg)
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?
![Page 5: Shell scripting](https://reader034.vdocuments.net/reader034/viewer/2022052307/54b2ed3c4a795936078b4588/html5/thumbnails/5.jpg)
WHAT IS SHELL ?
USER2
![Page 6: Shell scripting](https://reader034.vdocuments.net/reader034/viewer/2022052307/54b2ed3c4a795936078b4588/html5/thumbnails/6.jpg)
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
![Page 7: Shell scripting](https://reader034.vdocuments.net/reader034/viewer/2022052307/54b2ed3c4a795936078b4588/html5/thumbnails/7.jpg)
HOW SHELL WORKS?
Our
command
or
Shell script
Linux
Shell
Converted to
Binary language
by Shell
Now Linux
Kernal
Understad
Our
Request
![Page 8: Shell scripting](https://reader034.vdocuments.net/reader034/viewer/2022052307/54b2ed3c4a795936078b4588/html5/thumbnails/8.jpg)
ls
date
time BASH 00101000
10101011
01010010
LINUX
KERNEL
Example:
![Page 9: Shell scripting](https://reader034.vdocuments.net/reader034/viewer/2022052307/54b2ed3c4a795936078b4588/html5/thumbnails/9.jpg)
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
![Page 10: Shell scripting](https://reader034.vdocuments.net/reader034/viewer/2022052307/54b2ed3c4a795936078b4588/html5/thumbnails/10.jpg)
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
![Page 11: Shell scripting](https://reader034.vdocuments.net/reader034/viewer/2022052307/54b2ed3c4a795936078b4588/html5/thumbnails/11.jpg)
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
![Page 12: Shell scripting](https://reader034.vdocuments.net/reader034/viewer/2022052307/54b2ed3c4a795936078b4588/html5/thumbnails/12.jpg)
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
![Page 13: Shell scripting](https://reader034.vdocuments.net/reader034/viewer/2022052307/54b2ed3c4a795936078b4588/html5/thumbnails/13.jpg)
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
![Page 14: Shell scripting](https://reader034.vdocuments.net/reader034/viewer/2022052307/54b2ed3c4a795936078b4588/html5/thumbnails/14.jpg)
HOW TO WRITE SHELL SCRIPT
![Page 15: Shell scripting](https://reader034.vdocuments.net/reader034/viewer/2022052307/54b2ed3c4a795936078b4588/html5/thumbnails/15.jpg)
$ vi first
#!/bin.sh
#
# My first shell script
#
clear
echo “Time nd Tide wait 4 no one"
![Page 16: Shell scripting](https://reader034.vdocuments.net/reader034/viewer/2022052307/54b2ed3c4a795936078b4588/html5/thumbnails/16.jpg)
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"
![Page 17: Shell scripting](https://reader034.vdocuments.net/reader034/viewer/2022052307/54b2ed3c4a795936078b4588/html5/thumbnails/17.jpg)
HOW TO RUN SHELL SCRIPT
Run our script as
Syntax: ./your-shell-program-name
For e.g.
$ ./first
![Page 18: Shell scripting](https://reader034.vdocuments.net/reader034/viewer/2022052307/54b2ed3c4a795936078b4588/html5/thumbnails/18.jpg)
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
![Page 19: Shell scripting](https://reader034.vdocuments.net/reader034/viewer/2022052307/54b2ed3c4a795936078b4588/html5/thumbnails/19.jpg)
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
![Page 20: Shell scripting](https://reader034.vdocuments.net/reader034/viewer/2022052307/54b2ed3c4a795936078b4588/html5/thumbnails/20.jpg)
TEST OPERATORS
Test Works in 3 Ways:
o Numeric comparison
o String comparison
o File tests
![Page 21: Shell scripting](https://reader034.vdocuments.net/reader034/viewer/2022052307/54b2ed3c4a795936078b4588/html5/thumbnails/21.jpg)
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
![Page 22: Shell scripting](https://reader034.vdocuments.net/reader034/viewer/2022052307/54b2ed3c4a795936078b4588/html5/thumbnails/22.jpg)
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
![Page 23: Shell scripting](https://reader034.vdocuments.net/reader034/viewer/2022052307/54b2ed3c4a795936078b4588/html5/thumbnails/23.jpg)
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
![Page 24: Shell scripting](https://reader034.vdocuments.net/reader034/viewer/2022052307/54b2ed3c4a795936078b4588/html5/thumbnails/24.jpg)
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
![Page 25: Shell scripting](https://reader034.vdocuments.net/reader034/viewer/2022052307/54b2ed3c4a795936078b4588/html5/thumbnails/25.jpg)
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
![Page 26: Shell scripting](https://reader034.vdocuments.net/reader034/viewer/2022052307/54b2ed3c4a795936078b4588/html5/thumbnails/26.jpg)
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
![Page 27: Shell scripting](https://reader034.vdocuments.net/reader034/viewer/2022052307/54b2ed3c4a795936078b4588/html5/thumbnails/27.jpg)
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
![Page 28: Shell scripting](https://reader034.vdocuments.net/reader034/viewer/2022052307/54b2ed3c4a795936078b4588/html5/thumbnails/28.jpg)
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
![Page 29: Shell scripting](https://reader034.vdocuments.net/reader034/viewer/2022052307/54b2ed3c4a795936078b4588/html5/thumbnails/29.jpg)
![Page 30: Shell scripting](https://reader034.vdocuments.net/reader034/viewer/2022052307/54b2ed3c4a795936078b4588/html5/thumbnails/30.jpg)
export : EXPORTING SHELL VARIABLES
![Page 31: Shell scripting](https://reader034.vdocuments.net/reader034/viewer/2022052307/54b2ed3c4a795936078b4588/html5/thumbnails/31.jpg)
![Page 32: Shell scripting](https://reader034.vdocuments.net/reader034/viewer/2022052307/54b2ed3c4a795936078b4588/html5/thumbnails/32.jpg)
Reference:
1. A Practical Guide to Ubuntu Linux
by MARK G SOBEL
2. Linux Foundation Organisation
![Page 33: Shell scripting](https://reader034.vdocuments.net/reader034/viewer/2022052307/54b2ed3c4a795936078b4588/html5/thumbnails/33.jpg)
Thank you!!