1. Trang chủ
  2. » Công Nghệ Thông Tin

Mastering unix shell scripting phần 7 pdf

70 347 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 70
Dung lượng 487,79 KB

Nội dung

# Using $FILENAME as input # MUST USE DOUBLE QUOTES AROUND $PATTERN!!! -> “$PATTERN” cat “$FILENAME” \ | sed s/”${PATTERN}”/$(tput smso)”${PATTERN}”$(tput rmso)/g \ > $OUTPUT_FILE else # Input is from standard input # MUST USE DOUBLE QUOTES AROUND $PATTERN!!! -> “$PATTERN” sed s/”${PATTERN}”/$(tput smso)”${PATTERN}”$(tput rmso)/g \ > $OUTPUT_FILE # Check to see if the pattern was in the standard input grep “$PATTERN” $OUTOUT_FILE >/dev/null 2>&1 if [ $? -ne 0 ] then echo “\nSORRY: The string \c” tput smso echo “${PATTERN}\c” tput rmso echo “ was not found in standard input \c” echo “\n\n EXITING \n” exit 3 fi fi # Check the operating system, on AIX and HP-UX we need to # use the “pg”, or “page” command. The “more” command does # not work to highlight the text, it will show only the # characters that make up the escape sequence. All # other operating systems use the “more” command. case $(uname) in AIX|HP-UX) # This is a fancy “pg” command. It acts similarly to the # “more” command but instead of showing the percentage # displayed it shows the page number of the file /usr/bin/cat $OUTPUT_FILE | /usr/bin/pg -csn -p”Page %d:” ;; *) Listing 15.1 hgrep.ksh shell script. (continued) 398 Chapter 15 /usr/bin/cat $OUTPUT_FILE | /usr/bin/more ;; esac rm -f $OUTPUT_FILE # End of Script Cleanup Listing 15.1 hgrep.ksh shell script. (continued) In the shell script in Listing 15.1 we first check for the correct number of command- line arguments; either one or two arguments are valid. Otherwise, the script usage message is displayed, and the script will exit with a return code 1. If we have the cor- rect number of arguments, then we assign the arguments to variables. If we have two command-line arguments, then an input file is specified in $2—at least it is supposed to be a file. We need to do some sanity checking on this second command-line argu- ment by first checking to see that the file exists as a regular file. We do not want to do anything with the file if it is a block or character special file, a directory, or any other nonregular file. Next we make sure that the file is not empty. Then we ensure that the script can read the file, and finally we grep for the pattern in the file to see if we have anything to highlight. If all of the tests are passed, then we can proceed. By checking if the $FILENAME variable is null, or empty, we know which type of input we are dealing with. A null or empty $FILENAME variable means we use stan- dard input, which is input from a pipe in this case. If $FILENAME is not null, then we have a file specified as input to the script on the command line. The only difference in handling an input file versus standard input is that we will supply the ”cat $FILENAME |” if there is an input file specified. Otherwise, the input is already com- ing in from a pipe directly into the sed statement—it’s that simple. We have one more check before displaying the output. If we are using piped-in standard input, then we grep for “$PATTERN” in the $FILENAME to see if it exists. If not, we display a string not found message and exit. The output display is interesting because more will not work on HP-UX or AIX to dis- play the highlighted text. For HP-UX and AIX we use pg instead of more. To determine which flavor of Unix we are running, we use the uname command in a case statement. If the OS is either AIX or HP-UX, we used a fancy pg command, which has output that appears similar to the more output. Using pg -csn -p"Page %d:" will display the page number of the file, where more displays the percentage of file. All other Unix flavors will use more to display the output file. The script in Listing 15.1 is a good example of how a little ingenuity can greatly sim- plify a challenge. We sometimes make things more complicated than they need to be, as in my initial test script that parsed through the file line by line and character by char- acter, searching for the pattern. We live and learn! hgrep: Highlighted grep Script 399 Other Options to Consider As with every script there is room for improvement or customization, however you want to look at it. Other Options for the tput Command The only tput command option that we worked with was the tput smso command, which is used to turn on highlighting. The tput command has many other options to control terminal display. In our example we did a highlight of not only the text but also the surrounding block for each character. We could also highlight only the text piece, double video the entire text block, underline with other options—for example, we could have underlined bold text. The tput command is fun to play with. The short list of command options is shown in Table 15.1. Table 15.1 Options for the tput Command tput bell Ring the bell tput blink Start blinking mode tput bold Start double intensity (much brighter than reverse video) tput civis Turn the cursor off (make the cursor invisible) tput cnorm Make the cursor normal again tput cr Send a carriage to the terminal tput cvvis Make the cursor very bright tput dim Start one-half intensity mode tput ed Clear to the end of the display tput el Clear to the end of the line tput flash Send a visible bell (good to send a flash to someone’s screen) tput invis Start invisible text mode tput prot Start protected mode tput rc Restore the last saved cursor position (paved by tput sc) tput rev Begin reverse video mode (bright!) tput rmso End the standout mode (reverses tput smso) tput rmul Ends the underline (underscore) mode tput sc Save the cursor position 400 Chapter 15 Table 15.1 (Continued) tput sgr0 Turn off all video modes tput smso Start the standout mode (soft reverse video we used in this chapter) tput smul Start the underline (underscore) mode tput Underscore one character and move to the next character Table 15.1 is only an abbreviated listing of the tput command options. As you can see, we can do a lot with the text on the screen. Use your imagination, and play around with the commands. Summary In this chapter we introduced using reverse video to highlight text within our output. Also we showed how to do command substitution inside a sed command statement. There are many more options for the tput command to control the terminal; for exam- ple, we could have underlined the matching pattern. The nice thing about the tput command is that it will let you mix things up, too. In the next chapter we are going to look at how to keep the printers in the landscape printing. If you do not automate this function you could spend all of your time doing printer management instead of doing any real work. See you in the next chapter! hgrep: Highlighted grep Script 401 403 If you have worked in a large systems environment for very long you already know how frustrating it can be to keep the printer farm happy. In my contracting days I worked in several shops that consistently had problems with the printers. In most cases, the print queues went down because of network timeouts and extended device waits. In this kind of environment you have two choices: keep answering the calls from the help desk or write a shell script to monitor the printer queues and reenable the queues as they drop offline. I prefer the second method. Like every other Systems Administrator, I like to be proactive in my approach to solving the little problems as well as the big ones. The shop I remember the best was a hospital. This hospital has more than 30 satellite clin- ics around town and only one 100MB/Sec pipe coming in to the hospital from the out- side world. Most of the clinics have between three and five printers, with at least one printer active most of the day. When I came on board, the first problem I encountered was the huge volume of calls to the help desk about printer problems. What caught my eye was the fact that all of the calls came from the clinics, not from inside the hospital. I knew immediately that a shell script was in order! In this chapter we are going to look at two methods of bringing up the print queues, enabling individual queues and bring- ing up the whole lot. Because Unix flavors vary on handling printers and queues, we first will look at the differences between the Unix flavors. Print Queue Hell: Keeping the Printers Printing CHAPTER 16 System V versus BSD Printer Subsystems Depending on the Unix flavor, the commands vary to control the printers and queues because some use the System V subsystem and others use BSD. With AIX you have an ever more confusing situation beginning with AIX 5L. Starting with this release, AIX now supports both the “classic” AIX printer subsystem and the System V printer ser- vice. Another problem is that some commands do not provide the full print queue name if the queue name exceeds seven characters. I have come up with some ways to get around the long queue names, and on most systems you do not have to worry about long queue names too much if you want to control all of the printers at once. In this book we are covering AIX, HP-UX, Linux, and Solaris. For no other reason that I can think of, let’s cover the printer systems in alphabetical order. AIX Print Control Commands AIX is the most interesting of the bunch with its new support for the System V printer service starting with AIX 5L. Although the AIX classic printer subsystem will still be supported for many years, the move seems to be going to System V for printing service. Classic AIX Printer Subsystem Most AIX Systems Administrators still prefer to use the classic AIX printer subsystem. This is the primary printing that I have supported for years. With the AIX printer sub- system you do not have the detailed control that the System V service offers. For exam- ple, you do not control forms and user priorities at a granular level, and you cannot manage the printers independently of the print queues easily. With this printer sub- system anyone can print on any printer, and the print queue is either UP, allowing you to print, or DOWN, disabling all printing. The shell scripts we are going to write for the classic AIX printer subsystem work at the print queue level. The two commands we are going to use are lpstat and enq -A. Both commands pro- duce the same output, but some administrators seem to like one over the over. As I stated earlier, we need to be aware that sometimes print queues are created with queue names longer than seven characters, which is the default that can be displayed with both of these commands. I guess IBM noticed this little problem and added the -W switch to give a wide character output. Look at Listings 16.1 and 16.2 to see the different outputs. # lpstat Queue Dev Status Job Files User PP % Blks Cp Rnk hp4 lp0 READY hp4-ps lp0 READY hp4-gl lp0 READY yogi_hp lp0 DOWN yogi_hp lp0 DOWN Listing 16.1 Output using lpstat or enq -A. 404 Chapter 16 # lpstat -W Queue Dev Status Job Files User PP % Blks Cp Rnk hp4 lp0 READY hp4-ps lp0 READY hp4-gl lp0 READY yogi_hp4_1 lp0 DOWN yogi_hp4_1ps lp0 DOWN Listing 16.2 Output using lpstat -W or enq -AW. As you can see in Listing 16.1, the long queue names are cut off at the seventh char- acter when using the lpstat or enq -A commands. By adding the -W switch to these commands we see the entire long queue name. This is important because you cannot control a print queue if you do not have the exact, and full, queue name. There are two methods to script using either lpstat -W or enq -AW. One method is to loop through each queue that is reported DOWN; the other is to use one long com- pound command. We are first going to look at the looping method. A little for loop can be used to extract out the queue names of the printers in a DOWN state. The list used for the for loop comes from either of the following command statements: lpstat -W | tail +3 | grep DOWN | awk ‘{print $1}’ or enq -AW | tail +3 | grep DOWN | awk ‘{print $1}’ Both of the previous statements produce the same output. Notice that tail +3 is the second command in pipe, just after the lpstat and enq commands. We use tail +3 in this statement to remove the two lines of header information. This method is much cleaner than trying to grep out some unique character in both of the header lines. Notice that the number of lines, specified by +3, is one larger than the actual num- ber of lines that we want to remove. Using the tail command this way, we are telling tail to start listing at the third line, so two lines are removed at the top of the output. The third command in the pipe is where we grep for DOWN, looking for disabled printers, as shown in Listing 16.2. The output from this stage of the command is only the lines of the enq and lpstat output that contains the word DOWN. Using these lines as input for the next command in the pipe, we are ready to extract the actual queue name(s) of the disabled printers, as shown in the output here. yogi_hp4_1 lp0 DOWN yogi_hp4_1ps lp0 DOWN Print Queue Hell: Keeping the Printers Printing 405 The awk command, as we use it, is used to extract the field that we want to work with, which is the first field, the queue name. Using the previous output as input to our awk statement we extract out the first field using the following syntax: command | awk ‘{print $1}’ You can extract any valid field using awk as well as different fields at the same time. For example, if we want to extract fields 1 and 3, specified by $1 and $3, the following awk statement will take care of the task. command | awk ‘{print $1, $3}’ Notice that I added a comma between $1 and $3. If the comma is omitted, then there will not be a space between the two strings. Instead the output will be two strings appended together without a space. For our for loop we can first send the lpstat and enq command output to a file and process the file in a loop, or we can use command substitution to add the statement directly into the for loop to create the list of objects to loop through. Let’s look at our for loop structure. for Q in $( enq -AW | tail +3 | grep DOWN | awk ‘{print $1}’ ) do # Do something here. done Using this loop command statement, the for loop will loop through yogi_hp4_1 and yogi_hp4_1ps print queue names, which is equivalent to the following for loop structure: for Q in yogi_hp4_1 yogi_hp4_1ps do # Do something here. done Because we never know which queues may be down, we need to parse through the output of the actual queue names of the printers in a disabled state. The shell script in its entirety is shown in Listing 16.3. #!/bin/ksh # # SCRIPT: enable_AIX_classic.ksh # # AUTHOR: Randy Michael # DATE: 03/14/2002 # REV: 1.1.P Listing 16.3 For loop to enable “classic” AIX print queues. 406 Chapter 16 # # PLATFORM: AIX Only # # PURPOSE: This script is used to enable print queues on AIX systems. # # REV LIST: # # set -x # Uncomment to debug this script # set -n # Uncomment to check syntax without any execution # for Q in $( enq -AW | tail +3 | grep DOWN | awk ‘{print $1}’) do enable $Q (( $? == 0 )) || echo “\n$Q print queue FAILED to enable.\n” done Listing 16.3 For loop to enable “classic” AIX print queues. (continued) Inside the for loop we attempt to enable each print queue individually. If the return code of the enable command is not zero we echo an error message indicating that the queue could not be enabled. Notice the highlighted lines in Listing 16.3. We use the mathematical test, specified by the double parentheses, (( math test )). Using this math test you normally do not add a dollar sign, $, in front of a numeric variable. When the variable is produced by the system, such as $?, the dollar sign is required. Testing for equality also requires using the double equal signs, ==, because the single equal sign, =, is meant as an assignment, not a test. After the test to check for a zero return code, we use a logical OR, specified by the double pipes, ||. This logical OR will execute the next command only if the return code of the enable $Q command is nonzero, which means that the command failed. There is also a logical AND that is used by placing double ampersands, &&, in a com- mand statement. A logical AND does just the opposite; it would execute the succeeding command if the test is true, instead of false. Both the logical OR and logical AND are used as replacements for if then else statements. We can also accomplish this task by using a single compound command statement. Just as we used command substitution in the for loop, we can use command substitu- tion to produce command parameters. For example, we can use our for loop command to create command parameters to the enable command. To see this more clearly, look at the following two commands. enable $(enq -AW | tail +3 | grep DOWN | awk ‘{print $1}’) 2>/dev/null or enable $(lpstat -W | tail +3 | grep DOWN | awk ‘{print $1}’) 2>/dev/null Print Queue Hell: Keeping the Printers Printing 407 [...]... accepting requests since May 07 07: 02 2002 yogi_hp4_1ps accepting requests since May 07 07: 02 2002 long_queue not accepting requests since Tue May 7 07: 02:23 EDT 2002 s_q_nam not accepting requests since Tue May 7 07: 02:23 EDT 2002 - # lpstat -p printer long_queue disabled since Tue May 7 07: 02:01 EDT 2002 available stopped by user printer s_q_nam disabled since Tue May 7 07: 02:01 EDT 2002 available stopped... accepting requests since May 07 07: 02 2002 yogi_hp4_1ps accepting requests since May 07 07: 02 2002 Listing 16.12 lpstat -a and lpstat -p command output (continues) 429 430 Chapter 16 long_queue not accepting requests since Tue May s_q_nam not accepting requests since Tue May 7 07: 02:23 EDT 2002 - 7 07: 02:23 EDT 2002 - # lpstat -p printer long_queue disabled since Tue May 7 07: 02:01 EDT 2002 available... long_queue disabled since Tue May 7 07: 02:01 EDT 2002 available stopped by user printer s_q_nam disabled since Tue May 7 07: 02:01 EDT 2002 available stopped by user printer hp4 unknown state enabled since May 07 07: 30 2002 available printer yogi_hp4_1ps unknown state enabled since May 07 07: 30 2002 available Listing 16.12 lpstat -a and lpstat -p command output (continued) Notice in Listing 16.12 that the... | awk ‘{print $3}’) fi done Listing 16 .7 print_UP_HP-UX.ksh shell script listing Print Queue Hell: Keeping the Printers Printing I want to point out a nice little trick in the shell script in Listing 16 .7 In both of the if then fi statements, notice that we execute a command inside parentheses What this technique allows us to do is execute a command in a sub -shell and use the command’s resulting return... 2002 available stopped by user printer s_q_nam disabled since Tue May 7 07: 02:01 EDT 2002 available stopped by user printer hp4 unknown state enabled since May 07 07: 30 2002 available printer yogi_hp4_1ps unknown state enabled since May 07 07: 30 2002 available Listing 16.5 lpstat -a and lpstat -p command output Notice in Listing 16.5 that the output from each command option has a unique set of status... queuing_only_UP_Linux.ksh shell script listing Notice that the only thing that was changed this time is the first case statement structure was removed from the script and the name of the shell script was changed Print Queue Hell: Keeping the Printers Printing You could also modify the shell script in Listing 16.8 to add a command-line parameter to let you control queuing and printing individually from the same shell. .. Now we need to combine the shell scripts for each of the different Unix flavors so that one script does it all Please do not think that taking several shell scripts, making functions out of them, and combining the new functions into a new script are difficult tasks To make one script out of this chapter we are going to take the best of our scripts and extract the code For each shell script we make a new... always want to enable printing and queuing at the same time We can break up the shell script in Listing 16.8 and pull out the individual case statements to start either printing or queuing Because printing is controlled by array element 2 we can extract the first case statement to create a new shell script Let’s call this shell script printing_only_UP_Linux.ksh You can see the modifications in Listing... $LOGFILE ;; enabled|*) : # No-Op - Do Nothing ;; esac done Listing 16.9 printing_only_UP_Linux.ksh shell script listing (continued) Notice that the only thing that was changed is that the second case statement structure was removed from the script and the name was changed We can do the same thing to create a shell script that only enables queuing, as shown in Listing 16.10 423 424 Chapter 16 #!/bin/ksh... technique in a shell script to redirect the failure notification to a log file If you use a log file you may want to add a date stamp System V Printing on AIX Beginning with AIX 5L, IBM supports System V printing I find that Solaris has the closest command usage and output With only a few differences between AIX and Solaris System V printing in the output produced, you could use the shell scripts interchangeably . May 07 07: 02 2002 yogi_hp4_1ps accepting requests since May 07 07: 02 2002 long_queue not accepting requests since Tue May 7 07: 02:23 EDT 2002 - s_q_nam not accepting requests since Tue May 7 07: 02:23. May 7 07: 02:01 EDT 2002. available. stopped by user printer s_q_nam disabled since Tue May 7 07: 02:01 EDT 2002. available. stopped by user printer hp4 unknown state. enabled since May 07 07: 30. ‘{print $3}’) fi done Listing 16 .7 print_UP_HP-UX.ksh shell script listing. 416 Chapter 16 I want to point out a nice little trick in the shell script in Listing 16 .7. In both of the if then fi statements,

Ngày đăng: 09/08/2014, 16:20

TỪ KHÓA LIÊN QUAN