Documente Academic
Documente Profesional
Documente Cultură
Basic Scripting
This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses.
Copyright Network Development Group 2013.
Exam Objective
3.3 Turning Commands into a Script
Objective Summary
Basic Text Editing
Basic Shell Scripting
This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses.
Copyright Network Development Group 2013.
Text Editors
This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses.
Copyright Network Development Group 2013.
A script is a sequence of commands
This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses.
Copyright Network Development Group 2013.
nano
Type as normal, use arrow keys to
move around
Most commands are Control +
another character
Context sensitive commands are at
the bottom of the screen, e.g. ^X to
exit, ^W to find
^G gets you help anywhere
This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses.
Copyright Network Development Group 2013.
shebang
This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses.
Copyright Network Development Group 2013.
#!
#! is the shebang (hash + bang)
Tells the kernel what shell interpreter
to use to run the script
Must be on the first line
#!/bin/sh
#!/usr/bin/ruby
Lets you run the script with ./script
no matter which shell the user has
This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses.
Copyright Network Development Group 2013.
Working with Variables
This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses.
Copyright Network Development Group 2013.
Variables
Temporary storage of data in
memory
Assigning (no $):
FOO=hello
CWD=`pwd`
BAR=Hello $NAME
Using (need a $):
$FOO
echo Hello $NAME
This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses.
Copyright Network Development Group 2013.
Special Variables
$1..$9 are the arguments to the
script
./test.sh hello there # $1=hello
$2=there
$? is the exit code of the last
command to be run
use exit 1 to exit your own script
with error code 1
This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses.
Copyright Network Development Group 2013.
Working with Conditionals
This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses.
Copyright Network Development Group 2013.
Conditionals
if something; then # something returns
do this an exit code. If it is 0,
fi then do this will be
executed up until fi
if something; then
do this
elif something else; then # alternative syntax,
do that allows for multiple
else tests and a default
try this option if you want
fi
This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses.
Copyright Network Development Group 2013.
tests
test f /tmp/foo # test if file exists
test ! f /tmp/foo # test if file doesnt exist
test $A eq 1 # is $A = 1 (numeric)
test $B = Hello # string comparison
test $A lt 10 # $A < 10?
This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses.
Copyright Network Development Group 2013.
The for loop
Operates over a fixed set of items or a
glob
NAMES=Alice Bob Charlie
for N in $NAMES; do
echo Hello $N
done
for FILE in *; do
ls l $FILE
done
This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses.
Copyright Network Development Group 2013.
While loops
Tests the loop condition each time
while [ ! f /tmp/foo ]; do
try_to_make_foo # makes /tmp/foo on
success
sleep 1 # wait 1 sec
done
Used when the number of loops is
unknown or changes each loop
This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses.
Copyright Network Development Group 2013.