Spaces, Braces, and Semicolons
June 30, 2010 Hey, Ted
My Qshell script would work perfectly if I could make it count correctly. It thinks I have more objects in library QGPL than I really do have. Can you help? –Hugo Today is a spectacular day to improve our Qshell skills, thus improving our Unix and Linux shell skills and beefing up our résumés. Here is Hugo’s Qshell command: find /qsys.lib/qgpl.lib/ -name '*' | wc -l The “find” utility produces a list of everything (-name ‘*’) in QGPL. The list is passed along to the “wc” utility, which counts the number of lines (-l) in the list. The problem is that the list contains not only one line for each object, but also a line for the library itself and a line for each database file member. Let’s try a different approach. system dsplib qgpl | sed -n '/Number of objects . . . . . . . . . . . :/{ s///; p; q; }' The system utility runs the Display Library (DSPLIB) command, writing the output to the standard output device (stdout). To see what this output looks like, run the following command from a CL command line: DSPLIB LIB(QGPL) OUTPUT(*PRINT) You’ll see something like this: 5722SS1 V5R4M0 060210 Display Library Library . . . . . . . . . . . . . . . . : QGPL Type . . . . . . . . . . . . . . . . . : PROD Number of objects . . . . . . . . . . . : 2 Library ASP number . . . . . . . . . . : 1 Library ASP device . . . . . . . . . . : *SYSBAS Library ASP group . . . . . . . . . . : *SYSBAS Create authority . . . . . . . . . . . : *SYSVAL Text description . . . . . . . . . . . : General Purpose Library Object Type Attribute Size Description MYPGM *PGM CLP 73728 My program YOURPGM *PGM CLP 73728 Your program The pipe (vertical bar) tells Qshell that the output of DSPLIB is to be used as input to the stream editor, sed. Sed reads the output of DSPLIB, one line at a time, looking for lines that contain the string Number of objects . . . . . . . . . . . : This search string is known as an address. Notice that the address is delimited by slashes. When sed finds a line that matches the address, it executes the command string that follows the address. And this gives me a chance to show you some nifty features of sed. What do I want sed to do? I want it to strip off the Number of objects . . . . . . . . . . . : string. I want it to write what’s left of the line to stdout. Then I want sed to quit, because it has found what I was looking for. Nifty feature number 1 is that I don’t have to specify a search string in the “s” (substitute) sed command. If I omit this argument, sed assumes I meant the search string. Therefore, the following two substitute commands are equivalent in this case. s/Number of objects . . . . . . . . . . . ://; s///; Both tell sed to replace Number of objects . . . . . . . . . . . : with nothing. Nifty feature number 2 is that I can make sed execute more than one command. Like all things Unix, it’s cryptic, but here’s how it’s done.
To strip out the search string, write to stdout and quit, do like this: { s///; p; q; }' All that’s left is to use command substitution to load the number of objects into a variable. (That’s nifty feature number 3!) NbrOfObjects=$(system dsplib qgpl | sed -n '/Number of objects . . . . . . . . . . . :/{ s///; p; q; }') I know Qshell is weird to those of us who are accustomed to an engineered operating system, but believe it or not, a lot of people on this planet live and breathe this kind of stuff.
|