We will continue with using shell scripts 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).
If you require <u>'''additional practice'''</u> in additional shell scripts skills, run the commands in your '''Matrix''' account: <ul><li>'''N/A'''</li></ul>
Perform the following steps in your c7host machine:
# Open a Bash shell terminal and login as root.
<code style="color:#3366CC;font-family:courier;font-size:.9em;margin-left:20px;">
<br>
::#!/bin/bash <br>::<br>::::# packageInfo.bash<br>::# Purpose: Generates a report to displaying specified information of installed software<br>::#<br># USAGE: ./packageInfo.bash<br>::#<br>::# Author: *** INSERT YOUR NAME ***<br>::# Date: *** CURRENT DATE ***<br>::<br>::::if [ $HOME != "root" ] # only runs if logged in as root<br>::then<br> echo "You must be logged in as root." >&2<br>:: exit 1<br>::fi<br>
</code>
<br>
<code style="color:#3366CC;font-family:courier;font-size:.9em;">
::if [ $# -ne 1 ]<br>::then<br>:: echo "Your command must have a application-name as argument" >&2<br>:: echo "USAGE: $0 [application-name]" >&2<br>:: exit 1<br>::fi<br><br>::# Creates Create report title (echo with -e option allows newline \n character to be used)<br><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>
<br>
<code style="color:#3366CC;font-family:courier;font-size:.9em;">
::# Clear screen and use Here Document to display select on report items to read into variable<br><br>::cat <<+<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>::# Convert spaces to pipe symbol (|)<br>::processedChoice=$(echo $choice | sed 's/ /|/g')<br><br>::# Use sed with extended regular expressions to only print those matching report elements<br>::rpm -qi $1 | sed -r -n "/($processedChoice)/ p" >> /root/package-info.txt<br><br>::cat <<+<br>::File "/root/package-info.txt" can been created<br>::+<br>
</code>