Lecture8

Lecture8 - Lecture 8 Shell Programming Command substitution...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
Lecture 8 Shell Programming – Command substitution Regular expressions and grep Use of exit, for loop and expr commands COP 3344 Introduction to UNIX 1
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Command Substitution • a string in back quotes ` … ` does command substitution his means that the result of the command (the standard – This means that the result of the command (the standard output of the command) replaces the back quoted string Examples: count=`wc -w <$1` # the value of count is assigned the number of words in file $1 if [ `wc -l < $2.txt` -lt 1000 ]; #checks if the number of lines in the file is < 1000 cat `grep -l exit *.sh` print out all * sh files containing the word exit 2 #print out all *.sh files containing the word exit
Background image of page 2
Exit Command (again) • Conventionally, zero normally indicates success. onzero values indicate some type of failure It is Nonzero values indicate some type of failure. It is thus good practice to ensure that if the shell script terminates properly, it is with an “exit 0” command. • If the shell script terminates with some error that ould be useful to a calling program terminate would be useful to a calling program, terminate with an “exit 1” or other nonzero condition. • most Unix utilities that are written in C will also call “exit(<value>);” upon termination to pass a value back to the shell or utility that called that tilit 3 utility.
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Exit Example • The following shell script exits properly. It also distinguishes the response through the value returned. !/bin/sh #!/bin/sh #determines a yes (0) or no (1) answer from user echo “Please answer yes or no”; read answer while : do case $answer in “yes”) exit 0;; “no”) exit 1;; ) echo “Invalid; enter yes or no only” *) echo Invalid; enter yes or no only read answer;; esac 4 done
Background image of page 4
Testing the Exit Status • Conditions tested in control statements can also be e exit status of commands Assume that the the exit status of commands. Assume that the script “yes.sh” has been invoked.
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/23/2008 for the course COP 33344 taught by Professor Sudhir during the Spring '08 term at FSU.

Page1 / 18

Lecture8 - Lecture 8 Shell Programming Command substitution...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online