際際滷

際際滷Share a Scribd company logo
Linux Shell Programming
Linux Shell Programming
UNIT 2- Syllabus
teachics.org
Introduction
 Shell is an environment in which we can run our commands, programs, and
shell scripts.
 It gathers input from you and executes programs based on that input. When
a program finishes executing, it displays that program's output.
 - kernal = core of a computers operating system
 - terminal = text input/output environment
 - shell = command line interpreter
 - shell scripts = commands written in a file
teachics.org
Shells Available in Unix
- C shell (csh)
- TC shell (tcsh)
- Bourne shell (sh)
- Korn shell (ksh)
- Bourne Again Shell (bash)
Bourne Again Shell (bash)
Bash is the default shell in most Linux distributions.Bash offers functional
improvements over sh for both programming and interactive use.
teachics.org
Special Characters
Comments-Lines beginning with a # (with the exception of #!) will not be executed.
# This line is a comment.
; Command separator. Permits putting two or more commands on the same line.
echo hello ; echo world
;; Terminator in a case option
case "$variable" in
abc) echo "$variable = abc" ;;
xyz) echo "$variable = xyz" ;;
esac
` command substitution. The `command` construct makes available the output of
command for assignment to a variable
teachics.org
Special Characters
$ Variable substitution (contents of a variable)
$(variable)
  Weak quotes
echo hello world
  Strong quotes
echo hello world 
 Single character quote
teachics.org
Getting help
The help command provides information on built-in commands.
simply entering help at the terminal prompt will show a complete list of the built-in
commands available.
enter help followed by the command you wish to learn more about.
help pwd
With the -d option, the help command will only return a short description of the specified
command.
help -d pwd
With the -s option, help will return a short usage synopsis for the specified command.
help -s pwd
The -m option formats the help command output as a pseudo-manpage.
help -m pwd
teachics.org
man pages
The man command shows detailed manuals for each command. These are referred to as man
pages.
It provides a detailed view of the command which includes NAME, SYNOPSIS, DESCRIPTION,
OPTIONS, EXIT STATUS, RETURN VALUES, ERRORS, FILES, VERSIONS, EXAMPLES, AUTHORS ..etc
man pwd
teachics.org
Linux file system structure
teachics.org
Linux home page structure
teachics.org
Commands for navigating the Linux file system
 pwd - prints the complete path of the current working directory.
pwd -L : Prints the symbolic path.
pwd -P : Prints the actual path.
 cd - change the current working directory.
cd [directory]
 ls - list of the names of all files in the current working directory / specified directory.
ls
ls /etc
 file - displays the type of a file.
file [option] [filename]
file -b filename : used to display just file type in brief mode.
file directoryname/* : used to display all files filetypes in particular directory.
teachics.org
Commands for navigating the Linux file system
 cat  read and concatenate data from the file and gives their content as output.
cat filename read single filename
cat file1 file2 show contents of file1 and file2
cat -n file show contents of fille with preceding line number
cat -s file suppress repeated empty lines in output
 cp - copy fiiles or directories from source to destination
cp Src_file Dest_file copy the contents of src_file to the dest_file
cp Src_file1 Src_file2 Src_file3 Dest_directory copies each source file to the destination directory
cp -R Src_directory Dest_directory copies all files of the source directory to the destination directory, creating any files or directories
needed
 mv - move fiiles or directories from source to destination
mv Src_file Dest_file move the file src_file to the dest_file
mv -i Src_file Dest_directory ask the user for confirmation before moving a file that would overwrite an existing file,
mv -f Src_directory Dest_directory overwrite the destination file forcefully and delete the source file.
teachics.org
Commands for navigating the Linux file system
 mkdir - create multiple directories at once as well as set the permissions for the directories
mkdir foldername
mkdir -m 777 foldername used to set the file modes
 rmdir - remove empty directories from the filesystem
rmdir -p directory remove directory if it is empty
rmdir -p directory remove all child and parent empty directories
rmdir directorylist remove all directories
 whereis - locates source/binary and manuals sections for specified files.
whereis perl List the directories where the perl source files, documentation, and binaries are stored.
whereis -b perl List only perl binaries are stored.
whereis -s perl List only perl sources are stored.
whereis -m perl List only perl manuals are stored.
teachics.org
Piping and Redirection
Every program we run on the command line automatically has three data streams connected
to it.
STDIN (0) - Standard input (data fed into the program)
STDOUT (1) - Standard output (data printed by the program, defaults to the terminal)
STDERR (2) - Standard error (for error messages, also defaults to the terminal)
Piping and redirection is the means by which we may connect these streams between
programs and files to direct data in interesting and useful ways.
teachics.org
Piping
Pipes allow you to funnel the output from one command into another where it will be used as
the input.
Pipes are unidirectional
The | operator feeds the output from the program on the left to the program on the right.
This will sort the given file and print the unique values only.
sort record.txt | uniq
teachics.org
Redirection
By default, stdout and stderr are printed to your terminal.
But we can redirect that output to a file using the > operator:
echo hello > new-file
echo didnt print anything to the terminal because we redirected its output to a file named new-file.
it replaces new-files contents with the new contents.
If you want to append to the file, you can use the >> operator
echo hello again >> new-file
teachics.org
Informal Commands
 ps - to see which processes you're currently running on your Linux system.
Using the -f option for ps you can gain additional useful information on each process in the
listing.
 w - command-line utility that displays information about currently logged in users and what
each user is doing. It also gives information about how long the system has been running, the
current time, and the system load average.
 id - used to find out user and group names and numeric IDs (UID or group ID) of the
current user or any other user in the server.
 free - displays the total amount of free space available along with the amount of memory
used and swap memory in the system, and also the buffers used by the kernel.
 clear - used to remove all previous commands and output fromterminal windows
teachics.org
Informal Commands
 echo - display a line of text/string on standard output or a file.
echo [option(s)] [string(s)]
eg:-
echo Hello World
echo The value of variable x = $x
echo -e "Hello nWorld"
 more - used to view the text files in the command prompt, displaying one screen at a time in case the file is large. The
more command also allows the user do scroll up and down through the page.
more [-options] [-num] [+/pattern] [+linenum] [file_name]
[-options]: any option that you want to use in order to change the way the file is displayed. (-d, -l, -f, -p, -c, -s, -u)
[-num]: type the number of lines that you want to display per screen.
[+/pattern]: replace the pattern with any string that you want to find in the text file.
[+linenum]: use the line number from where you want to start displaying the text content.
[file_name]: name of the file containing the text that you want to display on the screen.
teachics.org
File Permissions
Ownership of Linux files
Every file and directory on your Unix/Linux system is assigned 3 types of owner,
User : owner of the file. By default, the person who created a file becomes its owner.
Group : group can contain multiple users. All users belonging to a group will have the same access
permissions to the file.
Other : This person has neither created the file, nor he belongs to a usergroup who could own the
file. Practically, it means everybody else.
Permissions
Every file and directory in your UNIX/Linux system has following 3 permissions.
Read : authority to open and read a file.
Write: authority to modify the contents of a file.
Execute : In Unix/Linux, you cannot run a program unless the execute permission is set.
teachics.org
File Permissions
using chmod command, we can set permissions (read, write, execute) on a file/directory for
the owner, group and the world.
chmod permission file
There are 2 ways to use the command
 Absolute mode
 Symbolic mode
teachics.org
File Permissions
Absolute Mode
file permissions are not represented as characters but a three-digit octal number.
chmod 764 filename
764' absolute code says the following:
- Owner can read, write and execute
- Usergroup can read and write
- World can only read
This is shown as
chmod -rwxrw-r- filename
Number Permission Type Symbol
0 No permission ---
1 Execute --x
2 Write -w-
3 Write + Execute -wx
4 Read r--
5 Read+Execute r-x
6 Read+Write rw-
7 Read+Write+Execute rwx
teachics.org
File Permissions
Symbolic Mode
n the symbolic mode, you can modify permissions of a specific owner.
It makes use of mathematical symbols to modify the file permissions..
chmod o=rwx filename
setting others read+write+execute permissions
chmod g+x filename
adding execute permission to group
chmod u-r filename
removing read permission of user
User Description
u user
g group
o other
Operator Description
+ No permission
- Execute
= Write
teachics.org
Comments
Lines beginning with a # (with the exception of #!) are comments and will not be
executed.
# This line is a comment
Comments may also occur following the end of a command.
echo a comment will follow # this is a comment
Comments may also follow whitespace at the beginning of a line.
# This is also a comment
a quoted or an escaped # in an echo statement does not begin a comment.
echo hello #This is not a comment
teachics.org
Variables
A variable is a name assigned to a location or set of locations in computer memory
holding an item of data.
If variable1 is the name of a variable, then $variable1 is a reference to its value.
The only times a variable appears without the $ prefix is when declared or assigned.
variable1=23
echo $variable1
Essentially, Bash variables are character strings, but, depending on context, Bash
permits arithmetic operations and comparisons on variables. The determining factor
is whether the value of a variable contains only digits.
teachics.org
Operators
Assignment Operator
= is the all-purpose assignment operator, which works for both arithmetic and string
assignments.
var=27
category=minerals # No spaces allowed after the "="
Logical Operators
! NOT
&& AND
|| OR
teachics.org
Operators
Arithmetic Operators
+ plus
- minus
* multiplication
/ division
** exponentiation
% mod (returns the remainder of an integer division operation)
+= plus-equal (increment variable by a constant) [38]
-= minus-equal (decrement variable by a constant)
*= times-equal (multiply variable by a constant)
/= slash-equal (divide variable by a constant)
%= mod-equal (remainder of dividing variable by a constant)
teachics.org
Operators
Relational Operators
-eq equal to
-nt not equal to
-gt greater than
-lt lessthan
-ge greater than or equal to
-le less than or equal to
< is less than (within double parentheses) (("$a" < "$b"))
<= is less than or equal to (within double parentheses) (("$a" <= "$b"))
> is greater than (within double parentheses) (("$a" > "$b"))
>= is greater than or equal to (within double parentheses) (("$a" >= "$b"))
teachics.org
Quoting
When referencing a variable, it is generally advisable to enclose its name in
double quotes. This prevents reinterpretation of all special characters within
the quoted string except $, ` (backquote), and  (escape).
Use double quotes to prevent word splitting.An argument enclosed in double
quotes presents itself as a single word, even if it contains whitespace
separators.
Within single quotes, every special character except ' gets interpreted.
Consider single quotes ("full quoting") to be a stricter method of quoting than
double quotes ("partial quoting").
The escape () preceding a character tells the shell to interpret that character
literally.
teachics.org
read
Reads the value of a variable from stdin, that is, interactively fetches input from the
keyboard.
echo "Enter the value of variable 'var1': "
read var1
echo
prints (to stdout) an expression or variable
echo Hello
echo $a
An echo requires the -e option to print escaped characters.
Normally, each echo command prints a terminal newline, but the -n option suppresses
this.
teachics.org
Conditional Commands
if can test any command.Reads the value of a variable from stdin, that is, interactively fetches input from the keyboard.
if [ condition ]
then
command1
else
command2
fi
elif is a contraction for else if. The effect is to nest an inner if/then construct within an outer one.
if [ condition1 ]
then
command1
elif [ condition2 ]
then
command4
else
default-command
fi
teachics.org
Conditional Commands
The case construct is the shell scripting analog to switch in C/C++. It permits
branching to one of a number of code blocks, depending on condition tests.
case "$variable" in
"$condition1" )
command...
;;
"$condition2" )
command...
;;
teachics.org
iterative commands
A loop is a block of code that iterates [52] a list of commands as long as the loop
control condition is true.
for arg in [list]
do
command(s)...
done
This construct tests for a condition at the top of a loop, and keeps looping as long as
that condition is true (returns a 0 exit status)..
while [ condition ]
do
command(s)...
done
teachics.org
break and continue
break command terminates the loop (breaks out of it)
for innerloop in 1 2 3 4 5
do
if [ "$innerloop" -eq 3 ]
then
break # Try break 2 to see what happens.
fi
done
continue causes a jump to the next iteration of the loop, skipping all the remaining
commands in that particular loop cycle.
for inner in 1 2 3 4 5 6 7 8 9 10 # inner loop
do
if [[ "$inner" -eq 7 && "$outer" = "III" ]]
then
continue 2 # Continue at loop on 2nd level, that is "outer loop".
fi
done
teachics.org
expr
All-purpose expression evaluator: Concatenates and evaluates the arguments
according to the operation given (arguments must be separated by spaces).
a=`expr 5 + 3`
-
teachics.org
bc
Bash can't handle floating point calculations, and it lacks operators for certain
important mathematical functions.
echo "scale=2; 2/3" | bc
.66
teachics.org
grep
searches a file for a particular pattern of characters and displays all lines that contain
that pattern.
case insensitive search
grep -i Word file.txt
display count of matches
grep -c Word file.txt
checking whole word only
grep -w Word file.txt
teachics.org
arrays
Bash arrays can hold multiple values at the same time.
arrayname[index]=value

