Open main menu

CDOT Wiki β

Changes

OPS235 Lab 1 - CentOS7

122 bytes added, 13:12, 1 April 2015
no edit summary
# Enter the following text content into your text-editing session:
<pre span style="font-family:courier;font-weight:bold;padding-left:4px;margin-left:15px;">#!/bin/bash # Forces script to run in the bash shell<br><br># Author: *** INSERT YOUR NAME ***<br># Date: *** CURRENT DATE ***<br>#<br># Purpose: To present sysadmin to create an software inventory<br># report containing selected elements<br>#<br># USAGE: ./report.bash<br><br><br>if [ $USER != "root" ] # checks to see if user is root, and exits script if not<br>then<br> echo "You must be logged in as root to run the command." >&2 # >&2 is a "redirection trick" to convert output as stderr<br> echo "Either login as root or issue command \"sudo ./report1.bash\"" >&2<br> exit 1<br>fi<br></prespan>
<ol><li value="4">Save your editing session, assign your report.bash file read and execute permissions (at least for the owner) and run by typing: <b><code><span style="color:#3366CC;font-size:1.2em;">./report.bash</span></code></b></li><li>Did it run? If not what do you think you need to do in order to run the Bash Shell Script?</li><li>Issue the command <b><code><span style="color:#3366CC;font-size:1.2em;">su</span></code></b> and run the Bash shell again. Did it work?</li><li>Reopen your text-editing session for report.bash and add the following lines of code to the bottom of the shell script file:</ol>
<pre span style="font-family:courier;font-weight:bold;padding-left:4px;margin-left:15px;"># Create report title<br><br>echo "SOFTWARE ASSET REPORT FOR INSTALLED LINUX SYSTEM" > /root/report.txt<br>echo "Date: $(date +'%A %B %d, %Y (%H:%M:%p)')" >> /root/report.txt<br>echo >> /root/report.txt<br><br></prespan>
<ol><li value="8">Save and run the bash shell script. View the contents of the file called "report.txt" that was generated. Notice how the redirection symbol &gt; is used at the beginning of the report, and then the other redirection symbol &gt;&gt; is used to help "grow" the report with the other content.</li><li>The only remaining content of the report would be the system information. We can use a shell scripting trick called "command substitution" $( .. ) in order place results from an command to be used by another command (like echo). Re-edit the shell script and add the following code at the bottom of the shell script file:</li></ol>
<pre span style="font-family:courier;font-weight:bold;padding-left:4px;margin-left:15px;">echo >> /root/report.txt<br>echo "Hostname: $(hostname)" >> /root/report.txt<br>echo >> /root/report.txt<br>echo >> /root/report.txt<br>echo "Kernel Version: $(uname -rv)" >> /root/report.txt<br>echo >> /root/report.txt<br></prespan>
<ol><li value="10">Save, run the script, and view the resulting file contents.</li><li>Edit the shell script and issue the build the report to include empty lines, titles, and content for the remaining commands: <b><code><span style="color:#3366CC;font-size:1.2em;">ps aux</span></code></b> and <b><code><span style="color:#3366CC;font-size:1.2em;">ifconfig</span></code></b>.</li><li>Save, run and confirm that the shell script is working correctly.</li><li>What would be the use of keeping this shell script as a Linux system administrator?</li></ol>
13,420
edits