I U S &

advertisement
INTRODUCTION TO UNIX SHELL
&
SCRIPTING
1
6/28/2016
UNIX

The first version of Unix came from AT&T in the early 1970s

Operating system developed by programmers for programmers

Designed such that users can extend the functionality

To build new tools easily and efficiently

To customize the shell and user interface

To string together a series of Unix commands to create new functionality

To create custom commands that do exactly what we want
2
6/28/2016
UNIX ARCHITECTURE
User
Shell
Unix
Commands
Other
Applications
Kernel
Hardware
Database
Packages
Compilers
3
6/28/2016
WHAT IS A SHELL?

A shell is a command line interpreter

A shell:


Reads the command line

Interprets its meaning

Executes the command

Returns the result via the outputs
There are several Shells available, the most common are:

sh
Bourne Shell

ksh
Korn Shell

csh,tcsh
C Shell

bash
Bourne-Again Shell
6/28/2016
4
SHELL CUSTOMIZATION

Each user has a default shell which is specified in the configuration
file /etc/passwd

To know the current Shell (echo $SHELL )

To change the Shell (exec Shellname)

Shell is initialized by reading its overall configuration (in /etc/)

And by reading the user's own configuration (hidden files in user
directory)
5
6/28/2016
SHELL SCRIPT

What is a Shell Script?


Why use a Shell Script?




Simply a collection of operating system commands put into a text file in the
order they are needed to be execution by the Shell
Combine lengthy and repetitive sequences of commands into a single,
simple command
Generalize a sequence of operations on one set of data, into a procedure
that can be applied to any similar set of data
Create new commands using combinations of utilities
Example

Suppose beginning of each class I want to use these command
mkdir myclass
mypath=$(pwd)
cp /users/student/goodfile $mypath/myclass/.
And you would rather like to use a simple command
./class_setup file
Could do this with Shell script
6/28/2016
6
CREATING A SHELL SCRIPT

Shell scripts are simple text files created with text editor



Type the commands in the text file and save it
To execute the script


vi scriptname
./scriptname arguments
Make sure the script files are marked as executable

chmod a+x scriptname
7
6/28/2016
FUNDAMENTALS

A Shell script should start with
#!/bin/bash




#! tells OS to check what kind of file it is before attempting to exec it and
tells which utility to use (sh, csh, tcsh, …)
This followed by the absolute pathname of the program that should
execute the script
In a Shell Script a comment starts with #
Shell variables are variables that are created and assigned values
by user
desc=“Script to create files for the class”
8
6/28/2016
FUNDAMENTALS
#!/bin/bash
desc=“Script to create files for the class”
echo $desc
mkdir myclass
mypath=$(pwd)
cp /users/student/goodfile $mypath/myclass/.
9
6/28/2016
FUNDAMENTALS




Positional parameters $0-$9 represent the command and the
arguments
$0 represents the command
$1-$9 represents first to the ninth argument
Example:
class_setup file1 file2 file3 file4
#!/bin/bash
echo $1
Output: file1
10
6/28/2016
FUNDAMENTALS



$1-$9 allows you to access 10 arguments
How to access others?
Built-in command shift promotes each of the command-line
arguments.





The first argument ( which was $1) is discarded
The second argument ( which was $2) becomes $1
The third becomes the second
And so on
Repeatedly using shift is a convenient way to loop over all the
command-line arguments
11
6/28/2016
FUNDAMENTALS

Example:
sfind file1 file2 file3 file4
#!/bin/bash
echo $1
shift
echo $1
Output: file1
file2
12
6/28/2016
FUNDAMENTALS

Special parameters available are:

$* : treats the entire list of arguments as a single argument

[email protected] : produce a list of separate arguments

$# : returns the number of arguments

#? : returns the exit status of the execution
Zero means the execution was successful
Non Zero means a failure in the execution
13
6/28/2016
Download