More Related Content

Similar to OS-Module 2 Linux Programming Important topics (20)

Unix_QT.ppsx
Unix_QT.ppsxUnix_QT.ppsx
Unix_QT.ppsx
vamsikrishna204239
Linux file commands and shell scripts
Linux file commands and shell scriptsLinux file commands and shell scripts
Linux file commands and shell scripts
PrashantTechment
58518522 study-aix
58518522 study-aix58518522 study-aix
58518522 study-aix
homeworkping3
Linux ppt
Linux pptLinux ppt
Linux ppt
Sanmuga Nathan
Shell_Scripting.ppt
Shell_Scripting.pptShell_Scripting.ppt
Shell_Scripting.ppt
KiranMantri
Linux System commands Essentialsand Basics.pptx
Linux System commands Essentialsand Basics.pptxLinux System commands Essentialsand Basics.pptx
Linux System commands Essentialsand Basics.pptx
mba1130feb2024
lec1.docx
lec1.docxlec1.docx
lec1.docx
ismailaboshatra
Linux presentation
Linux presentationLinux presentation
Linux presentation
Nikhil Jain
50 Most Frequently Used UNIX Linux Commands -hmftj
50 Most Frequently Used UNIX  Linux Commands -hmftj50 Most Frequently Used UNIX  Linux Commands -hmftj
50 Most Frequently Used UNIX Linux Commands -hmftj
LGS, GBHS&IC, University Of South-Asia, TARA-Technologies
Linuxppt
LinuxpptLinuxppt
Linuxppt
poornima sugumaran
Linux
LinuxLinux
Linux
nazeer pasha
Linux Basics.pptx
Linux Basics.pptxLinux Basics.pptx
Linux Basics.pptx
RanjitKumarPanda5
Introduction to Unix
Introduction to UnixIntroduction to Unix
Introduction to Unix
Sudharsan S
Karkha unix shell scritping
Karkha unix shell scritpingKarkha unix shell scritping
Karkha unix shell scritping
chockit88
Linux administration training
Linux administration trainingLinux administration training
Linux administration training
iman darabi
Unix_Introduction_BCA.pptx the very basi
Unix_Introduction_BCA.pptx the very basiUnix_Introduction_BCA.pptx the very basi
Unix_Introduction_BCA.pptx the very basi
Priyadarshini648418
Linuxppt
LinuxpptLinuxppt
Linuxppt
poornima sugumaran
Linux Commands
Linux CommandsLinux Commands
Linux Commands
Ramasubbu .P
Linux
LinuxLinux
Linux
Rathan Raj
Linuxppt
LinuxpptLinuxppt
Linuxppt
Reka

