Open main menu

CDOT Wiki β

Changes

OPS245 Lab 3

2,823 bytes removed, 18:46, 19 September 2021
m
Part 3: Generating Sofware Package Information with Shell Scripts: - making this section non-scripting
|}
===Part 3: Generating Sofware Package Information with Shell ScriptsUsing sed to Filter Output from Commands===We will continue with using shell scripts commands to create a Software Information Report that manipulates output generated by the '''rpm''' command. The '''sed''' and '''awk''' commands are very useful tools in shell scripting to manipulate text. In this lab, we will be using ''sed'' to allow the user to select certain portions from the rpm command (options -qi).
'''Perform the following steps:'''
<ol>
<li> Use your '''c7host''' VM for this section.</li>
<li>Open a Bash terminal and run the command:
# Use your '''c7host''' VM for this section.
# Open a Bash shell terminal and use '''sudo -i''' to switch to a root session.
# Use a text editor (such as <b><code><span style="color:#3366CC;font-size:1.2em;">vi</span></code></b> or <b><code><span style="color:#3366CC;font-size:1.2em;">nano</span></code></b>) to create a Bash Shell script called: <b><code><span style="color:#3366CC;font-size:1.2em;">packageInfo.bash</span></code></b> in the '''/root/bin''' directory.
# Enter the following text content into your text-editing session:
<code style="color:#3366CC;font-family:courier;font-size:.9em;margin-left:20px;font-weight:bold;">
<br>
&#35;!/bin/bash <br>
<br>
&#35; packageInfo.bash<br>
&#35; Purpose: Generates a report to displaying specified information of installed software<br>
&#35;<br>&#35; USAGE: ./packageInfo.bash [application-name]<br>
&#35;<br>
&#35; Author: *** INSERT YOUR NAME ***<br>
&#35; Date: *** CURRENT DATE ***<br>
<br>
if [ $(whoami) != "root" ] # only runs if root<br>
then<br>&nbsp;echo "You must be logged in as root." >&2<br>
&nbsp;exit 1<br>
fi<br>
</code>
<br>
<ol><li value="4">Save your editing session, but remain in the text editor.</li><li>The code displayed below will require the user to include only one argument after the command (script) which will be the application name. The following code will also generate the report title and current date. Add the following code</li></ol>
<br>
<code style="color:#3366CC;font-family:courier;font-size:.9em;font-weight:bold;">
 
if [ $# -ne 1 ]<br>
then<br>
&nbsp; echo "Your command must have a application-name as argument" >&2<br>
&nbsp; echo "USAGE: $0 [application-name]" >&2<br>
&nbsp; exit 1<br>
fi<br>
<br>
&#35; Create report title (echo with -e option allows newline \n character to be used)<br>
echo -e "\nSOFTWARE PACKAGE INFORMATION REPORT" > /root/package-info.txt <br>
echo -e "Date: $(date +'%A %B %d, %Y (%H:%M:%p)')\n\n " >> /root/package-info.txt<br>
 
</code>
<ol><li value="6">Save your editing session, but remain in the text editor.</li><li>The code displayed below uses a trick called the "Here Document" to redirect stdin from within the command (a quick way to display output on the screen). The read command will store the different information report items as words (separated by a space). The sed command used to convert the spaces to pipes (|) and stored into another variable. This allows the sed command to use extended regular expressions to print rpm elements that match those various patterns to be added to the report. Add the following code</li></ol>
<br>
<code style="color:#3366CC;font-family:courier;font-size:.9em;font-weight:bold;">
&#35; Clear screen and use Here Document to display select on report items to read into variable<br>
clear<br>
cat &lt;&lt;+<br>
Available Package Information Items:<br>
<br>
Name<br>
Summary<br>
Version<br>
License<br>
Source<br>
URL<br>
+<br>
read -p "Enter word(s) shown above separated by spaces: " choice<br>
<br>
&#35; Convert spaces to pipe symbol (|)<br>
processedChoice=$(echo $choice | sed 's/ /|/g')<br>
<br>
<code>
&#35; Use sed with extended regular expressions to only print those matching report elements<br>
rpm -qi $1 | sed -r -n "xchat</^($processedChoice)/ p" &gt;&gt; /root/package-info.txt<brcode><br>cat &lt;&lt;+<br/>File "/root/packageNotice that using the -i option with -infoq causes rpm to generate more output than we have seen from it so far.txt" has been created<br/li>+<brli>Pick any five packages and run the </code> rpm -qi<ol/code><li value="8">Save, set permissions, and then run that shell script for command on all five at once. This should produce dozens of lines of output. Consider the application '''gedit'''. Did output it create that report? Try running the script without an argument would generate if we used - What did it do? a instead of picking just a few packages.<li>Use the <bli>Run <code>wgetrpm -qia</code>now.)</bli> command <li>That is way too much output for us to downloadreasonably search through, study, and run the following shell scripts on-line:but we have commands that can filter this down to a much more readable amount.<blockquote/li><bli>Try the command: <code><span style=" pointerrpm -qia | sed -eventsnre '/^Name&#91;&#91;:nonespace:&#93;&#93;cursor+:default/ p' -e '/^Install Date&#91;&#91;color:space:&#3366CC93;font-size:1.2em&#93;">https*://ict.senecacollege.ca/~ops245/labs/packageInfoGraphical.bash</span>p'</code><br /b></blockquote>Instead of printing every line, this will only display the name and install date of each package.</li><li>Try modifying that command to understand what this Bash Shell script doesdisplay only the package name and License.</li></ol>
'''Answer the Investigation 3 observations / questions in your lab log book.'''
932
edits