Syadmin Bash Scripting 1471376912
Syadmin Bash Scripting 1471376912
The System
Administrator’s
Guide to Bash
Scripting
Contents Related Courses
Terminal Shortcuts 1
The System
Special Files 1 Administrator’s
Guide to Bash
Scripting
Wildcards 1
History 2
Need Help?
Screen 2
Executing Scripts 2
Linux Academy
I/O 3 Community
Redirection 3
Grouping Commands 4
Command Substitution 5
Jobs 5
Text Processing 6
Scripts 6
Basic Syntax 6
Shell 6
Shell Variables 7
Environment Variables 7
Aliases 11
If Statements 11
Basic Syntax 11
Else If Syntax 11
Case Statements 12
Basic Syntax 12
Operators 12
File Tests 13
String Tests 13
Arithmetic Tests 14
Misc Syntax 14
While Loop 14
Basic Syntax 14
For Loop 15
Basic Syntax 15
C-Like Syntax 15
Variables 16
Basic Syntax 16
Booleans 16
Arrays 17
Basic Syntax 17
Declaration 17
Assignment 17
Positional Parameters 18
Basic Syntax 18
Basic Syntax 19
Exit Statuses 20
Global Variable 20
In Conditional Statements 21
Create a Function 22
Basic Syntax 22
Call a Function 22
Positional Parameters 22
Return Codes 23
Checklist 23
Syslog Standard 24
Basic Syntax 24
Debugging 24
Exit on Error 25
Verbose Debugging 25
Manual Debugging 25
The System Administrator’s Guide to Bash Scripting Linux Academy
Terminal Shortcuts
• ] • up arrow • Shows the previous command; you can cycle through all previous commands
one by one if pressed repeatedly
• Tab • Tab completion; used to automatically complete long strings, such as file names or locations
Special Files
• . • Current directory
• .. • Parent directory
• ../ • Parent directory, including slash; used to navigate from the parent
• .hiddenfile • Files that start with a dot are hidden files; generally configuration files
Wildcards
• ? • Represents any one character
• [xbc] • Represents any one of the characters listed within the bracket
-1-
The System Administrator’s Guide to Bash Scripting Linux Academy
History
• View and edit commands previously run
• history • Shows a list of commands that have been previously run in a numbered list
• fc • Opens the previous command in a text editor; after closing the text editor, the modified
command will be ran in Bash
Screen
• Screen manager for multiple terminal screens
• screen -list • Lists all active screens with numeric ID and name
• screen -d <screen ID or name> • Detaches from the specified screen; returns you to the
original starting screen
• screen -r <screen ID or name> • Reattaches to the specified screen; opens the specified
screen
• screen -x <screen ID or name> • Multi-display mode or screen sharing mode; allows for
multiple users to view and send input to the same screen; requires at least two different sessions, with
both sessions are attached to the screen
• screen <command(s)> • Executes a command in a new screen and closes when it is finished
• exit • Terminates an open screen; logs user out if there are no other attached screens
Executing Scripts
• ./program.ext • Executes program from current directory
-2-
The System Administrator’s Guide to Bash Scripting Linux Academy
I/O
• Input / Output
Redirection
Redirect the output or input of a command into files, devices, and the input of other commands
• > • Redirects the standard output of a command into a file; replaces the contents of a file
• &> • Redirects standard error and standard output when redirecting text
• &>> • Appends standard error and standard output when redirecting text
• Example • cat < test.txt >> existingfile.txt
»» Uses the contents of test.txt on the cat command, then appends the results to existingfile.txt
Piping
• Processes commands on the output of other commands
• Uses the standard output of a command prior to the pipe as the standard input for the command
-3-
The System Administrator’s Guide to Bash Scripting Linux Academy
»» Lists all items in the current directory, then filters the results for the string test, then performs a file
removal with verbose output. This basically removes all files that have the string test in them.
»» ; • Run the following command even if the previous command fails or succeeds
»» && • Run the following command only if the previous succeeds or has no errors
»» || • Run the following command only if the previous fails or results in error
Grouping Commands
• Bash provides two ways to group a list of commands meant to be executed as a unit
• (list) • Parenthesis cause a subshell environment to be created; each of the commands in the
list will be executed within that subshell; because the list is executed within the subshell, variable
assignments do not remain after the subshell completes
• { list; } • Curly braces cause the list to be executed in the current shell; the semicolon at the
end of the list is required and white space must be added before and after the list
-4-
The System Administrator’s Guide to Bash Scripting Linux Academy
»» {0..12} => 0 1 2 3 4 5 6 7 8 9 10 11 12
»» {3..-2} => 3 2 1 0 -1 -2
»» {a..g} => a b c d e f g
»» {g..a} => g f e d c b a
• If the brace expansion has a prefix or suffix string, then those strings are included in the expansion:
»» a{0..3}b => a0b a1b a2b a3b
Command Substitution
• Inserts command output into another context
• $(Dollar Sign & Parenthesis) • Input any bash command or set of commands
• Examples:
»» echo the current date is `date` • Outputs the current date at the end of the string
»» file $(which login) • Outputs the file type of the located command file
»» echo "$(users | wc -w) users are logged in right now" • Outputs <number
of user> users are logged in right now
Jobs
• Commands run from the terminal, whether in the foreground or in the background
• In the terminal, while running a command, you can use CTRL+Z to send the command to the
background
• fg <job number> • Forground • Sends the specified job to the foreground of the terminal
• bg <job number> • Background • Sends the specified job to the background of the terminal
• <command> & • Run the command in the background, allowing you to run other commands while
it processes
• nohup • Run a command immune to hang-ups; allows a command to run even after a terminal is
closed or the user who ran the command is logged out
-5-
The System Administrator’s Guide to Bash Scripting Linux Academy
Text Processing
• "Double Quotation marks" • Meta-characters enclosed within the quotes are treated literally
with the exception of variables which have already been set.
• 'single quotation marks' • All meta-characters will be processed literally with no variable
processing
Scripts
• Contain a series of commands
• Anything you can type at the command line, you can put in a script
Basic Syntax
• #! /bin/bash
# Commands
Shell
Global Shell Configuration Files
• /etc/profile
• /etc/profile.d
• /etc/bashrc
• /etc/bash.bashrc
• /etc/skel • Contents of this directory are copied to new users directories when a new user is
created
-6-
The System Administrator’s Guide to Bash Scripting Linux Academy
user logs in
• ~/.bashrc • User-specific bash configuration file; executes whatever commands are within the file
(~/.bash_login) when a user logs in.
• ~/.bash_logout • Executes whatever commands are within the file (~/.bash_logout) when a
user logs out
Shell Variables
• set • Shows shell variables for the current instance of the running shell
• Set your own shell variables • EXAMPLE=VAR ; echo $EXAMPLE
»» Creates the shell variable EXAMPLE and sets the value to VAR, then prints the variable's value
• Remove shell variables • unset EXAMPLE ; echo $EXAMPLE
»» Removes the shell variable EXAMPLE; echo will show no display since $EXAMPLE is no longer
set to any value
Environment Variables
• env • Shows all environment variables
• env | grep EXAMPLE • Prints current environment variables and then greps the result for the
term EXAMPLE
• After you log off, the environment variables you set will restore to default; to permanently set an
environment variable, you must either edit the user configuration files or global configuration files for
bash
-7-
The System Administrator’s Guide to Bash Scripting Linux Academy
»» \h • hostname
»» \u • username
»» \t • 24 hour hh:mm:ss
»» \T • 12 hour hh:mm:ss
»» \n • New line
• Example • PS1='[`pwd`]$ '
»» Makes the shell prompt the path to current directory followed by the $ sign
-8-
The System Administrator’s Guide to Bash Scripting Linux Academy
• Color codes:
»» # Reset
»» Color_Off='\e[0m' # Text Reset
»» # Regular Colors
»» Black='\e[0;30m' # Black
»» Red='\e[0;31m' # Red
»» Green='\e[0;32m' # Green
»» Yellow='\e[0;33m' # Yellow
»» Blue='\e[0;34m' # Blue
»» Purple='\e[0;35m' # Purple
»» Cyan='\e[0;36m' # Cyan
»» White='\e[0;37m' # White
»» # Bold
»» BBlack='\e[1;30m' # Black
»» BRed='\e[1;31m' # Red
»» BGreen='\e[1;32m' # Green
»» BYellow='\e[1;33m' # Yellow
»» BBlue='\e[1;34m' # Blue
»» BPurple='\e[1;35m' # Purple
»» BCyan='\e[1;36m' # Cyan
»» BWhite='\e[1;37m' # White
»» # Underline
»» UBlack='\e[4;30m' # Black
»» URed='\e[4;31m' # Red
»» UGreen='\e[4;32m' # Green
»» UYellow='\e[4;33m' # Yellow
»» UBlue='\e[4;34m' # Blue
»» UPurple='\e[4;35m' # Purple
»» UCyan='\e[4;36m' # Cyan
-9-
The System Administrator’s Guide to Bash Scripting Linux Academy
»» UWhite='\e[4;37m' # White
»» # Background
»» On_Black='\e[40m' # Black
»» On_Red='\e[41m' # Red
»» On_Green='\e[42m' # Green
»» On_Yellow='\e[43m' # Yellow
»» On_Blue='\e[44m' # Blue
»» On_Purple='\e[45m' # Purple
»» On_Cyan='\e[46m' # Cyan
»» On_White='\e[47m' # White
»» # High Intensity
»» IBlack='\e[0;90m' # Black
»» IRed='\e[0;91m' # Red
»» IGreen='\e[0;92m' # Green
»» IPurple='\e[0;95m' # Purple
»» ICyan='\e[0;96m' # Cyan
»» IWhite='\e[0;97m' # White
»» BIRed='\e[1;91m' # Red
»» BIGreen='\e[1;92m' # Green
»» BIYellow='\e[1;93m' # Yellow
»» BIBlue='\e[1;94m' # Blue
»» BIPurple='\e[1;95m' # Purple
»» BICyan='\e[1;96m' # Cyan
»» BIWhite='\e[1;97m' # White
»» On_IRed='\e[0;101m' # Red
»» On_IGreen='\e[0;102m' # Green
- 10 -
The System Administrator’s Guide to Bash Scripting Linux Academy
»» On_IYellow='\e[0;103m' # Yellow
»» On_IBlue='\e[0;104m' # Blue
»» On_IPurple='\e[0;105m' # Purple
»» On_ICyan='\e[0;106m' # Cyan
»» On_IWhite='\e[0;107m' # White
Aliases
• Use to set a string to use for another command
If Statements
Basic Syntax
• if [ condition ];
then
#commands to be run if true
else
#commands to be run if false
fi
Else If Syntax
• When using else if within an if statement, you want to use elif
• if [ condition ];
then
#commands to be run if true
elif [ condition ];
then
#commands to be run if true
else
#commands to be run if false
fi
Case Statements
• Case statements are used to check the value of a parameter and execute code depending on the value
• This is similar to the switch statement in other languages with some slight differences:
»» Where you would use case, instead list the pattern followed by a closing parenthesis
Basic Syntax
• case "$VAR" in
pattern_1 )
# Commands to be executed
;;
pattern_2 )
# Commands to be executed
;;
* )
# Default
;;
esac
Operators
• <EXPRESSION1> && <EXPRESSION2> • True if both expressions are true
• <EXPRESSION1> || <EXPRESSION2> • True if at least one expression is true; do not use with
-o
• <STRING> =~ <ERE> • <STRING> is checked against the extended regular expression <ERE>;
- 12 -
The System Administrator’s Guide to Bash Scripting Linux Academy
true on a match
File Tests
• -a <FILE> • True if <FILE> exists; may cause conflicts
• <FILE1> -ef <FILE2> • True if <FILE1> and <FILE2> refer to the same device and inode
numbers
String Tests
• -z <STRING> • True if <STRING> is empty
- 13 -
The System Administrator’s Guide to Bash Scripting Linux Academy
Arithmetic Tests
• <INTEGER1> -eq <INTEGER2> • True if the integers are equal
• <INTEGER1> -le <INTEGER2> • True if the first integer is less than or equal second one
• <INTEGER1> -ge <INTEGER2> • True if the first integer is greater than or equal second one
• <INTEGER1> -lt <INTEGER2> • True if the first integer is less than second one
• <INTEGER1> -gt <INTEGER2> • True if the first integer is greater than second one
Misc Syntax
• <TEST1> -a <TEST2> • True if <TEST1> and <TEST2> are true; -a may also be used as a file
test
• ( <TEST> ) • Group a test (for precedence); in normal shell-usage, parentheses must be escaped;
use "\(" and "\)"
• -v <VARIABLENAME> • True if the variable <VARIABLENAME> has been set; use var[n] for
array elements
• -R <VARIABLENAME> • True if the variable <VARIABLENAME> has been set and is a nameref
variable (since 4.3-alpha)
While Loop
Basic Syntax
• while [ condition ] do
#command(s)
- 14 -
The System Administrator’s Guide to Bash Scripting Linux Academy
#increment
done
• Example:
»» x=1
while [ $x -le 5 ]
do
echo "Welcome $x times"
x=$(( $x + 1 ))
done
»» The above loop will run a command while x is less than or equal to 5
For Loop
Basic Syntax
• for arg in [list]
do
#command(s)
done
• During each pass through the loop, arg takes on the value of each successive variable in the list
• Example:
»» for COLOR in red green blue do
echo "COLOR: $COLOR"
done
»» # Output:
# Color: red
# Color: green
# Color: blue
C-Like Syntax
• for (( expression1; expression2; expression3 )) do
# Command 1
# Command 2
# Command 3
done
- 15 -
The System Administrator’s Guide to Bash Scripting Linux Academy
»» Expression1 • The first expression in the list is only checked the first time the for loop is ran.
This is useful for setting the starting criteria of the loop.
»» Expression2 • The second expression is the condition that will be evaluated at the start of
each loop to see if it is true or false.
»» Expression3 • The last expression is executed at the end of each loop. This comes in handy
when we need to add a counter.
• Example:
»» for (( SECONDS=1; SECONDS <= 60; SECONDS++ )) do
echo $SECONDS
done
Variables
• Because everything in bash is case sensitive, it is best practice to make variables in ALL CAPS
Basic Syntax
• Cannot start with a digit
Booleans
• Booleans are simple in bash, just declare a variable and assign it a true or false value
• VAR_NAME=true
• VAR_NAME=false
- 16 -
The System Administrator’s Guide to Bash Scripting Linux Academy
»» 1 = false
Arrays
Basic Syntax
• Cannot start with a digit
Declaration
• ARRAY=() • Declares an indexed array ARRAY and initializes it to be empty; this can also be used
to empty an existing array
• ARRAY[0]= • Generally, sets the first element of an indexed array; if no array ARRAY existed
before, it is created
• declare -a ARRAY • Declares an indexed array ARRAY; an existing array is not initialized
• declare -A ARRAY • Declares an associative array ARRAY; this is the one and only way to create
associative arrays
Assignment
• ARRAY[N]=VALUE • Sets the element N of the indexed array ARRAY to VALUE; N can be any
valid arithmetic expression
• ARRAY[STRING]=VALUE • Sets the element indexed by STRING of the associated array ARRAY
• ARRAY=VALUE • As above; if no index is given, as a default, the zeroth element is set to VALUE;
this is also true of associative arrays; there is no error if no key is specified, and the value is assigned
to string index 0
• ARRAY=(E1 E2 …) • Compound array assignment; sets the whole array ARRAY to the given list
of elements, indexed sequentially, starting at zero; the array is unset before assignment unless the +=
operator is used; when the list is empty (ARRAY= ()), the array it set to an empty array; this method
does not use explicit indexes and an associative array cannot be set like this; clearing an associative
array using ARRAY=() works
- 17 -
The System Administrator’s Guide to Bash Scripting Linux Academy
Positional Parameters
• For when you need to pass arguments to your scripts at the command line
• Positional parameters:
»» $FUNCNAME • The function name; inside a function, $0 is still the $0 of the shell, not the
function name
Basic Syntax
• Example • script.sh parameter1 parameter2 parameter3
»» $0 = "script.sh"
- 18 -
The System Administrator’s Guide to Bash Scripting Linux Academy
»» $1 = "parameter1"
»» $2 = "parameter2"
»» $3 = "parameter3"
• Example:
»» #! /bin/bash
echo $1
#This echos the first argument after the script name
echo -e "\n" #New Line
echo $2
#This echos the second argument after the script name
echo -e "\n" #New Line
echo $3
#This echos the third argument after the script name
echo -e "\n" #New Line
• Output:
Basic Syntax
• read -p "Prompt" VARIABLE_TO_BE_SET
• Example:
»» #! /bin/bash
read -p "Type Your Username" USERNAME
echo -e "\n"
read -p "Type The IP Address" IPADDR
echo -e "Logging into host $IPADDR with user \"${USERNAME}\" \n"\
ssh -p 22 ${IPADDR}@${USERNAME}
- 19 -
The System Administrator’s Guide to Bash Scripting Linux Academy
• To have formatted text at the command line, you need to know the escape sequences for echo
• Escape sequences:
»» echo -e " text <escape sequence> text
»» \a • Alert (bell)
»» \b • Backspace
»» \f • Form feed
»» \n • Newline
»» \r • Carriage return
»» \v • Vertical tab
»» \\ • Backslash
Exit Statuses
• The error status of a command; all commands return an exit status; this allows for granular control of
your scripts based on those statuses
• In Bash, there are up to 255 exit statuses with 0 being the first
• Exit status meanings:
»» 0 • Success
»» 1 • General Errors
»» 2 • Misuse of Shell Built-ins; syntax errors, missing keyword or command permission errors, etc
»» Other • Error
Global Variable
• To reference the exit status of a script use $?
• Exit statuses are numbered, so when you reference the variable $?, you get one of those numbers
• Example:
»» #! /bin/bash
ls /path/does/not/exist
- 20 -
The System Administrator’s Guide to Bash Scripting Linux Academy
echo "$?"
## Output of (echo "$?") = 2
In Conditional Statements
• In most cases, you use exit statuses within a conditional statement to perform an action based on
whether your program is having errors or not
• Example:
»» #! bin/bash
HOST="google.com" ping c
1
$HOST
if [ "$?" e
q "0"] then
echo "$HOST is reachable" else
echo "$HOST is unreachable"
fi
»» Because we're able to successfully ping google, our exit status would be 0
»» We ask if our exit status is equal to 0, because if it is our output would be google.com is
reachable
• In Bash, there are two logical operators that can take the place of some conditional statements:
»» command && command • The second command will only run if the previous command
succeeds
»» command || command • The second command will only run if the previous command fails
• To tell bash to halt execution of a script and return an exit status, you would use the exit command
Basic Syntax
• exit <exit status number>
• Example:
»» #! /bin/bash HOST="google.com" ping c
1
$HOST if ["$?" n
e "0"] then
echo "$HOST is unreachable"
exit 1
fi
- 21 -
The System Administrator’s Guide to Bash Scripting Linux Academy
exit 0
»» This pings google.com with one packet, then it asks if the exit status is not equal to 0
Create a Function
• Functions are blocks of reusable code; used when you need to do the same tasks multiple times
Basic Syntax
• myFunction { # Code Goes Here }
• myFunction() {
# Code Goes Here
}
Call a Function
• Unlike other languages, when you call a function in Bash you do not use parenthesis
• myfunction parameter1 parameter2 parameter3
Positional Parameters
• In functions, it’s possible to use positional parameters as arguments for your functions
• To use positional parameters, you must first reference them within your function
• Once defined, you can use your function with arguments that take on the place of the parameters
• Example:
»» function myfunction () {
# echo -e "$1 \n"
# echo -e "$2 \n"
# echo -e "$3 \n"
# echo -e "$4 \n"
}
- 22 -
The System Administrator’s Guide to Bash Scripting Linux Academy
Return Codes
• Each function has an exit status and functions have their own method of dealing with exit statuses
• By default, the return code of a function is simply the exit status of the last command executed within
the function
• functionName() {
# Code Goes Here
return <Return Code>
}
Checklist
• Does your script start with a shebang?
»» #/bin/bash
• Does your script include a comment describing the purpose of the script?
»» # This script creates a backup of every MySQL database on the system.
• Are the global variables declared at the top of your script, following
the initial comments?
»» DEBUG=true
HTML_DIR=/var/www
• Have you grouped all of your functions together following the global
variables?
»» if [ ! d "$HTML_DIR" ]; then
echo "$HTML_DIR does not exist. Exiting."
exit 1
fi
• #!/bin/bash
#
# Replace with the description and/or purpose of this shell script.
GLOBAL_VAR1="one"
GLOBAL_VAR2="two"
function function_one() {
local LOCAL_VAR1="one"
# Replace with function code.
}
Syslog Standard
• The syslog standard uses facilities and severities to categorize messages.
• Facilities • kern, user, mail, daemon, auth, local0 to local7
• Severities • emerg, alert, crit, err, warning, notice, info, debug
• Log file locations:
»» /var/log/messages
»» /var/log/syslog
Basic Syntax
• logger -p facility.severity "Message information"
• logger -t tagname -p facility.severity "Message information"
• Example:
»» logger -p local10.info "Information: You are a pretty cool dude"
»» logger -t myscriptname -p local10.info "Swagnificent"
Debugging
- 24 -
The System Administrator’s Guide to Bash Scripting Linux Academy
• For detailed information regarding debugging tools for bash, use the help set command
Exit on Error
• Exit on error immediately halts the execution of code if any command within the script has a non-
zero exit status
Verbose Debugging
• The -v option prints shell input lines as they are read
• The verbose option is similar to x-tracing; however, variables and regex are not expanded
Manual Debugging
• With manual debugging, we create our own debugging code
• Normally, we create a special variable known as DEBUG to inform our script whether debugging is
- 25 -
The System Administrator’s Guide to Bash Scripting Linux Academy
on or off
• #!/bin/bash DEBUG=true
if $DEBUG
then
echo "Debug Mode On." else
echo "Debug Mode Off."
fi
- 26 -