150 likes | 335 Vues
Introduction to Unix Shell & Scripting. 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
E N D
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
Unix Architecture User Shell Other Applications Unix Commands Kernel Hardware Database Packages Compilers
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 • kshKorn Shell • csh,tcsh C Shell • bash Bourne-Again Shell
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)
Shell Script • What is 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 • Why use a Shell Script? • 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 mkdirmyclass 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
Creating a Shell Script • Shell scripts are simple text files created with text editor • vi scriptname • Type the commands in the text file and save it • To execute the script • ./scriptname arguments • Make sure the script files are marked as executable • chmoda+xscriptname
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”
Fundamentals #!/bin/bash desc=“Script to create files for the class” echo $desc mkdirmyclass mypath=$(pwd) cp /users/student/goodfile $mypath/myclass/.
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
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
Fundamentals • Example: sfind file1 file2 file3 file4 #!/bin/bash echo $1 shift echo $1 Output: file1 file2
Fundamentals • Special parameters available are: • $* : treats the entire list of arguments as a single argument • $@ : 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