Recently uploaded (20)

Pass SAP C_C4H47_2503 in 2025 | Latest Exam Questions & Study Material
Pass SAP C_C4H47_2503 in 2025 | Latest Exam Questions & Study MaterialPass SAP C_C4H47_2503 in 2025 | Latest Exam Questions & Study Material
Pass SAP C_C4H47_2503 in 2025 | Latest Exam Questions & Study Material
Jenny408767
Unit1 Inroduction to Internal Combustion Engines
Unit1  Inroduction to Internal Combustion EnginesUnit1  Inroduction to Internal Combustion Engines
Unit1 Inroduction to Internal Combustion Engines
NileshKumbhar21
Different Facets of Knowledge on different View.pptx
Different Facets of Knowledge on different View.pptxDifferent Facets of Knowledge on different View.pptx
Different Facets of Knowledge on different View.pptx
NrapendraVirSingh
Enhancing SoTL through Generative AI -- Opportunities and Ethical Considerati...
Enhancing SoTL through Generative AI -- Opportunities and Ethical Considerati...Enhancing SoTL through Generative AI -- Opportunities and Ethical Considerati...
Enhancing SoTL through Generative AI -- Opportunities and Ethical Considerati...
Sue Beckingham
Conrad "Accessibility Essentials: Introductory Seminar"
Conrad "Accessibility Essentials: Introductory Seminar"Conrad "Accessibility Essentials: Introductory Seminar"
Conrad "Accessibility Essentials: Introductory Seminar"
National Information Standards Organization (NISO)
General Quiz at ChakraView 2025 | Amlan Sarkar | Ashoka Univeristy | Prelims ...
General Quiz at ChakraView 2025 | Amlan Sarkar | Ashoka Univeristy | Prelims ...General Quiz at ChakraView 2025 | Amlan Sarkar | Ashoka Univeristy | Prelims ...
General Quiz at ChakraView 2025 | Amlan Sarkar | Ashoka Univeristy | Prelims ...
Amlan Sarkar
U.S. Department of Education certification
U.S. Department of Education certificationU.S. Department of Education certification
U.S. Department of Education certification
Mebane Rash
Recruitment in the Odoo 17 - Odoo 17 際際滷s
Recruitment in the Odoo 17 - Odoo 17 際際滷sRecruitment in the Odoo 17 - Odoo 17 際際滷s
Recruitment in the Odoo 17 - Odoo 17 際際滷s
Celine George
MIPLM subject matter expert Dr Robert Klinski
MIPLM subject matter expert Dr Robert KlinskiMIPLM subject matter expert Dr Robert Klinski
MIPLM subject matter expert Dr Robert Klinski
MIPLM
O SWEET SPONTANEOUS BY EDWARD ESTLIN CUMMINGSAN.pptx
O SWEET SPONTANEOUS BY EDWARD ESTLIN CUMMINGSAN.pptxO SWEET SPONTANEOUS BY EDWARD ESTLIN CUMMINGSAN.pptx
O SWEET SPONTANEOUS BY EDWARD ESTLIN CUMMINGSAN.pptx
AituzazKoree
How to Setup Company Data in Odoo 17 Accounting App
How to Setup Company Data in Odoo 17 Accounting AppHow to Setup Company Data in Odoo 17 Accounting App
How to Setup Company Data in Odoo 17 Accounting App
Celine George
MIPLM subject matter expert Daniel Holzner
MIPLM subject matter expert Daniel HolznerMIPLM subject matter expert Daniel Holzner
MIPLM subject matter expert Daniel Holzner
MIPLM
All India Council of Skills and Vocational Studies (AICSVS) PROSPECTUS 2025
All India Council of Skills and Vocational Studies (AICSVS) PROSPECTUS 2025All India Council of Skills and Vocational Studies (AICSVS) PROSPECTUS 2025
All India Council of Skills and Vocational Studies (AICSVS) PROSPECTUS 2025
National Council of Open Schooling Research and Training
10.socialorganisationandsocialsystem .pptx
10.socialorganisationandsocialsystem .pptx10.socialorganisationandsocialsystem .pptx
10.socialorganisationandsocialsystem .pptx
Vivek Bhattji
How to Install Odoo 18 with Pycharm - Odoo 18 際際滷s
How to Install Odoo 18 with Pycharm - Odoo 18 際際滷sHow to Install Odoo 18 with Pycharm - Odoo 18 際際滷s
How to Install Odoo 18 with Pycharm - Odoo 18 際際滷s
Celine George
Anti-Viral Agents.pptx Medicinal Chemistry III, B Pharm SEM VI
Anti-Viral Agents.pptx Medicinal Chemistry III, B Pharm SEM VIAnti-Viral Agents.pptx Medicinal Chemistry III, B Pharm SEM VI
Anti-Viral Agents.pptx Medicinal Chemistry III, B Pharm SEM VI
Samruddhi Khonde
MIPLM subject matter expert Dr Alihan Kaya
MIPLM subject matter expert Dr Alihan KayaMIPLM subject matter expert Dr Alihan Kaya
MIPLM subject matter expert Dr Alihan Kaya
MIPLM
Anti-Fungal Agents.pptx Medicinal Chemistry III B. Pharm Sem VI
Anti-Fungal Agents.pptx Medicinal Chemistry III B. Pharm Sem VIAnti-Fungal Agents.pptx Medicinal Chemistry III B. Pharm Sem VI
Anti-Fungal Agents.pptx Medicinal Chemistry III B. Pharm Sem VI
Samruddhi Khonde
BIOPHARMACEUTICS AND PHARMACOKINETICS(BP604T) - Copy (3).pptx
BIOPHARMACEUTICS AND PHARMACOKINETICS(BP604T) - Copy (3).pptxBIOPHARMACEUTICS AND PHARMACOKINETICS(BP604T) - Copy (3).pptx
BIOPHARMACEUTICS AND PHARMACOKINETICS(BP604T) - Copy (3).pptx
maniramkumar
Marketing is Everything in the Beauty Business! 憓 Talent gets you in the ...
 Marketing is Everything in the Beauty Business! 憓 Talent gets you in the ... Marketing is Everything in the Beauty Business! 憓 Talent gets you in the ...
