Training Linux - 3
Training Linux - 3
Linux Fundamentals
DAY 3
3/21/2022 5
09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use
Linux Fundamentals
Type of Shells
• In Unix and Linux, the two major types of shell scripts are:
⮚ Bourne again shells (BASH)- BASH is the default shell for Unix version 7. The character for prompting a
bourne again shell is $.
⮚ C shells- A C shell is run in a text terminal window and is able to easily read file commands. The
character for prompting a C shell is %.
3/21/2022 6
09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use
Linux Fundamentals
• Open terminal.
• Navigate to the place where you want to create script using ‘cd‘ command.
• cd (enter) [This will bring the prompt at Your home Directory].
• touch my_first_script.sh (Here we named the script as hello, remember the ‘.sh‘
extension is compulsory).
• vi my_first_script.sh (nano hello.sh) [You can use your favourite editor, to edit the
script].
• chmod 744 my_first_script.sh (making the script executable).
• sh my_first_script.sh or ./my_first_script.sh (running the script)
3/21/2022 7
09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use
Linux Fundamentals
Getting started with Shell Programming
Variables in Shell
⮚ (1) System variables - Created and maintained by Linux itself. This type of variable defined in CAPITAL
LETTERS.
⮚ (2) User defined variables (UDV) - Created and maintained by user. This type of variable defined in
lower letters
3/21/2022 8
09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use
Linux Fundamentals
Getting started with Shell Programming
You can see system variables by giving command like $ set, some of the important System variables are:
3/21/2022 9
09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use
Linux Fundamentals
Getting started with Shell Programming
How to define User defined variable
Syntax:
variable name=value
'value' is assigned to given 'variable name' and Value must be on right side = sign
Example:
number_one=1 # This is ok
1=number_one # Error
3/21/2022 10
09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use
Linux Fundamentals
Getting started with Shell Programming
Rules for Naming variable name (Both UDV and System Variable)
(1) Variable name must begin with Alphanumeric character or underscore character (_), followed by one or more
Alphanumeric character. For e.g. Valid shell variable are as followsSyntax:
HOME
SYSTEM_VERSION
number_one
(2) Don't put spaces on either side of the equal sign when assigning value to variable. For e.g. In following variable
declaration there will be no error:
number_one=1
But there will be problem for any of the following variable declaration:
number_one =1
number_one= 1
number_one = 1
3/21/2022 11
09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use
Linux Fundamentals
Getting started with Shell Programming
(3) Variables are case-sensitive, just like filename in Linux
Number_one=1
number_one=1
(4) You can define NULL variable as follows (NULL variable is variable which has no value at the time of definition)
str=
str=“123"
Try to print it's value by issuing following command
echo $str
Nothing will be shown because variable has no value i.e. NULL variable.
3/21/2022 12
09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use
Linux Fundamentals
Getting started with Shell Programming
How to print or access value of UDV (User defined variables)
3/21/2022 13
09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use
Linux Fundamentals
Getting started with Shell Programming
echo Command
Use echo command to display text or value of variable.
Syntax:
echo [options] [string, variables...]
Displays text or variables value on screen.
Options:
-n Do not output the trailing new line.
-e Enable interpretation of the following backslash escaped characters in the strings:
\a alert (bell)
\b backspace
\c suppress trailing new line
\n new line
\r carriage return
\t horizontal tab
\\ backslash
E.g. $ echo -e "My name is \a\t\tHung\n"
3/21/2022 14
09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use
Linux Fundamentals
Getting started with Shell Programming
Shell Arithmetic
Use to perform arithmetic operations.
Syntax:
expr op1 math-operator op2
Examples:
expr 4 + 3
expr 4 - 3
expr 4 / 3
expr 4%3
echo `expr 4+3`.
3/21/2022 15
09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use
Linux Fundamentals
Getting started with Shell Programming
The read Statement
Use to get input (data from user) from keyboard and store (data) to variable. Syntax: read variable1, variable2,...variable
Example:
$ vi second_script.sh
echo "What is your last name?"
read last_name
echo "hello $last_name"
$ chmod 744 second_script
$ ./ second_script
3/21/2022 16
09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use
Linux Fundamentals
Getting started with Shell Programming
Wild cards (Filename Shorthand or meta Characters)
3/21/2022 17
09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use
Linux Fundamentals
Shells (bash) structured Language Constructs
For Mathematics, use following operator in Shell Script
3/21/2022 18
09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use
Linux Fundamentals
Shells (bash) structured Language Constructs
For string Comparisons us
3/21/2022 19
09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use
Linux Fundamentals
Shells (bash) structured Language Constructs
Shell also test for file and directory types
3/21/2022 20
09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use
Linux Fundamentals
Shells (bash) structured Language Constructs
Question?
?
Type String and Array in Shell
Script?
3/21/2022 21
09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use
Linux Fundamentals
Shells (bash) structured Language Constructs
If else condition
Syntax:
if condition
then
Statement(s) to be executed if condition is true
else
Statement(s) to be executed if condition is false
fi
3/21/2022 22
09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use
Linux Fundamentals
Shells (bash) structured Language Constructs
If-then-else condition
Syntax:
if condition1
then
Statement(s) to be executed if condition1 is true
elif condition2
then
Statement(s) to be executed if condition2 is true
else
None of the above condition1, condition 2 are true
fi
3/21/2022 23
09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use
Linux Fundamentals
Shells (bash) structured Language Constructs
The case Statement
Syntax:
case $variable in
pattern1)
command …
command;;
pattern2)
command …
command;;
patternN)
command …
command;;
esac
3/21/2022 24
09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use
Linux Fundamentals
Shells (bash) structured Language Constructs
while loop
Syntax:
While [ condition ]
do
Statement(s) to be executed if condition is true
done
3/21/2022 25
09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use
Linux Fundamentals
Shells (bash) structured Language Constructs
Function
To declare a function, simply use the following syntax
Syntax:
function_name()
{
list of commands
}
# invoke function
HelloWorld
3/21/2022 26
09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use
Linux Fundamentals
Shells (bash) structured Language Constructs
Pass Parameters to a Function
To declare a function, simply use the following syntax
Syntax:
function_name()
{
list of commands
}
# invoke function
HelloWorld Nguyen Hung
3/21/2022 27
09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use
Linux Fundamentals
Shells (bash) structured Language Constructs
Question?
?
How to de-bug the shell script?
3/21/2022 28
09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use
Linux Fundamentals
SELF-STUDY!!!
3/21/2022 29
09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use
Linux Fundamentals
If - Else
Find max in three number
Eg: You have three number 9,5,2. You find maximum number is 9.
Switch_case
Check number is Odd or Even
Eg: You have a input number. You check number is Odd or Even.
3/21/2022 30
09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use
Linux Fundamentals
While - loop
Print Star
Eg: You have a input number. You print tringle star.
3/21/2022 31
09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use
Linux Fundamentals
DAY 3
SUMMARY
3/21/2022 32
09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use
Linux Fundamentals
ASSIGNMENT
3/21/2022 33
09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use
Linux Fundamentals
REVIEW AND
RESEARCH
3/21/2022 34
09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use
Linux Fundamentals
REVIEW AND RESEARCH
Divide the class into 6 groups for research, presentation and discussion
3/21/2022 35
09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use
Linux Fundamentals
REVIEW AND RESEARCH
Target
1. Review
2. Definition, use case
3. How to install, deploy, command
4. Expanding the scope of research
5. Present
3/21/2022 36
09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use
Linux Fundamentals
REVIEW AND RESEARCH
How to implement
• Introduce Group
• Introduce module of Group
• Basic Summary
• Extend Summary
• Demo
• All of this
(About 10 - 15 minutes/group)
3/21/2022 37
09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use
Thank you