DOC PREVIEW
FSU COP 4342 - Shell Programming and Unix Utilities

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

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 8 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 8 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 8 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Shell Programming and Unix Utilities●Creating Shell Scripts●Variables, Arguments, and Expressions●Shell I/O and Debugging●Testing Conditions and Control Statements–test, if, case, while, until, for, break, continue ●Exit Status●Command Substitution●Regular Expressions and Utilities–grep, wc, touch, awk, tr, sort, gtbl, groff, ghostview, cut, head, tail, sed, gnuplot Shell Scripts (35.2)●Advantages of schell scripts.–Can very quickly setup a sequence of commands to avoid a repetitive task.–Can easily make several programs work together to meet a set of goals.●Disadvantage of shell scripts.–Little support for programming in the large.–Shell scripts are much slower since they are interpreted.What Shell to Use? (27.3)●The csh (C) shell and its derivative (tcsh) are recommended for use at the command line.●The sh (Bourne) shell and its derivatives (ksh, bash) are recommended for writing shell scripts.–All shell script examples in this course are given using the Bourne shell syntax.Finding Information about the Bourne Shell and Its Commands●Type “man <command>” or “info <command>” where <command> is the Bourne shell command of interest.●Type “man sh” and look in the manual page.●Look up information in the text or use other texts.●See me or the TA.Creating a Shell Script (35.1, 36.2)●General convention is to use a .sh extension (<name>.sh) for shell scripts.●Type in the first line of each shell script file: #!/bin/sh●Type in comments after this to indicate the purpose of the shell script. # This shell script is used to create a new # schema.●Change the permissions on the schell script so it can be executed by typing the name of the file. chmod +x <name>.shPrinting a Line to Standard Output●Use the echo command to print a line to stdout.form: echo <zero or more values>exs: echo “Hello World!” echo “Please enter a schema name:”Implementation of EchoShell Variables (35.9)●Are just used and not declared.●General convention is to use lowercase letters for shell variable names and uppercase letters for environment variable names.●Shell variables are assigned strings, which can be interpreted as numeric values. Note that there can be no blanks before or after the '='. form: <name>=<value> exs: var=0 var=”Good”Shell Variables (cont.)●Shell variables are initialized to be the empty string by default.●Shell variables can be dereferenced. form: $<name> exs: var1=$var2 echo “====” $testcase “====” echo “deleted” $num “records from” $schemaReading Values into a Shell Variable (35.18)●Use the read statement to read a line of standard input, split the line into fields of one or more strings, and assign these strings to shell variables. All leftover strings in the line are assigned to the last variable.form: read <var1> <var2> ... <varm>exs: read num read name field1 field2 read name1 name2 < input.txtShell Arguments (35.20)●Can pass arguments on the command line to a shell script, just like you can pass command line arguments to a program. nd.sh employee Intel●$1, $2, ..., $9 can be used to refer to up to nine command line arguments (argv[1] ... argv[9]).●$0 contains the name of the script (argv[0]). Example Using Shell Arguments●Example shell script called prompt.sh: #!/bin/sh echo “Please enter fields for the” $1 “schema.”●Example usage: prompt.sh Intel●Example output: Please enter fields for the Intel schema.More on Cmd Line Args (35.20)●$# contains the number of command line arguments.●$@ will be replaced with a string containing the command line arguments.●Example shell script called echo.sh. #!/bin/sh echo "The" $# "arguments entered were:" $@●Example usage: echo.sh cat dog horse●Example output: The 3 arguments entered were: cat dog horseDebugging Shell Scripts (35.25, 37.1)●Shell scripts are interpreted by the shell.–Syntax errors are not detected until that line is interpreted at run time.–There is no debugger.●You can invoke your shell script with command line flags to assist in debugging.–The -v flag indicates the verbose option and the shell prints the commands as they are read. –The -x flag causes the shell to print the interpolated commands after they are read.Debugging Shell Scripts (cont.)●Invoke your shell script as follows so that each line of the script is echoed as it is interpreted.form: sh -xv <scriptname>ex usage: sh -xv echo.sh a b cex output: #!/bin/sh echo "The" $# "arguments entered were:" $@ + echo The 3 arguments entered were: a b c The 3 arguments entered were: a b c●You can also set these options in the shell script itself.–The “set -xv” will turn on these options.–The “set +xv” will turn off these options.Testing Conditions (35.26)●Can test for various conditions. There are two general forms. test <condition> or [ <condition> ]●I think the latter is easier to read. Be sure to include a space before and after the brackets.●To test the result of a command, just use the command, which will return an exit status.●Can reverse a condition by putting '!' before it. [ ! <condition> ]●A ':' command always returns true.Testing File Attributes (35.26)●Test if a file exists and is readable. [ -r schemas.txt ] [ -r $1.db ]●Test if a file doesn't exist or is not writeable. [ ! -w databases.txt ]●Test if a file exists and is executable. [ -x ns.sh ]●Test if a file exists and is not a directory. [ -f tmp.txt ]Numeric Tests●The following {-eq, -ne, -gt, -ge, -lt, -le }operators can be used for numeric tests. [ $1 -lt $2 ] [ $1 -gt 0 ] [ $1 -eq $# ]Exit (24.4, 35.12, 35.16)●General form. The exit command causes the current shell script to terminate. There is an implicit exit at the end of each shell script. The status indicates the success of the script. If the status is not given, then the script will exit with the status of the last command. exitor exit <status>Simple If Statement (35.13)●General form.if conditionthen one-or-more-commandsfi●Example.if [ -r “tmp.txt” ]then echo “tmp.txt is readable.”fiTesting Strings●Can perform string comparisons. It is good practice to put the shell variable being tested inside double quotes (“$v” instead of $v). [ “$1” = “yes” ]●The following will give a syntax error when $1 is empty since: [ $1 != “yes” ] ●becomes [ != “yes” ]Testing Strings (cont.)●Can


View Full Document

FSU COP 4342 - Shell Programming and Unix Utilities

Download Shell Programming and Unix Utilities
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 Shell Programming and Unix Utilities 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 Shell Programming and Unix Utilities 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?