Marketing is Everything in the Beauty Business! 憓 Talent gets you in the ...
coreylewis960
Pass SAP C_C4H47_2503 in 2025 | Latest Exam Questions & Study Material
Pass SAP C_C4H47_2503 in 2025 | Latest Exam Questions & Study MaterialPass SAP C_C4H47_2503 in 2025 | Latest Exam Questions & Study Material
Pass SAP C_C4H47_2503 in 2025 | Latest Exam Questions & Study Material
Jenny408767
Unit1 Inroduction to Internal Combustion Engines
Unit1  Inroduction to Internal Combustion EnginesUnit1  Inroduction to Internal Combustion Engines
Unit1 Inroduction to Internal Combustion Engines
NileshKumbhar21
Different Facets of Knowledge on different View.pptx
Different Facets of Knowledge on different View.pptxDifferent Facets of Knowledge on different View.pptx
Different Facets of Knowledge on different View.pptx
NrapendraVirSingh
Enhancing SoTL through Generative AI -- Opportunities and Ethical Considerati...
Enhancing SoTL through Generative AI -- Opportunities and Ethical Considerati...Enhancing SoTL through Generative AI -- Opportunities and Ethical Considerati...
Enhancing SoTL through Generative AI -- Opportunities and Ethical Considerati...
Sue Beckingham
General Quiz at ChakraView 2025 | Amlan Sarkar | Ashoka Univeristy | Prelims ...
General Quiz at ChakraView 2025 | Amlan Sarkar | Ashoka Univeristy | Prelims ...General Quiz at ChakraView 2025 | Amlan Sarkar | Ashoka Univeristy | Prelims ...
General Quiz at ChakraView 2025 | Amlan Sarkar | Ashoka Univeristy | Prelims ...
Amlan Sarkar
U.S. Department of Education certification
U.S. Department of Education certificationU.S. Department of Education certification
U.S. Department of Education certification
Mebane Rash
Recruitment in the Odoo 17 - Odoo 17 際際滷s
Recruitment in the Odoo 17 - Odoo 17 際際滷sRecruitment in the Odoo 17 - Odoo 17 際際滷s
Recruitment in the Odoo 17 - Odoo 17 際際滷s
Celine George
MIPLM subject matter expert Dr Robert Klinski
MIPLM subject matter expert Dr Robert KlinskiMIPLM subject matter expert Dr Robert Klinski
MIPLM subject matter expert Dr Robert Klinski
MIPLM
O SWEET SPONTANEOUS BY EDWARD ESTLIN CUMMINGSAN.pptx
O SWEET SPONTANEOUS BY EDWARD ESTLIN CUMMINGSAN.pptxO SWEET SPONTANEOUS BY EDWARD ESTLIN CUMMINGSAN.pptx
O SWEET SPONTANEOUS BY EDWARD ESTLIN CUMMINGSAN.pptx
AituzazKoree
How to Setup Company Data in Odoo 17 Accounting App
How to Setup Company Data in Odoo 17 Accounting AppHow to Setup Company Data in Odoo 17 Accounting App
How to Setup Company Data in Odoo 17 Accounting App
Celine George
MIPLM subject matter expert Daniel Holzner
MIPLM subject matter expert Daniel HolznerMIPLM subject matter expert Daniel Holzner
MIPLM subject matter expert Daniel Holzner
MIPLM
10.socialorganisationandsocialsystem .pptx
10.socialorganisationandsocialsystem .pptx10.socialorganisationandsocialsystem .pptx
10.socialorganisationandsocialsystem .pptx
Vivek Bhattji
How to Install Odoo 18 with Pycharm - Odoo 18 際際滷s
How to Install Odoo 18 with Pycharm - Odoo 18 際際滷sHow to Install Odoo 18 with Pycharm - Odoo 18 際際滷s
How to Install Odoo 18 with Pycharm - Odoo 18 際際滷s
Celine George
Anti-Viral Agents.pptx Medicinal Chemistry III, B Pharm SEM VI
Anti-Viral Agents.pptx Medicinal Chemistry III, B Pharm SEM VIAnti-Viral Agents.pptx Medicinal Chemistry III, B Pharm SEM VI
Anti-Viral Agents.pptx Medicinal Chemistry III, B Pharm SEM VI
Samruddhi Khonde
MIPLM subject matter expert Dr Alihan Kaya
MIPLM subject matter expert Dr Alihan KayaMIPLM subject matter expert Dr Alihan Kaya
MIPLM subject matter expert Dr Alihan Kaya
MIPLM
Anti-Fungal Agents.pptx Medicinal Chemistry III B. Pharm Sem VI
Anti-Fungal Agents.pptx Medicinal Chemistry III B. Pharm Sem VIAnti-Fungal Agents.pptx Medicinal Chemistry III B. Pharm Sem VI
Anti-Fungal Agents.pptx Medicinal Chemistry III B. Pharm Sem VI
Samruddhi Khonde
BIOPHARMACEUTICS AND PHARMACOKINETICS(BP604T) - Copy (3).pptx
BIOPHARMACEUTICS AND PHARMACOKINETICS(BP604T) - Copy (3).pptxBIOPHARMACEUTICS AND PHARMACOKINETICS(BP604T) - Copy (3).pptx
BIOPHARMACEUTICS AND PHARMACOKINETICS(BP604T) - Copy (3).pptx
maniramkumar
Marketing is Everything in the Beauty Business! 憓 Talent gets you in the ...
 Marketing is Everything in the Beauty Business! 憓 Talent gets you in the ... Marketing is Everything in the Beauty Business! 憓 Talent gets you in the ...
