Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Overview
Overview
Objectives
1. Understand the Course Project
2. Use Basic Script Elements
3. Use Variables and Command Substitution
4. Use Control Structures
5. Use Arithmetic Operators
6. Read User Input
7. Use Arrays
8. Finalize the Course Project
9. Use Advanced Scripting Techniques
10. Learn About Useful Commands in Shell
Scripts
chmod +x script.sh
All other lines are headers which carry meta data and
transportation details of the mail
Variables
Variables
geeko@linux-rwke:~/Shell_Scripting> ./variables_1.sh
Hello, my name is Geeko
Variables
Command Substitution
Command Substitution
Request Tracker
Introduction
Create Loops
Create Branches
Create Branches
Request Tracker
Create Branches
Create Branches
Create Branches
Create Branches
case $number in
10) echo The value is 10;;
20) echo The value is 20;;
*) echo I don't know
esac
Create Loops
Can use the for loop where the line with for
determines how many times to execute the
commands
Create Loops
for i in 1 2 3
do
echo $i
done
Create Loops
#!/bin/bash
#for_loop_2.sh
for i in *
do
lower=`echo $i | tr | [upper:] [:lower;]`
mv $i $lower
done
Create Loops
Create Loops
Create Loops
#!/bin/bash
#while_loop_1.sh
...
while read line
do
if echo $line | grep '#' > /dev/null
then
echo $line | grep '#' | cut -d '|' --output-delimeter=', ' -f '2 3 4'
fi
done < /var/spool/mail/geeko
Create Loops
Explanation of while_loop_1.sh
The file /var/spool/mail/geeko is fed into
a while loop using input redirection (<)
Create Loops
Explanation of while_loop_1.sh
In the loop, grep tests is a line contains a #
--output-delimiter=','
- Defines that a , separates the output fields
-f '2 3 4'
- Lets cut only output the fields 2, 3 and 4
Overview
Request Tracker
Overview
Request Tracker
Use Arrays
Overview
Use Arrays
Request Tracker
then
lines[$reqnr]=#reqnr|$line
((reqnr++))
done < /var/spool/mail/geeko
Use Arrays
Request Tracker
#array_1.sh
...
do
reqnr=1
...
for (9i=1;i<=${#lines[@]};i++))
do
echo -e $number|$line | grep '#' | cut -d '|' --outputdelimeter=', ' -f '1 2 3 4 5'
done
unset lines
read input
done
Use Arrays
Request Tracker
In the while loop the requests are stored in the array $lines
and the value of the index $reqnr is incremented in every
cycle of the while loop
At the end of the while loop the array lines are deleted
using unset command
Introduction
Basic Syntax
functionname () {
commands
commands
}
commands
commands
}
...
mcd /tmp/new_directory
a)
b)
echo The option -b was used. ;;
c ) option_c=$OPTARG
echo The option -c has been set. ;;
esac
done
echo $option_c
Introduction
Same as +%Y-%m-%d
file1:blurb
file2:Blurb
Delete
Substitute (replace)
Output line
Append after
d
a
i
c
s
File exists
Compare 2 files
-nt
-ot
-ef
link)
Newer than
Older than
Refers to same inode (hard
Compare 2 integers
-eq
-ne
-gt
-lt
-ge
-le
Equal
Not equal
Greater than
Less than
Greater than or equal
Less than or equal
Test strings
test -z string
test string
True if string has nonzero length
test string1 = string2 True if strings are equal
test string1 != string2 True if strings are not equal
Combine tests
test ! condition
- True if condition is not true
test condition1 -a condition2 - True if both are true
test condition1 -o condition2 - True if either is true
The tr Command
The tr Command