SHELL PROGRAMMING
Introduction to Shell   <ul><li>You communicate with a UNIX system through a command program known as a  shell . </li></ul...
Introduction to Shell When you log in, you are in one of five shells. 1. Bourne Shll –  /bin/sh  – Steve Bourne (On AT & T...
Basics <ul><li>Definition:  </li></ul><ul><li>Shell is an agency that sits between the user and the UNIX system.  </li></u...
Shell Scripts Example: script.sh #! /bin/sh # script.sh: Sample Shell Script echo “Welcome to Shell Programming” echo “Tod...
Shell Variables Environmental Variables: Some global environment variables are, HOME  Path to your home directory HOST  Th...
Local Shell Variables A variable assignment is of the form  variable=value , but its evaluation requires the $ as prefix t...
<ul><li>Shell scripts accept arguments from the command line.  </li></ul><ul><li>Run non interactively  </li></ul><ul><li>...
Position variables
#! /bin/sh echo “Program Name : $0” echo “No of Arguments : $#” echo “Arguments are : $*” $ chmod +x 2.sh $ 2.sh A B C o/p...
Bourne Shell Programming <ul><ul><li>Control structures </li></ul></ul><ul><ul><ul><li>if … then </li></ul></ul></ul><ul><...
if … then <ul><ul><li>Structure </li></ul></ul><ul><ul><ul><ul><li>if  test-command </li></ul></ul></ul></ul><ul><ul><ul><...
for: Looping with a List for is also a repetitive structure.  Syntax: for variable in list  do Commands done  Note: list h...
A partial list of built-in <ul><ul><li>bg, fg, jobs job control </li></ul></ul><ul><ul><li>break, continue change the loop...
<ul><ul><li>kill sends a signal to a process or job </li></ul></ul><ul><ul><li>set  sets flag or argument </li></ul></ul><...
System Scripts <ul><ul><li>some system scripts  </li></ul></ul><ul><ul><ul><li>/etc/init.d/syslog </li></ul></ul></ul><ul>...
<ul><li>Powerful scripts can be built using the excellent features offered in shell programming. A preliminary insight on ...
Thank you
Upcoming SlideShare
Loading in...5
×

Shellscripting

1,136

Published on

LUG@IITD workshop presentation

Published in: Education, Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
1,136
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
65
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Shellscripting

  1. 1. SHELL PROGRAMMING
  2. 2. Introduction to Shell <ul><li>You communicate with a UNIX system through a command program known as a shell . </li></ul><ul><li>The shell interprets the commands that you type on the keyboard. </li></ul><ul><li>There are many different shells available for UNIX computers, and on some systems you can choose the shell in which you wish to work. </li></ul><ul><li>You can use shell commands to write simple programs (scripts) to automate many tasks </li></ul>
  3. 3. Introduction to Shell When you log in, you are in one of five shells. 1. Bourne Shll – /bin/sh – Steve Bourne (On AT & T UNIX) 2. Bourne Again SHell – /bin/bash – Improved Bourne shell (On Linux) 3. C Shell – /bin/csh – Bill Joy (On BSD UNIX) 4. TC Shell – /bin/tcsh (On Linux) 5. Korn Shell – /bin/ksh – David Korn (On AT & T UNIX) The system administrator determines which shell you start in.
  4. 4. Basics <ul><li>Definition: </li></ul><ul><li>Shell is an agency that sits between the user and the UNIX system. </li></ul><ul><li>Description: </li></ul><ul><li>Understands all user directives and carries them out. </li></ul><ul><li>Processes the commands issued by the user. </li></ul>
  5. 5. Shell Scripts Example: script.sh #! /bin/sh # script.sh: Sample Shell Script echo “Welcome to Shell Programming” echo “Today’s date : `date`” echo “This months calendar:” echo “My Shell :$ SHELL”
  6. 6. Shell Variables Environmental Variables: Some global environment variables are, HOME Path to your home directory HOST The hostname of your system LOGNAME The name you login with PATH Paths to be searched for commands SHELL The login shell you’re using PWD Present working directory
  7. 7. Local Shell Variables A variable assignment is of the form variable=value , but its evaluation requires the $ as prefix to the variable name. count=5 echo $count total=$count You can assign value of one variable to another variable echo $total Note: There should not be any space around =. i.e. if we say x =5 then the shell interprets x as command running with the =5 as argument!
  8. 8. <ul><li>Shell scripts accept arguments from the command line. </li></ul><ul><li>Run non interactively </li></ul><ul><li>Arguments are assigned to special shell variables (positional parameters). </li></ul><ul><li>Represented by $1, $2, etc; </li></ul>Position variables
  9. 9. Position variables
  10. 10. #! /bin/sh echo “Program Name : $0” echo “No of Arguments : $#” echo “Arguments are : $*” $ chmod +x 2.sh $ 2.sh A B C o/p  Program Name : 2.sh No of Arguments : 3 Arguments are : A B C Position variables
  11. 11. Bourne Shell Programming <ul><ul><li>Control structures </li></ul></ul><ul><ul><ul><li>if … then </li></ul></ul></ul><ul><ul><ul><li>for … in </li></ul></ul></ul><ul><ul><ul><li>while </li></ul></ul></ul><ul><ul><ul><li>until </li></ul></ul></ul><ul><ul><ul><li>case </li></ul></ul></ul><ul><ul><ul><li>break and continue </li></ul></ul></ul>
  12. 12. if … then <ul><ul><li>Structure </li></ul></ul><ul><ul><ul><ul><li>if test-command </li></ul></ul></ul></ul><ul><ul><ul><ul><li> then </li></ul></ul></ul></ul><ul><ul><ul><ul><li>commands </li></ul></ul></ul></ul><ul><ul><ul><ul><li>fi </li></ul></ul></ul></ul><ul><ul><ul><ul><li>Example: </li></ul></ul></ul></ul><ul><ul><ul><ul><li>if test “$word1” = “$word2” </li></ul></ul></ul></ul><ul><ul><ul><ul><li>then </li></ul></ul></ul></ul><ul><ul><ul><ul><li>echo “Match” </li></ul></ul></ul></ul><ul><ul><ul><ul><li>fi </li></ul></ul></ul></ul>
  13. 13. for: Looping with a List for is also a repetitive structure. Syntax: for variable in list do Commands done Note: list here comprises a series of character strings. Each string is assigned to variable specified.
  14. 14. A partial list of built-in <ul><ul><li>bg, fg, jobs job control </li></ul></ul><ul><ul><li>break, continue change the loop </li></ul></ul><ul><ul><li>cd, pwd working directory </li></ul></ul><ul><ul><li>echo, read display/read </li></ul></ul><ul><ul><li>eval scan and evaluate the command </li></ul></ul><ul><ul><li>exec execute a program </li></ul></ul><ul><ul><li>exit exit from current shell </li></ul></ul><ul><ul><li>export, unset export/ remove a val or fun </li></ul></ul><ul><ul><li>test compare arguments </li></ul></ul>
  15. 15. <ul><ul><li>kill sends a signal to a process or job </li></ul></ul><ul><ul><li>set sets flag or argument </li></ul></ul><ul><ul><li>shift promotes each command line argument </li></ul></ul><ul><ul><li>times displays total times for the current shell and </li></ul></ul><ul><ul><li>trap traps a signal </li></ul></ul><ul><ul><li>type show whether unix command, build-in, function </li></ul></ul><ul><ul><li>umask file creation mask </li></ul></ul><ul><ul><li>wait waits for a process to terminate. </li></ul></ul><ul><ul><li>ulimit print the value of one or more resource limits </li></ul></ul>A partial list of built-in
  16. 16. System Scripts <ul><ul><li>some system scripts </li></ul></ul><ul><ul><ul><li>/etc/init.d/syslog </li></ul></ul></ul><ul><ul><ul><li>/etc/init.d/crond </li></ul></ul></ul>
  17. 17. <ul><li>Powerful scripts can be built using the excellent features offered in shell programming. A preliminary insight on shell programming has been offered here. </li></ul><ul><li>Scripts can make use of these as well as advanced features to help programmers/users of system to work with efficiency and ease. </li></ul>conclusions:
  18. 18. Thank you
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×