Open main menu

CDOT Wiki β

Changes

OPS245 Scripting Exercises Lab 2 dev

1,685 bytes added, 09:30, 26 January 2023
no edit summary
= Virtual Machine Backup Script =
== Key Concepts ==
=== Conditional Logic ===
Condition Logic aka Control Flow statements aka If statements are an important scripting and programming concept. They allow us to make scripts more flexible by building in decision making. You learned how to use these in BASH scripting in ULI101, but lets review the basics. You use an if statement to check whether a condition is met (or not, depending on how you write it) and execute some code (or not) based on that condition. Here is a sample if statement in Bash:
<pre>
#!/bin/bash
# Author: Jason Carman
# Date: January 26, 2023
# Purpose: Check to see if user is root, if they are not inform them they should run the script as root and exit with an error (non-zero) status code
# Usage: ./checkuser.bash
#
 
# Check the output of the whoami command to determine if they are root
if [[ $(whoami) == "root" ]]; then
echo "You are root"
# Else, they are not root. Display a message and exit with a non-zero status code
else
 
# Display a message indicating the script should be run as root
echo "You are not root. Please run the script with root permissions."
 
# Exit with a non-zero status code indicating an error
exit 1
fi
</pre>
 
=== To do ===
* Copy the above code and give it execute permission using the chmod command.
* Try running it as a regular user. What happens?
* After running it as a regular user, from the command line type the following and observer the output. <pre>echo $?</pre>
* Now try running the script using sudo. What happens?
* From the command line type the following and observe the output. What is different from the previous output? <pre>echo $?</pre>
 
== Overview ==
In Lab 2 Investigation 3 you performed a manual backup of all 3 virtual machines. While this is something both important and necessary, it's a tedious process. This is something you could automate through scripting. In this exercise you are going to write a script that will backup all three of your virtual machines. We will improve on this backup script in a later exercise. To begin, open your text editor (vi) on c7host and copy the following template in. Be sure to modify Author and Date with the correct information.