932
edits
Changes
→INVESTIGATION 3: Using Shell Scripting to Generate System Information Reports: - reducing bash scripting content.
'''Answer Investigation 2 observations (all parts and questions) in your lab log book.'''
= INVESTIGATION 3: Using the Shell Scripting to Generate System Information Reports =
{|width="40%" align="right" cellpadding="10"
|- valign="top"
:You may have learned about creating and running Bash Shell Scripts in your ULI101 course. Shell scripts help Linux users and system administrators to automate repetitive tasks to become more efficient and to help them save time. In order to avoid confusion with syntax, you will not be writing bash scripts in this course. You will be reviewing and building a basic Bash Shell script however still run bash commands to perform various tasks, e.g. to generate information reports for your newly-installed Linux host machine.
<ol><li value="4">Refer to the Bash Shell Scripting Reference Guide prior to proceeding with this section. As you continue, you are required to make Bash Shell scripting notes in your lab1 logbook.</li>
<li>Create a directory called bin in your home directory to store your shell scripts by issuing the command:<br><b><code><span style="color:#3366CC;font-size:1.2em;">mkdir ~/bin</span></code></b></li>
<li>Change to that newly-created '''bin''' directory</li>
</ol>
<ol>
<li value="7">Launch a text editor (such as <b><code><span style="color:#3366CC;font-size:1.2em;">vim</span></code></b> or <b><code><span style="color:#3366CC;font-size:1.2em;">nano</span></code></b>) Using output redirection, send the output from each of the following commands to create a Bash Shell script file called: <b><code><span style="color:#3366CC;font-size:1report.2em;">myreporttxt.bash</span></code></b> in your current directory.</li><li>Copy and paste the text below into your vi editing session for your file report.bash<br> (how do Note that when you copy and paste efficiently in Linux?)<br></li></ol> <code style="color:#3366CC;font-family:courier;font-size:.9em;margin-left:20px;font-weight:bold;"><br>#!/bin/bash<br><br># Author: *** INSERT YOUR NAME ***<br># Date: *** CURRENT DATE ***<br>#<br># Purpose: Creates system info report<br>#<br># USAGE: ./myreport.bash<br><br>if [ $(whoami) != "root" ] # if not logged in as root<br>then<br> echo "You must be logged in as root." >&2<br> exit 1<br>fi<br></code><br><ol><li value="9">Save your editing sessionare done, assign the '''myreport.bash''' you should have one file read and execute permissions (at least for the owner) and run by typing:<br><b><code><span style="color:#3366CC;font-size:1.2em;">./myreport.bash</span></code></b></li><li> Did it work?</li><li>Run it again with <b><code><span style="color:#3366CC;font-size:1.2em;">sudo ./myreport.bash</span></code></b></li><li>Reopen your text-editing session for '''~/bin/myreport.bash''' and add the following lines that has output from all of code to the bottom of the shell script file:</ol><br><code style="color:#3366CC;font-family:courier;font-size:commands.9em;font-weight:bold;"># Create report title<brul><br>echo "SYSTEM REPORT" <brli>echo "Date: $(date +'%A %B %d, %Y (%I:%M %p)')" <br>echo <br></code><brli><ol><li value="13">Save and run the bash shell script (remember to use sudo to run it as root). You should see the output printed to your screen. Use a file redirection <b>></b> to save the output into a file called '''report.txt''' (I hope you are using the up arrow key to issue previously issued commands in order to save time!). 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:hostname</li></ol><br><code style="color:#3366CC;font-family:courier;font-size:.9em;font-weight:bold;">echo "Hostname: $(hostname)" <brli>echo <br>echo "Kernel Version: $(uname -rv)" <br>echo <br></code><brli><ol><li value="15">Save, run the script (redirecting the output once again), and view the ''report.txt'' contents (are you using tip that was given to save time?).ps aux</li><li>Edit the shell script and include output from the <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;">ip address show</span></code></b> commands (with appropriate titles). </li><li>Save, run and confirm that the shell script is working correctly.</li><liul>What would be the use of keeping this shell script as a Linux system administrator?</li></ol><ol><li value="19">Here are some more "complex" Bash Shell scripts, that perform View the same task''report. Although you are not required txt'' contents. You should be able to understand some of these other tricksthem because you just put that content there, it is recommended that but what would this look like if you view look at the contents of the scripts and save them for future consideration or examples.</li><li>The <b><code>wget</code></b> command can be used to quickly download files file several months from the Internet. Issue the following command:<br><b><code><span now? style="pointer-events: none;cursor: default;color:#3366CC;font-size:1.2em;">wget https://ict.senecacollege.ca/~ops245/labs/text-report.bash</span></code></b></li><li>Verify that the file '''text-report.bash''' was downloaded In order to your current directory.</li><li>Assign read and execute permissions for make this file by issuing more readable, use the command: <b><code><span style="color:#3366CC;font-size:1.2em;">chmod u+rx text-report.bash</span></code></b></li><li>Run this Bash Shell script by issuing the command: <b><code><span style="color:#3366CC;font-size:1.2em;">./text-report.bash</span></code></b></li><li>Check line to see if it created add a report in your current directory. What is blank line between the purpose of the report?</li><li>Use the <b>vi</b> text editor to view the contents of the file <b>text-report.bash</b>. Can you understand how this script works?<br><br></li><li>Use the <b><code>wget</code></b> output from each command to download, study, and run a header before each command briefly describing what the following shell scripts onoutput is (note that this will likely require re-line:<br><b><code><span style=" pointer-events:none;cursor:default;color:#3366CC;font-size:1.2em;">https://ict.senecacollege.ca/~ops245/labs/report.bash<br> https://ict.senecacollege.ca/~ops245/labs/report3.bash</span></code></b><br><br></li><li>Try to understand what running all of these Bash Shell scripts docommands).</li></ol>
'''Answer Investigation 3 observations (all parts and questions) in your lab log book.'''