2. https://github.com/syaifulahdan/os-practice|Operating System Practice | 2 to 66
A. Objectives
1. Learn the basic elements of a shell script.
2. Create an interactive shell program.
3. Using parameters in the program.
4. Studying test conditions as well as logic operators associated
with test instructions
5. Know the built-in variables of the shell.
6. Create an app with a shell using if-then-else construction.
7. Using case - esac structure.
8. Loop with while, for, do while.
9. Create a function and know how to call the function.
4. https://github.com/syaifulahdan/os-practice|Operating System Practice | 4 to 66
The shell script is created with a text editor (ASCII editor)
and is generally assigned an ".sh" extension.
The script always starts with a comment, which starts with
the # sign, connected with! and the name of the shell
used.
#!/bin/sh (1)
# Program shell (2)
#
var1=x (2)
var2=8
1. SHELL SCRIPT
5. https://github.com/syaifulahdan/os-practice|Operating System Practice | 5 to 66
Beginning of the shell program, this initial
comment will be read by the system, then the
system activates the shell progra (/bin /sh)
listed on it. Shell programs can be selected, for
example / bin / csh, / bin / ksh and others
(1)
Is a comment, as a documentation, this line
will be ignored by the shell program
(2)
The use of variables (assignment), there
should be no space between variable
names and constants
(3)
6. https://github.com/syaifulahdan/os-practice|Operating System Practice | 6 to 66
2. VARIABLE
Variable shell is a variable that can have value in the form of String
value. The variable writing procedure is as follows:
name_var = value_var
Variables must start with alphabet, followed by alphanumeric and
other characters.
Variables can be written in lowercase or uppercase or a mixture of
both.
Shell distinguish uppercase and lowercase (case sensitive), for
example:
VPT=ftik
i=5
7. https://github.com/syaifulahdan/os-practice|Operating System Practice | 7 to 66
Assignment of variable values should not be separated by
spaces, since the shell will regard the separation as a
parameter, for example:
VPT =ftik ##error
VPT =ftik ##error
To see the value / contents of a variable, use the $ sign in front
of the variable name. In the shell, the echo instruction can
display the contents of that variable, for example:
VPT=ftik
echo $VPT
salary=320000
echo $salary
echo $VPT $salary
9. https://github.com/syaifulahdan/os-practice|Operating System Practice | 9 to 66
3. READ KEYBOARD
The value of the variable can be filled through the
keyboard (stdin) with read instructions.
4. PARAMETER
A shell program can have as many as 9 parameters and
is represented by a special variable of $! $ 2, $ 3, $ 4, $
5, $ 6, $ 7, $ 8 and $ 9. The program name she ll (script
name) is represented by the $ 0 variable.
The number of parameters expressed as $ #. When it
does not provide a parameter, then the value of $ # is 0.
10. https://github.com/syaifulahdan/os-practice|Operating System Practice | 10 to 66
The $ * shell variable specifies all strings that become
parameters / arguments of a script ($ @ has the same
meaning). $$ specifies the process id number (pid) of the
executed script. This pid will keep changing (generally)
ascending, each time the process runs.
11. https://github.com/syaifulahdan/os-practice|Operating System Practice | 11 to 66
5. STATUS EXIT
Each program after it is executed will provide information
through the special $? variable. The indications given are:
When the program ends successfully, $? = 0
When the program ends with an error, $? ≠ 0
The value of the exit status can be seen through the echo $?
instruction ?
12. https://github.com/syaifulahdan/os-practice|Operating System Practice | 12 to 66
6. CONSTRUCTION: IF
if-start instructions
then
Instructions 1
Instructions 2
………………
f
if will execute the initial instruction, and the exit status of the
instruction will be the condition. If 0, then the next instruction
goes into the block then. If not 0, then the program flow is
forwarded after the fi word key.
13. https://github.com/syaifulahdan/os-practice|Operating System Practice | 13 to 66
7. CONSTRUCTION: IF, THEN, ELSE
If instructions1
then
Instructions1. 1
Instructions1. 2
………………
Else
Instructions2. 1
Instructions2. 2
………………
fi
If the exit status is not equal to 0, then the condition becomes FALSE and
the instruction after else will be executed....
14. https://github.com/syaifulahdan/os-practice|Operating System Practice | 14 to 66
8. INSTRUCTION TEST
The test instruction is used to check the condition of an
expression. The expression consists of a factor and operator
separated by a space.
The test result will give the value of an exit status, ie 0 if the
expression is appropriate, otherwise the result is ≠ 0.
Operator for test
Test for files and directory
16. https://github.com/syaifulahdan/os-practice|Operating System Practice | 16 to 66
Test for files and directory
Operator 0 or TRUE, if
-f filename File exists, ordinary file
-d filename File exists, file is a directory
-r filename The file can be read
-w filename File can be written
-x filename File is executable
-s filename The file exists and is not empty
Test can be done to check whether file exists, readable,
writable, blank and others.
17. https://github.com/syaifulahdan/os-practice|Operating System Practice | 17 to 66
To facilitate readability, the test can be written with
[ ekspresi ]
[actually is another name of the test, the difference [will look
for brackets closing] at the end of the expression that must be
separated by a space.
18. https://github.com/syaifulahdan/os-practice|Operating System Practice | 18 to 66
9. LOGICAL && AND || (SHELL LEVEL)
Notation && and || used to combine shell instruction as an
alternative to if then else.
Notation && and || often found in the shell script system
administrator to run the routine of the operating system.
instruction 1 && instruction 2
the shell will execute instruction1, and if the exit status
of instruction1 is FALSE, then the result of the AND is
definitely the same as FALSE, so instruction2 has no
effect anymore. Therefore, instruction2 are not
executed. Conversely, if the result of instruction1 is
TRUE (0), then instruction2 is executed
19. https://github.com/syaifulahdan/os-practice|Operating System Practice | 19 to 66
instruction 1 || instruction 2
the shell will execute instruction1, if the status exit is
TRUE (0), the result of the OR operation is certain to
generate TRUE, regardless of the execution result of
instruction2.
Therefore the instruction2 do not need to be run. If the
result of instruction1 is FALSE, then instruction2
will be executed.
20. https://github.com/syaifulahdan/os-practice|Operating System Practice | 20 to 66
10. OPERATOR INTEGERS FOR TESTS
To compare two numbers, the test requires a different operator
than the string.
Operator 0 or TRUE, if
i1 –eq i2 number equal
i1 –ge i2 Greater or equal to
i1 –gt i2 Greater than
i1 –le i2 Smaller or equal to
i1 –lt i2 Smaller
i1 –ne i2 Numbers are not equal
23. https://github.com/syaifulahdan/os-practice|Operating System Practice | 23 to 66
12. CONSTRUCTION: IF THEN ELSE IF
If instructions1
then
Instructions1. 1
Instructions1. 2
………………
Elif Instructions2
then
Instructions2. 1
Instructions2. 2
………………
Else
Instructions3. 1
Instructions3. 2
………………
Fi
25. https://github.com/syaifulahdan/os-practice|Operating System Practice | 25 to 66
13. ARITHMETICAL CALCULATIONS
The type of SHELL variable is only one that is STRING.
There is no other type like Numeric, Floating, Boolean or
other. Consequently this variable can not make arithmetic
calculations, for example:
A=5
B=$A +1 ## error
UNIX provides a utility called expr that is a utility that
performs simple arithmetic.
27. https://github.com/syaifulahdan/os-practice|Operating System Practice | 27 to 66
15. CONSTRUCTION CASE
Case is used to simplify the use of chain if, so with the case,
conditions can be grouped logically with more clear and easy to
write.
case variable in
match1)
instructions1.1
instructions1.2
..................
;;
match2)
instructions2.1
instructions2.2
..................
;;
*)
instructions3.1
instructions3.2
..................
;;
esac
28. https://github.com/syaifulahdan/os-practice|Operating System Practice | 28 to 66
16. CONSTRUCTION FOR
For is used for repetition using a var which in each loop is
replaced by the value on the list.
for var in str1 str2 .....strn
do
instructions1
instructions1
..................
done
29. https://github.com/syaifulahdan/os-practice|Operating System Practice | 29 to 66
17. WHILE CONSTRUCTION
While used for instruction repetition, which is generally limited by
a condition.
As long as the condition is TRUE, then the loop continues.
The loop will stop, when the FALSE condition, or the program
exits the while block through exit or break.
while condition
do
instructions1
instructions2
..................
done
30. https://github.com/syaifulahdan/os-practice|Operating System Practice | 30 to 66
18. DUMMY INSTRUCTIONS
Dummy instruction is an instruction that does not
do anything, but this instruction gives exit status 0
(TRUE). Therefore, dummy instructions can be
used as forever conditions on loops (eg while).
The dummy instruction symbol is :⇒
31. https://github.com/syaifulahdan/os-practice|Operating System Practice | 31 to 66
19. FUNCTION
Function is a program that can be called by another
program by using the NameFunction () notation. The
function gives exit status ($?) Which is declared with
return nr, or value 0 as default.
Creating a function begins with a function name, a
parameter, then a block programs specified in {...}.
example:
F1( ) {
........
........
return 1
}
32. https://github.com/syaifulahdan/os-practice|Operating System Practice | 32 to 66
Variables can be defined in functions as local or global
variables. and it is very important to note, the name of
the variable used in a function, not to clash with the
same variable name outside the function, so that no
variable content changes.
34. https://github.com/syaifulahdan/os-practice|Operating System Practice | 34 to 66
1 Login as user.
2 Open the Console Terminal and do the experiments
below and then analyze the results of the
experiment.
3 Conduct the experiments below and then analyze
the experimental results.
4 Complete the practice questions.
37. https://github.com/syaifulahdan/os-practice|Operating System Practice | 37 to 66
$ . prog01.sh
3. Te run a shell, can alse create executable file and executed
relatve frem current directery
2. Te run a shell, use the dot netaten in frent ef the pregram
name
$ chmod +x prog01.sh
$ ./prog01.sh
38. https://github.com/syaifulahdan/os-practice|Operating System Practice | 38 to 66
Experiment 2 : Variables
1. Examples use variables in an interactve shell
2. Separaten ef 2 wierds wiith spaces indicates the executen ef 2
pieces ef instructen. The $ character must exist at the beginning ef
the variable name te viewi the centents ef the variable if net, then
eche wiill take the parameter as string.
$ VPT2=ftik informatic (There is an error message)
$ VPT2=”ftik informatic”
$ echo VPT2
$ echo $VPT2
$ VPT=ftik
$ echo=VPT
40. https://github.com/syaifulahdan/os-practice|Operating System Practice | 40 to 66
$ echo $V3
$ echo $V3UTI
$ echo ${V3}UTI
4. Cembine the centents ef a variable wiith anether string.
77 If cembined wiith an undefined variable name (empty)
then the eche instructen returns an empty string. Te aveid
cenfusien, the name ef the variable needs te be pretected
by {} and then the centents ef that variable are cembined
wiith the string.
43. https://github.com/syaifulahdan/os-practice|Operating System Practice | 43 to 66
$ .prog01.sh
$ prog01.sh (There is an error message)
$ ./prog01.sh (There is an error message)
$ chmod +x prog01.sh
$ ./prog01.sh
7. A simple wiay te execute a shell is te use the det netaten
in frent ef the shell script name. If the actual directery is
net listed in PATH, then the cemmand can net be feund.
When the script is net executable, the script can net be
executed.
45. https://github.com/syaifulahdan/os-practice|Operating System Practice | 45 to 66
2. Read the Name and Address frem the keybeard
$ vi prog02.sh
#!/bin/sh
# prog02.sh
# membaca nama dan alamat
echo “Nama Anda : “
read nama
echo “Alamat : “
read alamat
echo “Kota : “
read kota
echo
echo “Hasil adalah : $nama, $alamat di $kota”
47. https://github.com/syaifulahdan/os-practice|Operating System Practice | 47 to 66
4. The eche instructen autematcally assigns a newi line, se te aveid
it previded the -n epten, wihich states te eche te remeve the newi
line. Medify pregram prog02.sh
$ vi prog02.sh
#!/bin/sh
# prog02.sh
# Read Name and Address)
echo -n “Your Name : “
read name
echo -n “Address : “
read address
echo -n “City : “
read city
echo
echo “Hasil adalah : $name, $address in $city”
49. https://github.com/syaifulahdan/os-practice|Operating System Practice | 49 to 66
6. An empty variable is a variable that has ne value. This variable is
ebtained en assignment er read frem keybeard er variable that has
net been defined
$ read nama
<CR>
$ echo $nama
$ A=
$ B=””
$ C=$A$B
$ echo $C
50. https://github.com/syaifulahdan/os-practice|Operating System Practice | 50 to 66
7. Variables can be substtuted wiith the executen results ef an
instructen.
In the example belewi, the pwd instructen is executed first wiith a
pair ef Back Quates (inverted quetes). The result ef the executen
wiill be entered as the value ef the DIR variable
$ pwd
$ DIR=`pwd`
$ echo $DIR
51. https://github.com/syaifulahdan/os-practice|Operating System Practice | 51 to 66
8. Create a prog03.sh shell script
$ vi prog03.sh
#!/bin/sh
# prog03.sh
#
NAMA=`whoami`
echo Active Username is $NAMA
date =`date|cut -c1-10`
echo Today is the date $date
9. Executen prog03.sh
$ . prog03.sh
52. https://github.com/syaifulahdan/os-practice|Operating System Practice | 52 to 66
Experiment 4 : Parameter
1. Create shell script prog04.sh
$ nano prog04.sh
#!/bin/sh
# prog04.sh version 1
# Parameter passing
#
echo “name of the program is $0”
echo “Parameter 1 is $1”
echo “Parameter 2 is $2”
echo “Parameter 3 is $3”
54. https://github.com/syaifulahdan/os-practice|Operating System Practice | 54 to 66
3. Create shell script prog04.sh versien 2 by giving the number ef
parameters
$ nano prog04.sh
#!/bin/sh
# prog04.sh version 2
# Parameter passing
#
echo “Number of parameters $#”
echo “name of the program is $0”
echo “Parameter 1 is $1”
echo “Parameter 2 is $2”
echo “Parameter 3 is $3”
56. https://github.com/syaifulahdan/os-practice|Operating System Practice | 56 to 66
5. Create shell script preg04.sh versien 3 by adding tetal parameters
and precess id number (PID)
$ nano prog04.sh
#!/bin/sh
# prog04.sh version 3
# Parameter passing
#
echo “Number of parameters $#”
echo “name of the program is $0”
echo “Parameter 1 is $1”
echo “Parameter 2 is $2”
echo “Parameter 3 is $3”
echo “total parameters are $*”
echo “PID of this shell process is $$”
4. Executen prog04.sh wiith 4 parameters
$ . prog04.sh budi wati amir ani
59. https://github.com/syaifulahdan/os-practice|Operating System Practice | 59 to 66
2. If it cempares the status exit wiith 0, if it is the same, then the
pregram bleck gees inte the then-fi bleck
$ if [ $? = 0 ]
> then
> echo “The user is active”
> fi
3. The numbers (1) and (2) abeve can be simplified wiith
$ if who|grep <user> >/dev/null
> then
> echo okay
> fi
60. https://github.com/syaifulahdan/os-practice|Operating System Practice | 60 to 66
Experiment 7 : Censtructen if then else
1. Create shell script preg05.sh
#!/bin/sh
# prog05.sh
# The program will confirm whether the name
# user is active or not
#
echo –n “Give the user a name : ”
read name
if who | grep $name > /dev/null
then
echo “$name is active”
else
echo “$user name is not active”
fi
61. https://github.com/syaifulahdan/os-practice|Operating System Practice | 61 to 66
2. Run prog05.sh, enter the name ef the actve user that appears in
the who's instructen and alse try te name the inactve user
$ who
$ . prog05.sh [name : incative user]
$ . prog05.sh [name : active user]
63. https://github.com/syaifulahdan/os-practice|Operating System Practice | 63 to 66
Exercise : Practce 6A
1 Create a Program and name it excercise1.sh
#!/bin/sh
# prog02.sh
# Excercise 1
echo -n “NPM : “
read npm
echo -n “Nama : “
read nama
echo -n “Jurusan : “
read jurusan
echo -n “Alamat : “
read alamat
echo
echo “Mahasiswa Jurusan : $jurusan, bernama $nama
dan beralamat di $alamat” adalah mahasiswa
Berprestasi
64. https://github.com/syaifulahdan/os-practice|Operating System Practice | 64 to 66
Exercise : Practce 6A
2 Create a Program and name it excercise2.sh
#!/bin/sh
# prog04.sh version 3
# Parameter passing
#
echo "Mentioned 6 Color, Max 6"
echo “Number of parameters $#”
echo “name of the program is $0”
echo “color 1 is $1”
echo “color 2 is $2”
echo “color 3 is $3”
echo “color 4 is $4”
echo “color 5 is $5”
echo “color 6 is $6”
echo “total parameters are $*”
echo “PID of this shell process is $$”
66. https://github.com/syaifulahdan/os-practice|Operating System Practice | 66 to 66
“Pleasure in a job makes perfection on the results achieved”.
Aristoteles
“Believe you can. You're halfway”. Theodore Roosevelt
“You might be able to delay, but time will not wait”. Benjamin
Franklin
“The efort will work if someone does not give up”. Napoleon
Hill
“Opportunity to fnd a better strength in us arises when life
seems to be very challenging”. Joseph Campbell