Introduction to UNIX Shells - PowerPoint PPT Presentation

1 / 26
About This Presentation
Title:

Introduction to UNIX Shells

Description:

Sun Proprietary/Confidential: Internal Use Only. Introduction to ... netbeans sfw. bash-3.00# PS1='Hello There ' Hello There whoami. root. Hello There ksh ... – PowerPoint PPT presentation

Number of Views:60
Avg rating:3.0/5.0
Slides: 27
Provided by: forum5
Category:

less

Transcript and Presenter's Notes

Title: Introduction to UNIX Shells


1
Introduction to UNIX Shells
  • Exclusively for VTU

2
Agenda
  • Languages Compiled vs Interpreted
  • What is a SHELL
  • The various Unix Shells
  • Which Shell to Use ?
  • Shell Basic Concepts
  • Shell Scripting
  • Resources

3
Compiled vs Interpreted
4
Compiled vs Interpreted (Contd.)
  • Compiled Languages
  • FORTRAN
  • C/C
  • Pascal
  • Interpreted Languages
  • Perl
  • Tcl/Tk
  • Unix Shell Scripts

5
What is a SHELL ?
  • A program that allows User Interaction with OS
  • Interacts with Terminals
  • Allows user to execute Unix utilities
  • Provides an interpreted language
  • Scripting of common user tasks
  • Much more than a DOS Batch file

6
What is a SHELL ? (Contd.)
  • Interface between Operating System and User

Interaction
Scripting
Our Friendly Shell
Unix Utilities/Daemons (ls, cp, cron etc.)
System Libraries
KERNEL
7
The Various Unix Shells
  • Bourne Shell (sh)
  • Stephen Bourne at Bell Labs 1977
  • Good for portable scripting
  • C Shell (csh)
  • Bill Joy at Berkeley (SUN cofounder, vi guy)
  • C Like Syntax
  • Korn Shell (ksh)
  • David Korn at ATT Bell Labs
  • Major enhancement over Bourne Shell

8
More Unix Shells
  • Tenex C Shell (tcsh)
  • Enhancement over C Shell
  • Good for interactive use
  • Bourne Again Shell (bash)
  • GNU OpenSource Shell
  • Has some features of KSH
  • Good for interactive use

9
Confused! What do I Use ?
  • Scripting
  • sh Portable Scripting
  • ksh Advanced Scripting mostly portable
  • Interactive Use
  • csh Basic interactive use
  • tcsh Enhanced interactive features over csh
  • bash Effective for interactive use
  • Do NOT use csh/tcsh for scripting!
  • Can use bash for scripting but mostly non-portable

10
Basic Concepts
bash-3.00 echo SHELL /usr/bin/bash bash-3.00
pwd /opt bash-3.00 ls csw onbld SUNWits
SUNWspro netbeans sfw bash-3.00
PS1"Hello Theregt " Hello Theregt
whoami root Hello Theregt ksh exit Hello
Theregt
The Shell PROMPT
Which SHELL ?
Where am I ? (Directory)
Which files are here ?
Change the boring PROMPT
Which User ?
Lets run Korn Shell
Back to Bash
11
Basic Concepts (Contd.)
Which Operating System ?
Hello Theregt uname SunOS Hello Theregt echo
1210 Hello Theregt history 1 echo SHELL 2
pwd 3 ls 4 PS1"Hello Theregt " 5 whoami 6
ksh 7 uname 8 echo 9 history
Current shell's Process ID
List out the previous executed commands
12
Other Shell Features
  • Piping Send output of one command to input of
    another
  • ls sort Gives a sorted list of files
  • Shell special variables
  • ? Exit status of last command
  • ltcmdgt Execute ltcmdgt in background
  • Shell builtins
  • jobs List all background commands
  • env List all environment variables
  • CTRLC Terminate current command
  • CTRLR Search in the history list

