Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
1
Outline
Introduction
Linux Kernel
Linux Shell
Shell scripting
Compiled vs. interpreted
Variables
How to write and execute scripts
Bash programming
Shell arithmetic
Command-line arguments
Echo
If sentences
For loops
2
Introduction: Linux Kernel
3
Introduction: Linux Kernel [1]
4
Introduction: Linux Shell
Context
Computers understand '0' and '1' (binary language)
Early days: instructions provided in machine code (assembly language):
8B 03 34 12 <--> MOV CX, 1234H
Shell
Command-language interpreter
Accept commands in human language (English) and translate them to binary
language
Read commands from input devices (keyboard) or files
5
Introduction: Linux Shell
6
Introduction: Shell examples
*tmux
https://gist.github.com/MohamedAlaa/2961058
7
Introduction: Change shell per user
8
Introduction: Linux Shell CLI
9
Activity 1: First steps with shell
10
Introduction: Linux common
commands
ls cp
cat mv
who rm
date chmod
pwd whoami
echo wc
more grep
tail sort
11
Introduction: Process
12
Introduction: Why processes?
13
Introduction: Linux commands related
to Process
ps: see current running process
Option ‘T’ to see process status
kill {PID}: stop process with process-id 'PID’
Kill process in background: kill {job-id}
&: used to run process in background
‘jobs’ command: list processes in background
Resume process in foreground: fg {job-id}
Resume process in background: bg {job-id}
Suspend/stop process: Ctrl + Z (send to background)
Terminate process: Ctrl + C
14
Introduction: Redirection
15
Activity 2: Use redirection
16
Activity 2: Solutions
17
Introduction: Pipe
18
Activity 3: use pipe
19
Activity 3: use pipe
a) who | wc –l
b) cd ~
ls -lh | grep "Do"
20
Shell Scripting
21
Shell Scripting: Interpreted vs.
Compiled language
Interpreted Compiled
Instructions are not translated to Instructions translated to machine
machine language, but to language (executed directly by
intermediate language processed hardware)
by interpreter (program) Advantage
Advantage Faster than interpreted: machine
Portability code
24
Shell Scripting: Define UDV
Syntax: variable_name=value
Example: temp_var=10
Rules:
Must begin with alphanumeric or underscore character
Do not put spaces (either side of equal sign) when assigning value to variable
Variables are case-sensitive
NULL variable: has no value at time of definition
25
Shell Scripting: Access value of UDV
'echo' command
-n: do not output the trailing new line
-e: enable interpretation of backslashes escapes
Shell arithmetic: use 'expr' command
Space required between operator and numbers
Assign result to variable: z=`expr x + y`
'read' statement: get input from keyboard and store it in variable
26
Shell Scripting: Additional options
Quotes:
Double quotes: remove meaning of characters
Single quotes: enclosed remains unchanged
Back quotes: execute command
Comments start with '#'
27
Shell Scripting: Write and execute
scripts
Use text editor (emacs, vi/vim, nano, cat)
See line numbers: nano –c, vim (:set nu)
Set execute permissions to user (creator is not allowed by default)
Execute using ./{script_name} (dot indicates execute command in current
shell) or specifying shell to use
System variable PATH: script as command
28
Bash Scripting: Shell Arithmetic
Addition: +
Subtraction: -
Multiplication: \*
Division: /
Remain: %
29
Bash Scripting: Arguments
30
Activity 5: write and execute first
script
Create a script that clears the terminal screen and prints 'Welcome to v305!'.
Use comments to describe what script does
Create a script that receives two numbers as arguments, sum them and print
the sum
Create a command from this script
31
Activity 5: Solutions
clear
echo "Welcome to v305!“
addition.sh
echo –n “1st number: ”
read num1
echo –n “2nd number: ”
read num2
echo `expr $num1 + $num2`
Verify that ‘$HOME/bin’ is in PATH system variable
cd ~
mkdir bin
mv ../addition.sh /bin
32
Bash Scripting: Exit status
33
Bash Scripting: Comparison expression
34
Bash Scripting: Comparison expression
String comparison
string1 = string2: equal
string1 != string2: not equal
String evaluation
String1: not null or defined
-n string1: not null and does exist (variable MUST be quoted)
-z string1: null and does exist
35
Bash Scripting: examples
Mathematical comparison:
String comparison:
[ -n “$var” ]
36
Bash Scripting: Comparison expression
Mathematical expression
-eq: equal to
-ne: not equal to
-lt: less than
-le: less than or equal
-gt: greater than
-ge: greater than or equal
Example: are arguments equal?
[ “$1” –ne “$2” ]
37
Bash Scripting: ‘test’ command
38
Bash Scripting: ‘test’ command
examples
Arithmetic: number comparison
test “$1” -lt “$2”
39
Bash Scripting: Logical operators
40
Bash Scripting: Logical operators
example
Order of inputs
Greater argument
41
Bash Scripting: Decision making
if [ "$num" -le 0 ]
then
echo "Number is not greater than 0“
elif [ "$num" -lt 10 ]
then
echo "1 digit number“
elif [ "$num" -lt 100 ]
then
echo "2 digit number“
else
echo "Number with 3 or more digits“
fi
43
Bash Scripting: Loops
44
Bash Scripting: For loop
'for' syntax
for {variable_name} in {list}
do
execute one of each item in list until end
done
“for” statement can use following syntax
for (( expr1; expr2; expr3 ))
45
Activity 6:
46
References
47