Week 8 Creating Simple Shell Scripts
Week 8 Creating Simple Shell Scripts
1
Chapter Objectives
2
Creating Shell Scripts
3
Creating Shell Scripts
4
Creating Shell Scripts
5
Creating Portable Shell Scripts
The problem is, that newer shell script syntax may not be
compatible when executed in older shells, and may actually may
not allow the shell script to properly run!
6
Creating Portable Shell Scripts
For Example:
#!/bin/bash <- Run in Bash Shell
#!/usr/bin/csh <- Run in C Shell
#!/usr/bin/ksh <- Run in Korn Shell
#!/usr/bin/sh <- Run in Bourne Shell
7
Creating Portable Shell Scripts
This special comment MUST be in the first line of the shell script,
and the #! symbol MUST be the first two characters on the line
(otherwise, it is treated like a REGULAR comment)
8
Creating Portable Shell Scripts
There are many text editors available such as nled, pico, emacs,
and vi.
USEFUL TIP:
9
Making Shell Scripts Executable
You cannot simply type the shell script by name to run the shell script.
There is an important factor to consider first:
chmod +x myShellScript.bash
./myShellScript.bash
Note: you can use ./ to force
the shell script to be run from the
current directory, or you can add the
directory pathname to the PATH 10
variable more on that later
Learning How to Learn
There are many resources to help you when you create a shell
script:
11
Learning How to Learn
For labs and assignments, you can use the man or info
commands in Linux (or perform a net search on the Web)
Eg. man k copy <- helps to search for commands for copy
12
Chapter Summary
A she-bang line at the top of the script forces (ensures) that the
shell script runs in the shell it was designed for, or does not
allow the shell script to run if shell is missing or pathname to
shell is incorrect (common error message: Bad Interpreter
13
Chapter Summary
You can either add the directory pathname into the PATH
environment variable to run the shell script, or you can force the
shell script to be run from the current directory by using the
relative pathname ./
14