Linux Systems Administration Shell Scripting Basics Mike Jager Network Startup Resource Center Mike - Jager@synack - Co.nz
Linux Systems Administration Shell Scripting Basics Mike Jager Network Startup Resource Center Mike - Jager@synack - Co.nz
Linux Systems Administration Shell Scripting Basics Mike Jager Network Startup Resource Center Mike - Jager@synack - Co.nz
These materials are licensed under the Creative Commons Attribution-NonCommercial 4.0 International license
(http://creativecommons.org/licenses/by-nc/4.0/)
Cleanup
Run as root, of course
.
Do not run these: demo only!
#
#
#
#
cd /var/log
cat /dev/null > messages
cat /dev/null > wtmp
echo "Logs cleaned up.
Hash Bang
#! and the shell (first line only)
chmod a+x (remember the permissions)
Example: put the following text in hello.sh
#!/bin/bash
echo Hello World
Variables
shell searches PATH for programs if you do not type them with an absolute path
$ echo pwd
$ echo $( pwd )
cd $LOG_DIR
cat /dev/null > messages
cat /dev/null > wtmp
echo "Logs cleaned up.
exit # The right and proper method of "exiting" from a script.
Conditionals
if expressionthen statement
if expressionthen statement1else statement2.
if expression1then statement1else ifexpression2
then statement2else statement3
Loops
Sample Syntax
#!/bin/bash
for i in $( ls ); do
echo item: $i
done
#!/bin/bash
COUNTER=0
while [ $COUNTER -lt 10 ]; do
echo The counter is $COUNTER
let COUNTER=COUNTER+1
done
#!/bin/bash
COUNTER=20
until [ $COUNTER -lt 10 ]; do
echo COUNTER $COUNTER
let COUNTER-=1
done
Practice
Write a shell script to print the disk usage every 5
seconds.
Extras
Programming (say in C) builds on similar
concepts.
Source text is COMPILED into binary
machine code. Why?