Agenda Introduction to Shell Scripts Purpose of Shell Scripts Types of Shells / Running Shells...

12
Agenda Introduction to Shell Scripts Purpose of Shell Scripts Types of Shells / Running Shells Creating Simple Shell Scripts: Creating Portable Shell Scripts Commenting Running Shell Scripts

Transcript of Agenda Introduction to Shell Scripts Purpose of Shell Scripts Types of Shells / Running Shells...

Page 1: Agenda Introduction to Shell Scripts Purpose of Shell Scripts Types of Shells / Running Shells Creating Simple Shell Scripts: Creating Portable Shell Scripts.

Agenda

Introduction to Shell Scripts Purpose of Shell Scripts Types of Shells / Running Shells Creating Simple Shell Scripts:

Creating Portable Shell Scripts Commenting Running Shell Scripts

Page 2: Agenda Introduction to Shell Scripts Purpose of Shell Scripts Types of Shells / Running Shells Creating Simple Shell Scripts: Creating Portable Shell Scripts.

Shell Scripts Unix Shell Scripts are simply files that

contain Unix commands that can be run or “issued” as a command (program).

Purposes of using Shell Scripts: Automate Repetitive Tasks (instead of

manually issuing commands manually). Create new commands or utilities (with

specified arguments including options) to meet the system administrator’s needs.

Page 3: Agenda Introduction to Shell Scripts Purpose of Shell Scripts Types of Shells / Running Shells Creating Simple Shell Scripts: Creating Portable Shell Scripts.

Types of Shells The Shell is a command interpreter that

allows the user or administrator to issue commands.

Since the development of time, many shells have been created which provide features for the user or administrator.

Although this course concentrates on the bash shell, there are many other shells available.

Page 4: Agenda Introduction to Shell Scripts Purpose of Shell Scripts Types of Shells / Running Shells Creating Simple Shell Scripts: Creating Portable Shell Scripts.

Types of Shells Several Type of Shells are

displayed below: Bourne Shell

First shell created when Unix OS was created. Although considered to be “out-dated” all newer shells allow user to use Bourne Shell syntax thus making it a very “portable” (i.e. easily run on other shells)

Page 5: Agenda Introduction to Shell Scripts Purpose of Shell Scripts Types of Shells / Running Shells Creating Simple Shell Scripts: Creating Portable Shell Scripts.

Types of Shells C Shell

Shell that has many features for C programmers.

Korn Shell Incorporates features from C Shell as well as

improved mathematical processing features (eg. “built-in functions”) that was not available with Bourne Shell. Common shell used for programmers in Unix OS. The Linux equivalent of the Korn Shell is the “Z Shell”.

Page 6: Agenda Introduction to Shell Scripts Purpose of Shell Scripts Types of Shells / Running Shells Creating Simple Shell Scripts: Creating Portable Shell Scripts.

Types of Shells Bash Shell

Bash Shell is an acronym for “Bourne-again Shell” (a little “techie” humour). This shell is considered to encapsulate all of the best features of the Bourne, C, and Korn Shells.

Also, the Bash Shell is considered to be a “user-friendly” command interpreter with more intuitive command editing capabilities (such as up-arrow for displaying previously-issued commands)

Page 7: Agenda Introduction to Shell Scripts Purpose of Shell Scripts Types of Shells / Running Shells Creating Simple Shell Scripts: Creating Portable Shell Scripts.

Running Shells Although when using Tux, the Bash Shell is

the default shell when you log onto your account, you can run many of the above-mentioned shells. (if in doubt, use the which command to locate pathname of shell).

Shell PathnameBash /usr/bin/bashKorn /usr/bin/ksh and/or /usr/bin/zshC /usr/bin/cshBourne /usr/bin/sh

Page 8: Agenda Introduction to Shell Scripts Purpose of Shell Scripts Types of Shells / Running Shells Creating Simple Shell Scripts: Creating Portable Shell Scripts.

Creating Portable Shell Scripts

One common problem when creating and running shell scripts is what happens if the type of shell that shell script was “designed-for” is not available on the operating system?

The next screen shows a technique to instruct the shell script to run in a “specified” shell or do not run if shell is not present.

This technique is called creating a portable shell script (eg. portable Bash shell script, portable C shell script, portable Korn shell script, etc…)

Page 9: Agenda Introduction to Shell Scripts Purpose of Shell Scripts Types of Shells / Running Shells Creating Simple Shell Scripts: Creating Portable Shell Scripts.

Creating Portable Shell Scripts

On the first line of the shell script, you can choose which shell in which to run the script:

First Line Shell#!/bin/bashBash (in Phobos #!/bin/bsh)#!/bin/ksh Korn#!/bin/csh C#!/bin/zsh Z#!/bin/sh

Warning: # must be located in first column of first line and ! Must be located in second column of first line immediately followed by correct pathname or shell is run in “default” shell!

Page 10: Agenda Introduction to Shell Scripts Purpose of Shell Scripts Types of Shells / Running Shells Creating Simple Shell Scripts: Creating Portable Shell Scripts.

Commenting Shell Scripts It is a good idea to provide comments

near the top of your shell script and comments throughout your shell script to help explain what the shell script does.

Remember: You may post this shell script on the Internet for others to use, or you may no longer be in charge of the shell script, so others need to view comments to quickly understand how shell scripts works.

Page 11: Agenda Introduction to Shell Scripts Purpose of Shell Scripts Types of Shells / Running Shells Creating Simple Shell Scripts: Creating Portable Shell Scripts.

Commenting Shell Scripts A comment is a hash symbol # Any text after the hash symbol # is ignored by the

shell for the remainder of the line.

Examples:

# This shell script does…

mkdir mydir # Command to create directory “mydir”

If the command #! to specify which shell to run shell script in is not contained in first and second columns of first line respectively, the # is treated by shell like a comment and shell script is run in “default” shell!

Shell ignores text

Page 12: Agenda Introduction to Shell Scripts Purpose of Shell Scripts Types of Shells / Running Shells Creating Simple Shell Scripts: Creating Portable Shell Scripts.

Running Shell Scripts There are two methods of running shell

scripts: Type the shell type with the name of the shell

script as an argument

eg. bash script-name

Type the name of the shell script. This requires that the shell script file has at least read and execute permissions.

Eg. script-name