📄 0069-0071.html
字号:
<HTML>
<HEAD>
<TITLE>Maximum RPM (RPM):Getting Information About Packages:EarthWeb Inc.-</TITLE>
<META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW">
<SCRIPT>
<!--
function displayWindow(url, width, height) {
var Win = window.open(url,"displayWindow",'width=' + width +
',height=' + height + ',resizable=1,scrollbars=yes');
}
//-->
</SCRIPT>
</HEAD>
-->
<!-- ISBN=0672311054 //-->
<!-- TITLE=Maximum RPM (RPM)//-->
<!-- AUTHOR=Edward Bailey//-->
<!-- PUBLISHER=Macmillan Computer Publishing//-->
<!-- IMPRINT=Sams//-->
<!-- CHAPTER=05 //-->
<!-- PAGES=0053-0078 //-->
<!-- UNASSIGNED1 //-->
<!-- UNASSIGNED2 //-->
<P><CENTER>
<a href="0066-0068.html">Previous</A> | <a href="../ewtoc.html">Table of Contents</A> | <a href="0072-0074.html">Next</A>
</CENTER></P>
<A NAME="PAGENUM-69"><P>Page 69</P></A>
<B>5.2.2.11.1. Literal Text
</B>
<P>Any part of a format string that is not associated with tags or array iterators will be treated
as literal text. Literal text is just that: It's text that is printed just as it appears in the format
string. In fact, a format string can consist of nothing but literal text, although the output wouldn't
tell us much about the packages being queried. Let's give the
--queryformat option a try, using a format string with nothing but literal text:
</P>
<!-- CODE SNIP //-->
<PRE>
# rpm -q --queryformat `This is a test!' rpm
This is a test!#
</PRE>
<!-- END CODE SNIP //-->
<P>The RPM command might look a little unusual, but if you take out the
--queryformat option, along with its format string, you'll see that this is just an ordinary query of the
rpm package. When the --queryformat option is present, RPM will use the text immediately following
the option as a format string. In this case, the format string is
`This is a test!'. The single quotes are required. Otherwise, it's likely that your shell will complain about some of the
characters contained in the average format string.
</P>
<P>The output of this command appears on the second line. As you can see, the literal text
from the format string was printed exactly as it was entered.
</P>
<B>5.2.2.11.2. Carriage Control Escape Sequences
</B>
<P>Wait a minute. What is that # doing at the end of the output? Well, that's our shell
prompt. You see, we didn't direct RPM to move to a new line after producing the output, so the
shell prompt ended up being tacked to the end of the output.
</P>
<P>Is there a way to fix that? Yes, there is. We need to use an escape sequence. An escape
sequence is a sequence of characters that starts with a backslash
(\). Escape sequences add carriage control information to a format string.
The following escape sequences can be used:
</P>
<UL>
<LI> \a—Produces a bell sound or similar alert.
<LI> \b—Backspaces one character.
<LI> \f—Outputs a form-feed character.
<LI> \n—Outputs a newline character sequence.
<LI> \r—Outputs a carriage return character.
<LI> \t—Causes a horizontal tab.
<LI> \v—Causes a vertical tab.
<LI> \\—Displays a backslash character.
</UL>
<P>Based on this list, it seems that an \n escape sequence at the end of the format string will
put our shell prompt on the next line:
</P>
<!-- CODE SNIP //-->
<PRE>
# rpm -q -queryformat `This is a test!\n' rpm
This is a test!
#
</PRE>
<!-- END CODE SNIP //-->
<P>Much better.
</P>
<A NAME="PAGENUM-70"><P>Page 70</P></A>
<B>5.2.2.11.3. Tags
</B>
<P>The most important parts of a format string are the tags. Each tag specifies what
information is to be displayed and can optionally include field width, as well as justification and data
formatting instructions. (RPM uses printf to do
--queryformat formatting. Therefore, you can use any of the
printf format modifiers discussed in the printf(3) man page.)
</P>
<P>But for now, let's look at the basic tag. In fact, let's look at three: the tags that print the
package name, version, and release. Strangely enough, these tags are called
NAME, VERSION, and RELEASE. In order to be used in a format string, the tag names must be enclosed in curly braces and
preceded by a percent sign. Let's give it a try:
</P>
<!-- CODE SNIP //-->
<PRE>
# rpm -q --queryformat `%{NAME}%{VERSION}%{RELEASE}\n' rpm
rpm2.31
#
</PRE>
<!-- END CODE SNIP //-->
<P>Let's add a dash between the tags and see if that makes the output a little easier to read:
</P>
<!-- CODE SNIP //-->
<PRE>
# rpm -q --queryformat `%{NAME}-%{VERSION}-%{RELEASE}\n' rpm
rpm-2.3-1
#
</PRE>
<!-- END CODE SNIP //-->
<P>Now our format string outputs standard package labels.
</P>
<B>5.2.2.11.4. Field Width and Justification
</B>
<P>Sometimes it's desirable to allocate fields of a particular size for a tag. This is done by
putting the desired field width between the tag's leading percent sign and the opening curly brace.
Using our package-label_producing format string, let's allocate a 20-character field for the version:
</P>
<!-- CODE SNIP //-->
<PRE>
# rpm -q --queryformat `%{NAME}-%20{VERSION}-%{RELEASE}\n' rpm
rpm- 2.3-1
#
</PRE>
<!-- END CODE SNIP //-->
<P>The result is a field of 20 characters: 17 spaces followed by the 3 characters that make up
the version.
</P>
<P>In this case the version field is right-justified; that is, the data is printed at the far right of
the output field. We can left-justify the field by preceding the field width specification with a dash:
</P>
<!-- CODE SNIP //-->
<PRE>
# rpm -q --queryformat `%{NAME}-%-20{VERSION}-%{RELEASE}\n' rpm
rpm-2.3 -1
#
</PRE>
<!-- END CODE SNIP //-->
<P>Now the version is printed at the far left of the output field. You might be wondering
what would happen if the field width specification didn't leave enough room for the data being
printed. The field width specification can be considered the minimum width the field will take. If
the data being printed is wider, the field will expand to accommodate the data.
</P>
<B>5.2.2.11.5. Modifiers—Making Data More Readable
</B>
<P>While RPM does its best to appropriately display the data from a
--queryformat, there are times when you'll need to lend a helping hand. Here's an example. Say we want to display the
name
</P>
<A NAME="PAGENUM-71"><P>Page 71</P></A>
<P>of each installed package, followed by the time the package was installed. Looking through
the available tags, we see INSTALLTIME. Great! Looks like this will be simple:
</P>
<!-- CODE //-->
<PRE>
# rpm -qa --queryformat `%{NAME} was installed on %{INSTALLTIME}\n'
setup was installed on 845414601
pamconfig was installed on 845414602
filesystem was installed on 845414607
...
rpm was installed on 851659311
pgp was installed on 846027549
#
</PRE>
<!-- END CODE //-->
<P>Well, that's a lot of output, but it's not very useful. What are those numbers? RPM didn't
lie—they're the times the packages were installed. The problem is that the times are being
displayed in their numeric form used internally by the operating system, and humans like to see the
day, month, year, and so on.
</P>
<P>Fortunately, there's a modifier for just this situation. The name of the modifier is
:date, and it follows the tag name. Let's try our example again, this time using
:date:
</P>
<!-- CODE //-->
<PRE>
# rpm -qa --queryformat `%{NAME} was installed on %{INSTALLTIME:date}\n'
setup was installed on Tue Oct 15 17:23:21 1996
pamconfig was installed on Tue Oct 15 17:23:22 1996
filesystem was installed on Tue Oct 15 17:23:27 1996
...
rpm was installed on Thu Dec 26 23:01:51 1996
pgp was installed on Tue Oct 22 19:39:09 1996
#
</PRE>
<!-- END CODE //-->
<P>That sure is a lot easier to understand, isn't it?</P>
<P>Here's a list of the available modifiers:</P>
<UL>
<LI> The :date modifier displays dates in human-readable form. It transforms
846027549 into Tue Oct 22 19:39:09 1996.
<LI> The :perms modifier displays file permissions in an easy-to-read format. It changes
-32275 to -rwxr-xr-x-.
<LI> The :depflags modifier displays the version comparison
ags used in dependency processing in human-readable form. It turns
12 into >=.
<LI> The :fflags modifier displays a c if the file has been marked as being a
configuration file, a d if the file has been marked as being a documentation file, and blank
otherwise. Thus, 2 becomes d.
</UL>
<B>5.2.2.11.6. Array Iterators
</B>
<P>Until now, we've been using tags that represent single data items. There is, for example,
only one package name or installation date for each package. However, other tags can represent
many different pieces of data. One such tag is
FILENAMES, which can be used to display the names
of every file contained in a package.
</P>
<P><CENTER>
<a href="0066-0068.html">Previous</A> | <a href="../ewtoc.html">Table of Contents</A> | <a href="0072-0074.html">Next</A>
</CENTER></P>
</td>
</tr>
</table>
<!-- begin footer information -->
</body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -