As a systems administrator you should automate as many tasks as possible
This provides documentation on how to perform the tasks
This provides uniformity in product delivery.
Type-o problems
Forgetting how to solve a problem.
Teaching others to solve a problem.
I have lived in a strange systems-admin world but my users could handle errors and mistakes, but not inconsistency.
Automating tasks will eliminate a fair amount of inconsistency
The simplest script is just a set of command line entries in a file.
First line #!/bin/bash
Other lines starting with # are comments.
echo is the "print" command.
Look at simple.sh
Run with
bash simple.sh
This will run the command in a sub shell
Or make executable and run it.
chmod u+x simple.sh
simple.sh
This will run the script in a subshell
Or source
This will run the script in the current shell.
Try this with two.sh
Bash Variables (vars.sh)
Set with var=value
No spaces around equal sign.
foo=1
value must be in quotes if it contains a space.
bar=5+$foo
If it is an operation on another variable, the other variable
a="hello world"
Variables inside of quotes are interpreted
b="$a and another $a"
Variables that don't exist have a null value.
But you can unset a variable.
unset $b
And if your value includes a $, escape it with \
c=\$a
You can also escape "
d="\"hello world\""
BASH has MANY built in variables.
With many different formats
Look at the options for the prompt, page 70
"bash has some of the most advanced programming capabilities of any command line interpreter of its type. Although its syntax is nowhere near as elegant or consistent as that of most conventional programming languages." (Page 81)
Functions
Two possible methods to declare
function name {
commands
}
name () {
commands
}
Either in a script or on the command line.
For the command line
fun3() {
echo hello
echo world
}
declare -F lists all defined functions.
declare -f fun3 gives code for fun3
declare -f gives code for all functions.
unset -f funname will remove from environment.
For my script, funky.sh
echo -e enable interpretation of backslash escapes
I wanted tabs
Arguments to Functions
And to scripts.
$@ is the list of all arguments
$# is the number of arguments
$0 is argv[0], $1 is the first argument or argv[1], ...
These are local to the individual function /script