DOC PREVIEW
FSU COP 3353 - Lecture 7 Intro to Shell Scripts

This preview shows page 1-2-3-4 out of 12 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 12 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 12 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 12 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 12 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 12 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Lecture 7What is a shell script?Simple script (egshell.sh)‏Executing shell scriptsShell scriptsPrinting a line to standard outputShell Environment VariablesUser defined variablesShell variables (Bourne shell)‏Reading values into shell variablesShell argumentsExample using shell arguments1Lecture 7Introduction to Shell ScriptsCOP 3353 Introduction to UNIX2What is a shell script?•An executable file containing–Unix shell commands–Programming control constructs (if, then, while, until, case, for, break, continue, while, …)– basic programming capabilities (assignments, variables, arguments, expressions, …)•The file entries are the script•The file is interpreted rather than compiled and executed–The first line of the script indicates which shell is used to interpret the script3Simple script (egshell.sh)#!/bin/sh#this is the script in file egshell.shcaldatewho | grep liuexit•The “#!” is used to indicate that what follows is the shell used to interpret the script•The “exit” command immediately quits the shell script (by default it will also quit at the end of the file)4Executing shell scripts•sh myscript #uses Bourne shell•tcsh myscript #uses t-cshellNote that the above explicitly invoke the appropriate shell with the file containing the commands as a parameter. The file does not need to be executable.•You can also make the file executable and then simple run as a command–chmod 755 myscript (or chmod +x myscript)–myscript5Shell scripts•Advantages–Can quickly setup a sequence of commands to avoid a repetitive task–Can make several programs work together•Disadvantages –Little support for large and complicated programming semantics–Shell scripts need to be interpreted hence are slower programs•Which shell to use?–csh shell and tcsh shell are recommended for use at the command line–sh (Bourne) shell and bash shell are recommended for writing shell scripts–Examples will generally use the Bourne shell6Printing a line to standard output•Use the echo command to print a line to stdout•Form of command:echo <zero or more values>•Examplesecho “Hello World”echo “hello” “world” #two valuesecho hello #need not always use quotesecho “please enter your name”7Shell Environment Variables•These are variables provided as part of the shell’s operational environment•They exist at startup but can be changed•Examples are: USER, HOME, PATH, SHELL, HOSTNAME•The “setenv” command (in tcsh) is used to set these, for example, by:setenv PATH $PATH:/home/here/bin(this sets the PATH variable so that it’s current value is appended by :/home/here/bin)–Note that setenv is how tcsh sets the environment variables8User defined variables•You can also specify variables yourself and these can also be used inside a script•In tcsh, the “set” command is used to set a variable to a string value•Form:set <name> = <value>•Examples:set alpha = “any string”set beta = 3set mypath = /home/special/public_html•Once a variable has been defined, it’s value can be used by “dereferencing” it with $.ls –al $mypath •Note that using setenv or set without any parameters simply displays the current settings9Shell variables (Bourne shell)•Note that for all shells, variables need not be declared explicitly, but simply used•For the Bourne shell, the use is as follows (note that there should be no blanks before and after the equals sign and no need for the set command.•Form:<name>=<value>•Examplealpha=“hello world”beta=45echo $alpha $beta “third argument”•Note that $alpha is the value of the variable alpha10Reading values into shell variables•The read statement is used to read a line of standard input, split the line into fields of one or more strings, and assign those strings to shell variables. Any strings not assigned are assigned to the last variable.•Form:read <var1> <var2> … <varn>•Examplesread numread field1 field2 restread field1 field2 < ifile.txt11Shell arguments•Arguments on the command line can be passed to a shell script, just as you can pass command line arguments to a program•$1, $2, …, $9 are used to refer to up to nine command line arguments (similar to C’s argv[1], argv[2], …, argv[9]).•Note that $0 contains the name of the script (argv[0])•Example:shprog.sh john 40shprog.sh bob 45 “new york”12Example using shell arguments•Script:#!/bin/sh#script name is greet.sh#friendly display of today’s dateecho “Hello” $1 $2 “pleased to meet you”echo “The date is”dateexit•Usage:greet.sh john


View Full Document

FSU COP 3353 - Lecture 7 Intro to Shell Scripts

Download Lecture 7 Intro to Shell Scripts
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture 7 Intro to Shell Scripts and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 7 Intro to Shell Scripts 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?