Title: Introduction to UNIX Shells
1Introduction to UNIX Shells
2Agenda
- Languages Compiled vs Interpreted
- What is a SHELL
- The various Unix Shells
- Which Shell to Use ?
- Shell Basic Concepts
- Shell Scripting
- Resources
3Compiled vs Interpreted
4Compiled vs Interpreted (Contd.)
- Compiled Languages
- FORTRAN
- C/C
- Pascal
- Interpreted Languages
- Perl
- Tcl/Tk
- Unix Shell Scripts
5What 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
6What 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
7The 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
8More 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
9Confused! 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
10Basic 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
11Basic 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
12Other 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
13Scripting 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
14Executing 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
15Variables
- 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
16Variables (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.
17Variables (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!)
18Arguments
- 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
19Flow 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 ?
20Flow 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
21Other Flow Controls
while ... do ... done switch ltvargt
ltconst1gt) ... ltconst2gt) ... esac for
var in ltexpressiongt do done
22Example
- !/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
23Unix 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
24Things 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
25Elements of a Good Script
- Interpreter
- Comments
- Search Path
- Argument Checking
- Acceptable User
- Exit Status and
- Stdin, Stderr, and Stdout.
26The End