linux_wiki:use_shell_scripting_to_automate_system_maintenance_tasks

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

linux_wiki:use_shell_scripting_to_automate_system_maintenance_tasks [2016/10/08 17:51]
billdozor
linux_wiki:use_shell_scripting_to_automate_system_maintenance_tasks [2019/05/25 23:50]
Line 1: Line 1:
-====== Use Shell Scripting To Automate System Maintenance Tasks ====== 
- 
-**General Information** 
- 
-Review of common shell scripting syntax.  
- 
----- 
- 
-====== Lab Setup ====== 
- 
-The following virtual machines will be used: 
-  * server1.example.com (192.168.1.150) -> Create and test some scripts 
- 
----- 
- 
-====== General Layout ====== 
- 
-General bash script layout 
-<code bash> 
-#!/bin/bash 
- 
-echo "Hello world" 
-exit 0 
-</code> 
- 
----- 
- 
-====== Arguments ====== 
- 
-Sending and parsing arguments to scripts. 
-<code bash> 
-#!/bin/bash 
- 
-echo "The first argument is: $1" 
-echo "The second argument is: $2" 
- 
-echo "There are $# total arguments." 
-echo "All of the arguments are: $@" 
- 
-# Loop through arguments one at a time 
-for myarg in $@; do 
-  echo "Argument is: $myarg" 
-done 
-</code> 
- 
----- 
- 
-====== Getting User Input ====== 
- 
-User input with the read command 
-<code bash> 
-#!/bin/bash 
- 
-# If there is no argument passed, prompt for input 
-if [ -z $1 ]; then 
-  echo "Enter something: " 
-  read myname 
-else 
-  # There is an argument, use that 
-  myname=$1 
-fi 
- 
-echo "You entered: $myname" 
-exit 0 
-</code> 
- 
----- 
- 
-====== Conditionals ====== 
- 
-If/else conditional testing 
-<code bash> 
-if [ -f $1 ]; then 
-  echo "Argument is a file" 
-elif [ -d $1 ]; then 
-  echo "Argument is a directory" 
-else 
-  echo "Argument is something else..." 
-fi 
- 
-exit 0 
-</code> 
- 
-\\ 
-Logical operators 
-<code bash> 
-# Logical AND: If first command is true, execute second command 
-[ -f $1 ] && echo "This is a file" 
- 
-# Logical OR: If first command is true, do not execute second command 
-[ -f $1 ] || echo "This is not a file" 
-</code> 
- 
----- 
- 
-====== Loops ====== 
- 
-Iterating through data. 
- 
-===== For Loops ===== 
- 
-Useful for processing ranges of information. 
- 
-Specific amount of iterations 
-<code bash> 
-for (( count=1; count <= 100; count++ )); do 
-  echo "The count is: $count" 
-done 
- 
-exit 0 
-</code> 
- 
-\\ 
-Unknown iterations through a range of data 
-<code bash> 
-for node in $(cat system_list.txt); do 
-  ssh $node "uptime" 
-done 
-</code> 
- 
-\\ 
-Range of numbers 
-<code bash> 
-for number in {100..200}; do 
-  ping -c 1 192.168.1.$number > /dev/null && echo "192.168.1.$number is up" 
-done 
-</code> 
- 
-===== While Loops ===== 
- 
-While loops are useful for monitoring something or to repeat something forever and break under certain conditions. 
- 
-\\ 
-Loop infinitely 
-<code bash> 
-# Keep checking to see if a specific host is up (the first argument to the script) 
-while true; do 
-  ping -c 1 $1 
-   
-  # if the last command (ping) returns successful (0 exit code) 
-  if [[ $? -eq 0 ]]; then 
-    echo "$1 is up" 
-    exit 0 
-  else 
-    sleep 5 
-  fi 
- 
-done 
-</code> 
- 
-\\ 
-Monitor a process 
-<code bash> 
-# While output results are true, go back to sleep 
-while ps aux | grep $1 | grep -v grep; do 
-  sleep 5 
-done 
- 
-# Mail when no results from ps (process stopped) 
-echo "The process($1) is no longer running." | mail -s "Process $1 stopped" root 
-</code> 
- 
-===== Case Evaluation ===== 
- 
-Evaluate data and provide matches for expected values. 
- 
-<code bash> 
-case $1 in 
-  start) 
-    start_program 
-  ;; 
-  stop) 
-    stop_program 
-  ;; 
-  status) 
-    status_of_program 
-  ;; 
-  *) 
-    echo "Usage: $0 (start|stop|status)" 
-  ;; 
-esac 
-</code> 
- 
----- 
- 
-====== Debugging ====== 
- 
-To debug a script, execute it with a special argument: 
-<code bash> 
-bash -x myscript.sh 
-</code> 
-  * -x -> Shows line by line what the script is doing, which allows easier debugging when it breaks 
- 
----- 
  
  • linux_wiki/use_shell_scripting_to_automate_system_maintenance_tasks.txt
  • Last modified: 2019/05/25 23:50
  • (external edit)