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/09/21 22:58]
billdozor created
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.  
- 
----- 
- 
-====== 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> 
- 
----- 
  
  • linux_wiki/use_shell_scripting_to_automate_system_maintenance_tasks.txt
  • Last modified: 2019/05/25 23:50
  • (external edit)