CAPITULO III REVISIONES DE GABINETE Y DE DICTAMEN
3.1 REVISIÓN DE GABINETE
3.1.2 NOTIFICACIÓN DEL REQUERIMIENTO DE DOCUMENTACIÓN
You have used the grep command as a filter to extract or delete lines containing a particular text pattern. Here you will learn two more commands that are useful for text processing. The sed command is a stream editor that takes text from stdin and sends it to stdout after editing it.
The cut command is used to extract a desired part of text from a line. It also takes its input from stdin and sends its output to stdout.
Using sed
The stream editor is a useful tool to edit large amounts of text at one time. For example, you may need to search for a word in a large file and replace it with another word. Let's try to replace the word "echo" with "ECHO" in script-27. The sed command will do the job as follows.
$ sed s/echo/ECHO/g script-27
#!/usr/bin/sh NUM=100 while true do
ECHO "Enter a divisor for integer 100 : \c"
read DIV if [ $DIV -eq 0 ] then
ECHO "Divide by zero is not permitted"
exit 1 fi
(( QUO = NUM / DIV )) (( REM = NUM % DIV ))
ECHO "The quotient is : $QUO"
ECHO "The remainder is : $REM"
done
If you want to do an operation on all files, you can write a shell program to accomplish the job.
Program script-28 shown here replaces "echo" with "ECHO" in all files of the current directory.
#!/usr/bin/sh for FILE in * do
cat $FILE |sed s/echo/ECHO/g >tempfile cp tempfile $FILE
done rm tempfile
As you can see, this is a very useful tool to make changes to a large number of files that could take a long time otherwise. Consider you are writing a book and want to change "figure" to
"Fig" in all chapters. If you don't know how to do it in an efficient way, you may start editing all files manually, spending hours on a job that need take only a few minutes.
There are many ways to use sed that make it a very useful tool. For additional information, consult the sed manual pages.
Using cut
The cut command is used to extract a particular part of data from a line of text. If the data are in the form of fields, you can extract particular fields. For example, if you want to list all user names on your system, you can use the cut command on the /etc/passwd file as follows:
cut -f 1 -d : /etc/passwd
or
cat /etc/passwd | cut -f 1 -d :
Here the -f 1 option tells the command that you want to extract field number 1. The -d : option shows that the fields in the data are separated by a delimiter colon ":". Since user names are in the start of each line in /etc/passwd and they are followed by a colon, the command extracts all user names from the file.
You may also use the cut command to extract a particular number of characters from a file. To extract the first eight characters from every line in the /etc/passwd file, you may use the following command.
cat /etc/passwd | cut -c 1-8
Here you specified a range of characters using the -c 1-8 option. See the manual pages for more information on the cut command.
Let us use the cut command in a shell program script-29. This script is used to send an email message to all users on a system. The message contents are stored in a file mailfile. You use the mailx command to send the message.
#!/usr/bin/sh
for USER in $(cut -f 1 -d : /etc/passwd) do
mailx -s "Test mail" $USER <mailfile done
You have used the cut command to create a list of user names and then send a mail message to each name in the list.
The sleep Command
The sleep command is used to suspend execution for a certain amount of time. You provide the number of seconds as the argument to the sleep command. The following code segment lists all files in the current directory with a pause of five seconds between every file.
for FILE in * do
ll $FILE sleep 5 done
Chapter Summary
This was the second and last chapter on the topic of shell programming. Shell loops are discussed in this chapter. You started learning arithmetic and logic operations with the help of the let command. Explicit and implicit modes of the let command were discussed. Table 11-1 listed operators that can be used with the let command. Then you studied the while-do-done loop. The syntax of this loop is:
while condition do
command block done
The while-do-done loop repeats itself until the condition becomes false. The until-do-done loop is similar to the while-do-done loop; the only difference is that it repeats until the condition becomes true. The syntax of this loop is:
until condition do
command block done
Next you studied the for-do-done loop, which is used to process a list of elements provided as the argument to the for command. The syntax of the for-do-done loop is:
for var in list do
command block done
The for-do-done loop continues to execute until it has processed all elements in the list. You also found flow diagrams of all these loops in this chapter.
When you need to break a loop, you can use the break, continue, or exit commands. The break command transfers program control to the command just after the next done keyword.
The continue command skips all remaining commands in the loop and transfers control to the start of the loop. The exit command permanently terminates the program and returns an exit code. The exit command is used in situations where a critical error is encountered and it is dangerous to continue program execution.
In the last part of the chapter, you learned some text processing commands. The stream editor (sed) is used to edit data received at stdin. The edited data is sent to stdout. With the help of shell programs, you can perform some complex editing tasks using sed. The cut command is used to extract data coming from stdin. This command is especially useful when the data are in the form of fields separated by a delimiter. You can extract any field with the help of the cut command by specifying a field number. A small shell program was presented to send mail to all system users with the help of the cut command. The sleep command is used to suspend program execution for a certain amount of time.
This is the last chapter of the first part of the book. The next part contains chapters related to HP-UX system administration. The material presented in the next chapters is specific to HP-UX and may not be used on other UNIX systems.
Chapter Review Questions
1: What is the difference between the test and let commands?
2: Explain the unary and binary negative signs.
3: Consider the following program. What may be the possible problem?
VAR=1
(($VAR=$VAR+1)) while [ $VAR -lt 10 ] do
echo $VAR done
4: Write a program that provides three options. The first option asks the user to enter a number. It then adds the number to 100 and displays the result. The second option subtracts the number from 100 and displays the result. The third option terminates the program.
5: Write a program that accepts a list of user names on the command line and then displays a message if any of these users is logged in.
6: Previously all users were using the Bourne shell. The administration has changed the policy and now everyone is asked to use the POSIX shell. Write a program that changes the shell of all users from /usr/bin/old/sh to /usr/bin/sh.
Test Your Knowledge
1: Which command will you use to add the values of two variables VAR1 and VAR2, and store the result in VAR3?
[ $VAR3 = $VAR1 + $VAR2 ]
$VAR3 = [ $VAR1 + $VAR2 ]
$VAR3 = (( VAR1 + VAR2 ))
$VAR3 = VAR1 + VAR2 ))
2: You want to wait for 10 seconds at the end of the loop in each loop cycle. Which command will you use?
sleep pause wait
Any of the three commands can be used.