Unix Shell Programming Workshop
Unix Shell Programming Workshop
&
Scripting with csh/tcsh
Brief Unix History
Unix Shell & Flavor
CSH/TCSH Scripts
Unix Architecture
What is Shell?
Shell is
Customization of a Session
Each shell supports some customization.
User prompt
Where to find mail
Shortcuts (alias)
files
startup
Types of Shells
Interactive vs. Non-interactive; login or not
Interactive login shell started after login
Non-interactive shell
Popular Shells
sh
ksh
csh,tcsh
bash
Bourne Shell
Korn Shell
C Shell (for this course)
Bourne-Again Shell
Families of Shells
To check shell:
To switch shell:
Startup files
sh,ksh:
/etc/profile
(out-of-the-box login shell settings)
/etc/profile.local (addtnl. local system settings)
~/.profile
(addtnl. user customized settings)
~/.kcshrc (non-login shell user customization)
bash:
/etc/profile (out-of-the-box login shell settings)
/etc/bash.bashrc (out-of-box non-login settings)
/etc/bash.bashrc.local (global non-login settings)
~/.bash_profile
(login shell user customization)
~/.bashrc (non-login shell user customization)
~/.bash_logout (user exits from interactive login shell)
csh/tcsh:
/etc/login (out-of-the-box login shell settings)
/etc/csh.login (non-login shell customizations)
/etc/csh.login.local (global non-login settings)
~/.login: (login shell user customizations)
~/.cshrc: (non-login shell user customizations)
~/.cshrc.logout: (non-login shells at logout)
~/.logout: (read by login shells at logout)
A shell script is a file that contains commands that the shell can
execute.
Any commands you enter in response to a shell prompt.
A utility
A compiled program
Shell programming
Exec succeeds
System overlays the newly created subshell with the executable
program
The command is a shell script
Exec failed
The command is assumed to be a shell script
The subshell runs the commands in the shell.
Special sequence
Make a comment #
Comments make shell scripts easier to
Shell variables
Positional parameters
Special parameters
Such as
Positional Parameters
The command name and arguments are the
positional parameters.
Positional Parameters
Example:
Change
willow> cd 1
List
willow> ls -l display_5args
Change
the script
willow> ./display_5args 1 2 3 4 5
you are running script ./display_5args with parameter 1 2 3 4 5
Positional Parameters
Positional Parameters
Example:
Special Parameters
Useful values
Command-line arguments
Execution of shell commands
Can not change the value directly, like positional parameters
Special Parameters
The number of arguments: $#
willow> ./num_args 1
willow> ./num_args 2
this script is called with 1 arguments.
willow> ./num_args 0
this script is called with 1 arguments.
Special Parameters
Exit status: $?
willow> ls a
a: No such file or directory
willow> echo $?
2
willow> echo olemiss
olemiss
willow> echo $?
0
Summary
A shell is both a command interpreter and a programming
language.
Job control
Variables
Control-z/fg/bg/&
Local and environment variables
Declare and initialize a variable ( no type)
Export unset
Parameter expansion/variable
expansion/command/substitution/pathname expansion
Quote ( \ )
all but parameter, variable expansion and \
suppress all types of expansion
\ escaping the following special character
Summary
Shell parameters
HOME
PATH
PS1
SHELL
$0
$n
$*
$@
$#
$$
$!
$?
Summary
Special Characters
NEWLINE
;
()
&
|
>
>>
<
<<
*
?
\
` `
[]
$
.
#
&&
||
!