13
Scripting For the Shell
  • Any collection of shell commands that can be
    stored in a file and executed is called a Shell
    Script.
  • Scripts have variables and flow control
    statements like other programming languages.
  • Variables set var, typeset var, var...
  • Arrays set -A arr, arr1one, arr2two
    ...
  • Flow Control if-then-else, while, for, switch
  • Functions funcname () ...
  • Arithmetic expr, (( ... ))
  • Much more powerful than DOS batch files
  • ksh provides most features full programming
    language

14
Executing Scripts
  • There are two ways to execute a shell script
  • Direct Interpretation
  • ksh scriptfile args ...
  • Indirect Interpretation
  • First line of the scriptfile must be
  • !/usr/bin/ksh
  • ...
  • Make the scriptfile executable
  • chmod x scriptfile
  • Execute it
  • ./scriptfile

15
Variables
  • Local Variables
  • Specific to a Shell Function
  • funcname ()
  • typeset var1 var2
  • ...
  • Global Variables
  • Available throughout the Shell Script
  • Not available outside the Shell Script
  • Declared outside of any function

16
Variables (Contd.)
  • Environment Variables
  • Available outside of shell scripts
  • Set systemwide and is available to all programs
    and shell scripts
  • Can be called Super-Global Variables
  • Set at the shell prompt
  • bash-0.3 varnamevalue
  • bash-0.3 export varname
  • Set in /etc/default/init (on Solaris) for
    systemwide availability.
  • Set in /home/username/.login, /home/username/.prof
    ile or .bashrc (rc-file) for per-user
    configuration.

17
Variables (Contd.)
  • Be Careful!
  • No spaces between variable name and sign
  • varname value
  • Having spaces gives an unhelpful error. Correct
    syntax is
  • varnamevalue
  • To set per-user values interactively, edit your
    rc-file and execute
  • on bash source .profile or .bashrc
  • on sh/ksh . ./.profile (Note the dots!)

18
Arguments
  • Arguments can be passed to a shell script
  • Arguments can be passed to a shell function
  • Accessed using
  • ltnumbergt
  • 0 Special, name of function or script
  • 1 .. 9 Positional arguments
  • - Number of positional arguments
  • shift Used to access positional arguments
    beyond 9th position

19
Flow Control
  • If Statements Syntax
  • if ltlogical expressiongt
  • then
  • ....
  • fi
  • Logical expression is also called a test
    condition
  • -e ltfilenamegt Does file exist ?
  • -x ltfilenamegt If file executable ?
  • - d ltpathnamegt Is the pathname a directory ?

20
Flow Control Example
  • !/bin/ksh
  • if -lt 2
  • then
  • echo Must give one parameter!
  • else
  • if -e 1
  • then
  • echo File already exists!
  • exit
  • fi
  • touch 1
  • chmod x 1
  • vim 1
  • fi

21
Other Flow Controls
while ... do ... done switch ltvargt
ltconst1gt) ... ltconst2gt) ... esac for
var in ltexpressiongt do done
22
Example
  • !/bin/ksh
  • list all files and directories
  • for fn in ls
  • do
  • if -f fn then
  • echo f is a file
  • elif -f fn then
  • echo f is a directory
  • fi
  • done

23
Unix Philosophy
  • Make each program do one thing well. To do a new
    job, build afresh rather than complicate old
    programs by adding new features.
  • - Doug McIlroy

24
Things to keep in mind
  • Do NOT Develop as root user
  • Log into the system as a normal unprovileged user
  • Open a separate terminal window in graphical mode
  • Make sure you are in your home directory
  • You can use vi, vim, gvim, gedit etc to edit your
    script

25
Elements of a Good Script
  • Interpreter
  • Comments
  • Search Path
  • Argument Checking
  • Acceptable User
  • Exit Status and
  • Stdin, Stderr, and Stdout.

26
The End
Write a Comment
User Comments (0)
About PowerShow.com