1 / 13

Introduction to Unix Shell & Scripting

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

chibale
Download Presentation

Introduction to Unix Shell & Scripting

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to Unix Shell&Scripting

  2. 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

  3. Unix Architecture User Shell Other Applications Unix Commands Kernel Hardware Database Packages Compilers

  4. 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

  5. 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)

  6. 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

  7. 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

  8. 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”

  9. Fundamentals #!/bin/bash desc=“Script to create files for the class” echo $desc mkdirmyclass mypath=$(pwd) cp /users/student/goodfile $mypath/myclass/.

  10. 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

  11. 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

  12. Fundamentals • Example: sfind file1 file2 file3 file4 #!/bin/bash echo $1 shift echo $1 Output: file1 file2

  13. 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

More Related