• Aucun résultat trouvé

The Shell Garbage Can /dev /null

Dans le document Ridge Guide (Page 174-181)

then echo $word is in $file

Shell Tutorial

Notice that the read command is assigning values to two variables. The first characters that you type in, up to a space, are assigned to word. All of the rest of the characters including spaces will be assigned to file.

Pick a word that you know is in one of your files and tryout this shell program. Did you see that even though the program works, there is an irritating problem? Your program displayed more than the line of text called for by the program. The extra lines of text displayed on your terminal were the output of the grep command.

The Shell Garbage Can /dev /null

The shell has a file that acts like a garbage can. You can deposit any unwanted output in the file called /dev /null, by redirecting the command output to /dev /null.

Tryout /dev /null by throwing out the results of the who command. First, type in the who command. The response tells you who is on the system. Now, try the who command, but redirect the response into the file /dev /null.

who> /dev/null<CR>

The response displayed on your terminal was your prompt. The response to the who command was placed in /dev /n ull and became null, or nothing. If you want to dispose of the grep command response in your search program, change the if command line.

if grep $word $file > / dey /n ull < CR>

Now execute your search program. The program should only respond with the text of the echo command line.

The if ... then construction can also issue an alternate set of commands with else, when the if command sequence is false. The general format of the if ... then ... else construct follows.

Shell Tutorial

it<CR>

commandl<CR>

last command<CR>

then<CR>

commandl<CR>

last command<CR>

else<CR>

commandl <CR>

last command<CR>

fi<CR>

You can now improve your search command. The shell program search can look for a word in a file. If the word is found, the program will tell you the word is found. If it is not found (else) the program will tell you the word was NOT found. The text of your search file will look like the following:

$ eat seareh<CR>

echo Type in the word and the file name.

read word file i f

grep $word $file >/dev/null then

echo $word is in $file else

echo $word is NOT in $file fi

$

User's Guide Shell Tutorial

Following is a quick recap of the enhanced shell program called search.

command search Description:

Remarks:

Shell Program Recap search - tell if a word is in a file

arguments interactive Tells the user whether or not a word IS In a file.

The arguments, the word and the file, are asked for interactively.

The test Command for Loops

test is a very useful command for conditional constructs. The test command checks to see if certain conditions are true. If the condition is true, then the loop will continue.

If the condition is false, then the loop will end and the next command is executed.

Some of the useful options for the test command are:

test -r filename<CR>

True if the file exists and is readable test -w filename<CR>

True if the file exists and has write permission test -x filename<CR>

True if the file exists and is executable test -s filename<CR>

True if the file exists and has at least one character

If you have not changed the values of the PATH variable that were initially given to you by the system, then the executable files in your bin directory can be executed from anyone of your directories. You may want to create a shell program that will move all the executable files in the current directory to your bin directory. The test -x command can be used to select the executable files from the list of files in the current directory. Review the mv.file program example of the for construct.

Shell Tutorial parameters. The following screen executes the command from the current directory and then moves to the

bin

directory and lists the files in that directory. All the

User's Guide Shell Tutorial

$ mv.ex *<CR>

$ cd; cd bin; ls< CR>

The Conditional Construct case .. esac

The case ... esac is a multiple choice construction that allows you to choose one of several patterns and then execute a list of commands for that pattern. The keyword in must begin the pattern statements with their command sequence. You must place a ) after the last character of each pattern. The command sequence for each pattern is ended with ;;. The case construction must be ended with esac (letters of case reversed). The general format for the case construction is:

case ehal"acters<CR>

in<CR>

patternl )<CR>

com.mand line 1< CR>

h\$t command line<CR>

;;<OR>

patttu,"n2)< CR>

c<>mmand line 1< CR>

la$t command line< CR>

;;<CR>

pattel"n3)< CR>

eOlllmand line 1 < CR>

last command line<CR>

;;<OR>

*)<OR>

~ommand l<CR>

last command<CR>

;;<OR>

esae<:OR>

The case construction will try to match characters with the first pattern. If there is a match, the program will execute the command lines after the first pattern and up to the ;;.

Shell Tutorial User's Guide

If the first pattern is not matched, then the program will proceed to the second pattern.

Mter a pattern is matched, the program does not try to match any more of the patterns, but goes to the command following esac. The

*

used as a pattern at the end of, the list of patterns allows you to give instructions if none of the patterns are matched. The

*

means any pattern, so it must be placed at the end of the pattern list if the other patterns are to be checked first.

If you have used the vi editor, you know you must assign a value to the TERM variable so that the shell knows what kind of terminal is going to display the editing window of vi. A good example of the case construction would be a program that will set up the shell variable TERM for you according to what type of terminal you are logged in on. If you log in on different types of terminals, the program set.term will be very handy for you.

set.term will ask you to type in the terminal type, then it will set TERM equal to the terminal code. You may want to glance back at the beginning of the vi tutorial for the explanation of those commands. The command lines are:

TERM=terminal code<CR>

export TERM<CR>

In this example of set.term, the person uses either a Ridge Graphics Display or a Televideo 950.

User's Guide Shell Tutorial

The set.term program will first check if the value of term is ridge. If it is, then it will assign the value ridge to TERM, and exit the program. If it is not ridge, it will check for televideo. It will execute the commands under the first pattern that it finds, and then go to the next command after the esac command.

At the end of the patterns for the terminals, the pattern

*,

meaning everything else, will warn you that you do not have a pattern for that terminal, and it will also allow you to leave the case construct.

$ cat set.term<CR>

echo If you have a Ridge Display type in ridge echo If you have a Televideo 950 type in televideo read term

case $term in

esac

ridge)

TERM=ridge televideo)

TERM=tvi950

*)

echo not a correct terminal type

export TERM

echo end of program

$

What would have happened if you had placed the

*

pattern first? The set.term program would never assign a value to TERM since it would always fit the first pattern

*, which means everything.

When you read the section on modifying your login environment, you may want to put the set.term command in your bin, and add the command line

set.term<CR>

to your . profile.

Shell Tutorial User"'s Guide

Following is a quick recap of the set.term shell program.

Shell Program Recap

Dans le document Ridge Guide (Page 174-181)

Documents relatifs