Lab2 Shell Script Demo 2

From CDOT Wiki
Revision as of 10:52, 17 January 2015 by Msaul (talk | contribs)
Jump to: navigation, search
#!/bin/bash

# ATTENTION: This script is still a demo creating a file with
#            imaginary settings for VMs!

# vm-stop.bash
#
# Author: Murray Saul
# Date: January 17, 2015
#
# Purpose: To allow a Linux sysadmin to identify status of
#          VMs on system and stop them...

# Check to see if logged in as root to be able to create file
# in /root/ directory...

if [ $USER != "root" ]
then
   echo "You must be logged in as root to run the command."
   echo "Either login as root or issue command \"sudo ./report1.bash\""
   exit 1
fi

# Create dummy virsh list file (virsh list --all) for testing purposes
# to incoporate the zenity command to list status of VMs and
# use checkbox to check or uncheck based on their status in order
# to launch VM that are only "shut-off"

cat > vm-status.txt <<+
Id     Name                           State
----------------------------------------------------
 1     practical1                     running
 2     centos1                        running
 -     centos2                        shut off
 -     centos3                        shut off
+
 
awk 'NR > 2 {print}' vm-status.txt > vm-status2.txt

# Using zenity (dialog box constructor)
# Prompts user for elements to be included in the report...
# Activated check box returns values (multiple values | symbol )...

items=$(zenity --height 320 --width 290 --text "<b>Current status of VMs:\n(VMs running are selected to stop):</b>\n" --list --checklist --column "Session Type" --column "Description" $(awk -F" " '{if ($3 == "running") {print "TRUE " $2 "-running"} else {print "FALSE " $2 "-shutdown"}}' vm-status2.txt))
 
# Replace pipe "|" with space, and store as positional parameters
set $(echo $items | sed "s/|/ /g") > /dev/null 2> /dev/null

for x          # Run loop for each positional parameter to launch application
do
 
  virsh stop $x

done


# End of Bash Shell Script

[ Scripting Compatibility for Current OPS235 labs: (Week-by-week) ] [ OPS235 - Curriculum Discussion ] [ OPS Stream Discussion ]