Marketing is Everything in the Beauty Business! 憓 Talent gets you in the ...
coreylewis960

OS-Module 2 Linux Programming Important topics

  • 3. teachics.org Introduction Shell is an environment in which we can run our commands, programs, and shell scripts. It gathers input from you and executes programs based on that input. When a program finishes executing, it displays that program's output. - kernal = core of a computers operating system - terminal = text input/output environment - shell = command line interpreter - shell scripts = commands written in a file
  • 4. teachics.org Shells Available in Unix - C shell (csh) - TC shell (tcsh) - Bourne shell (sh) - Korn shell (ksh) - Bourne Again Shell (bash) Bourne Again Shell (bash) Bash is the default shell in most Linux distributions.Bash offers functional improvements over sh for both programming and interactive use.
  • 5. teachics.org Special Characters Comments-Lines beginning with a # (with the exception of #!) will not be executed. # This line is a comment. ; Command separator. Permits putting two or more commands on the same line. echo hello ; echo world ;; Terminator in a case option case "$variable" in abc) echo "$variable = abc" ;; xyz) echo "$variable = xyz" ;; esac ` command substitution. The `command` construct makes available the output of command for assignment to a variable
  • 6. teachics.org Special Characters $ Variable substitution (contents of a variable) $(variable) Weak quotes echo hello world Strong quotes echo hello world Single character quote
  • 7. teachics.org Getting help The help command provides information on built-in commands. simply entering help at the terminal prompt will show a complete list of the built-in commands available. enter help followed by the command you wish to learn more about. help pwd With the -d option, the help command will only return a short description of the specified command. help -d pwd With the -s option, help will return a short usage synopsis for the specified command. help -s pwd The -m option formats the help command output as a pseudo-manpage. help -m pwd
  • 8. teachics.org man pages The man command shows detailed manuals for each command. These are referred to as man pages. It provides a detailed view of the command which includes NAME, SYNOPSIS, DESCRIPTION, OPTIONS, EXIT STATUS, RETURN VALUES, ERRORS, FILES, VERSIONS, EXAMPLES, AUTHORS ..etc man pwd
  • 11. teachics.org Commands for navigating the Linux file system pwd - prints the complete path of the current working directory. pwd -L : Prints the symbolic path. pwd -P : Prints the actual path. cd - change the current working directory. cd [directory] ls - list of the names of all files in the current working directory / specified directory. ls ls /etc file - displays the type of a file. file [option] [filename] file -b filename : used to display just file type in brief mode. file directoryname/* : used to display all files filetypes in particular directory.
  • 12. teachics.org Commands for navigating the Linux file system cat read and concatenate data from the file and gives their content as output. cat filename read single filename cat file1 file2 show contents of file1 and file2 cat -n file show contents of fille with preceding line number cat -s file suppress repeated empty lines in output cp - copy fiiles or directories from source to destination cp Src_file Dest_file copy the contents of src_file to the dest_file cp Src_file1 Src_file2 Src_file3 Dest_directory copies each source file to the destination directory cp -R Src_directory Dest_directory copies all files of the source directory to the destination directory, creating any files or directories needed mv - move fiiles or directories from source to destination mv Src_file Dest_file move the file src_file to the dest_file mv -i Src_file Dest_directory ask the user for confirmation before moving a file that would overwrite an existing file, mv -f Src_directory Dest_directory overwrite the destination file forcefully and delete the source file.
  • 13. teachics.org Commands for navigating the Linux file system mkdir - create multiple directories at once as well as set the permissions for the directories mkdir foldername mkdir -m 777 foldername used to set the file modes rmdir - remove empty directories from the filesystem rmdir -p directory remove directory if it is empty rmdir -p directory remove all child and parent empty directories rmdir directorylist remove all directories whereis - locates source/binary and manuals sections for specified files. whereis perl List the directories where the perl source files, documentation, and binaries are stored. whereis -b perl List only perl binaries are stored. whereis -s perl List only perl sources are stored. whereis -m perl List only perl manuals are stored.
  • 14. teachics.org Piping and Redirection Every program we run on the command line automatically has three data streams connected to it. STDIN (0) - Standard input (data fed into the program) STDOUT (1) - Standard output (data printed by the program, defaults to the terminal) STDERR (2) - Standard error (for error messages, also defaults to the terminal) Piping and redirection is the means by which we may connect these streams between programs and files to direct data in interesting and useful ways.
  • 15. teachics.org Piping Pipes allow you to funnel the output from one command into another where it will be used as the input. Pipes are unidirectional The | operator feeds the output from the program on the left to the program on the right. This will sort the given file and print the unique values only. sort record.txt | uniq
  • 16. teachics.org Redirection By default, stdout and stderr are printed to your terminal. But we can redirect that output to a file using the > operator: echo hello > new-file echo didnt print anything to the terminal because we redirected its output to a file named new-file. it replaces new-files contents with the new contents. If you want to append to the file, you can use the >> operator echo hello again >> new-file
  • 17. teachics.org Informal Commands ps - to see which processes you're currently running on your Linux system. Using the -f option for ps you can gain additional useful information on each process in the listing. w - command-line utility that displays information about currently logged in users and what each user is doing. It also gives information about how long the system has been running, the current time, and the system load average. id - used to find out user and group names and numeric IDs (UID or group ID) of the current user or any other user in the server. free - displays the total amount of free space available along with the amount of memory used and swap memory in the system, and also the buffers used by the kernel. clear - used to remove all previous commands and output fromterminal windows
  • 18. teachics.org Informal Commands echo - display a line of text/string on standard output or a file. echo [option(s)] [string(s)] eg:- echo Hello World echo The value of variable x = $x echo -e "Hello nWorld" more - used to view the text files in the command prompt, displaying one screen at a time in case the file is large. The more command also allows the user do scroll up and down through the page. more [-options] [-num] [+/pattern] [+linenum] [file_name] [-options]: any option that you want to use in order to change the way the file is displayed. (-d, -l, -f, -p, -c, -s, -u) [-num]: type the number of lines that you want to display per screen. [+/pattern]: replace the pattern with any string that you want to find in the text file. [+linenum]: use the line number from where you want to start displaying the text content. [file_name]: name of the file containing the text that you want to display on the screen.
  • 19. teachics.org File Permissions Ownership of Linux files Every file and directory on your Unix/Linux system is assigned 3 types of owner, User : owner of the file. By default, the person who created a file becomes its owner. Group : group can contain multiple users. All users belonging to a group will have the same access permissions to the file. Other : This person has neither created the file, nor he belongs to a usergroup who could own the file. Practically, it means everybody else. Permissions Every file and directory in your UNIX/Linux system has following 3 permissions. Read : authority to open and read a file. Write: authority to modify the contents of a file. Execute : In Unix/Linux, you cannot run a program unless the execute permission is set.
  • 20. teachics.org File Permissions using chmod command, we can set permissions (read, write, execute) on a file/directory for the owner, group and the world. chmod permission file There are 2 ways to use the command Absolute mode Symbolic mode
  • 21. teachics.org File Permissions Absolute Mode file permissions are not represented as characters but a three-digit octal number. chmod 764 filename 764' absolute code says the following: - Owner can read, write and execute - Usergroup can read and write - World can only read This is shown as chmod -rwxrw-r- filename Number Permission Type Symbol 0 No permission --- 1 Execute --x 2 Write -w- 3 Write + Execute -wx 4 Read r-- 5 Read+Execute r-x 6 Read+Write rw- 7 Read+Write+Execute rwx
  • 22. teachics.org File Permissions Symbolic Mode n the symbolic mode, you can modify permissions of a specific owner. It makes use of mathematical symbols to modify the file permissions.. chmod o=rwx filename setting others read+write+execute permissions chmod g+x filename adding execute permission to group chmod u-r filename removing read permission of user User Description u user g group o other Operator Description + No permission - Execute = Write
  • 23. teachics.org Comments Lines beginning with a # (with the exception of #!) are comments and will not be executed. # This line is a comment Comments may also occur following the end of a command. echo a comment will follow # this is a comment Comments may also follow whitespace at the beginning of a line. # This is also a comment a quoted or an escaped # in an echo statement does not begin a comment. echo hello #This is not a comment
  • 24. teachics.org Variables A variable is a name assigned to a location or set of locations in computer memory holding an item of data. If variable1 is the name of a variable, then $variable1 is a reference to its value. The only times a variable appears without the $ prefix is when declared or assigned. variable1=23 echo $variable1 Essentially, Bash variables are character strings, but, depending on context, Bash permits arithmetic operations and comparisons on variables. The determining factor is whether the value of a variable contains only digits.
  • 25. teachics.org Operators Assignment Operator = is the all-purpose assignment operator, which works for both arithmetic and string assignments. var=27 category=minerals # No spaces allowed after the "=" Logical Operators ! NOT && AND || OR
  • 26. teachics.org Operators Arithmetic Operators + plus - minus * multiplication / division ** exponentiation % mod (returns the remainder of an integer division operation) += plus-equal (increment variable by a constant) [38] -= minus-equal (decrement variable by a constant) *= times-equal (multiply variable by a constant) /= slash-equal (divide variable by a constant) %= mod-equal (remainder of dividing variable by a constant)
  • 27. teachics.org Operators Relational Operators -eq equal to -nt not equal to -gt greater than -lt lessthan -ge greater than or equal to -le less than or equal to < is less than (within double parentheses) (("$a" < "$b")) <= is less than or equal to (within double parentheses) (("$a" <= "$b")) > is greater than (within double parentheses) (("$a" > "$b")) >= is greater than or equal to (within double parentheses) (("$a" >= "$b"))
  • 28. teachics.org Quoting When referencing a variable, it is generally advisable to enclose its name in double quotes. This prevents reinterpretation of all special characters within the quoted string except $, ` (backquote), and (escape). Use double quotes to prevent word splitting.An argument enclosed in double quotes presents itself as a single word, even if it contains whitespace separators. Within single quotes, every special character except ' gets interpreted. Consider single quotes ("full quoting") to be a stricter method of quoting than double quotes ("partial quoting"). The escape () preceding a character tells the shell to interpret that character literally.
  • 29. teachics.org read Reads the value of a variable from stdin, that is, interactively fetches input from the keyboard. echo "Enter the value of variable 'var1': " read var1 echo prints (to stdout) an expression or variable echo Hello echo $a An echo requires the -e option to print escaped characters. Normally, each echo command prints a terminal newline, but the -n option suppresses this.
  • 30. teachics.org Conditional Commands if can test any command.Reads the value of a variable from stdin, that is, interactively fetches input from the keyboard. if [ condition ] then command1 else command2 fi elif is a contraction for else if. The effect is to nest an inner if/then construct within an outer one. if [ condition1 ] then command1 elif [ condition2 ] then command4 else default-command fi
  • 31. teachics.org Conditional Commands The case construct is the shell scripting analog to switch in C/C++. It permits branching to one of a number of code blocks, depending on condition tests. case "$variable" in "$condition1" ) command... ;; "$condition2" ) command... ;;
  • 32. teachics.org iterative commands A loop is a block of code that iterates [52] a list of commands as long as the loop control condition is true. for arg in [list] do command(s)... done This construct tests for a condition at the top of a loop, and keeps looping as long as that condition is true (returns a 0 exit status).. while [ condition ] do command(s)... done
  • 33. teachics.org break and continue break command terminates the loop (breaks out of it) for innerloop in 1 2 3 4 5 do if [ "$innerloop" -eq 3 ] then break # Try break 2 to see what happens. fi done continue causes a jump to the next iteration of the loop, skipping all the remaining commands in that particular loop cycle. for inner in 1 2 3 4 5 6 7 8 9 10 # inner loop do if [[ "$inner" -eq 7 && "$outer" = "III" ]] then continue 2 # Continue at loop on 2nd level, that is "outer loop". fi done
  • 34. teachics.org expr All-purpose expression evaluator: Concatenates and evaluates the arguments according to the operation given (arguments must be separated by spaces). a=`expr 5 + 3` -
  • 35. teachics.org bc Bash can't handle floating point calculations, and it lacks operators for certain important mathematical functions. echo "scale=2; 2/3" | bc .66
  • 36. teachics.org grep searches a file for a particular pattern of characters and displays all lines that contain that pattern. case insensitive search grep -i Word file.txt display count of matches grep -c Word file.txt checking whole word only grep -w Word file.txt
  • 37. teachics.org arrays Bash arrays can hold multiple values at the same time. arrayname[index]=value