0% found this document useful (0 votes)
3 views30 pages

1-Shell Scripting

The document provides an overview of shell scripting, including system calls, the init process, and the purpose of shell scripting. It discusses key concepts such as process control, file management, and the structure of ELF files. Additionally, it covers the differences between static and dynamic linking, forking, and execve, along with categories of Linux commands.

Uploaded by

yzaki1969
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views30 pages

1-Shell Scripting

The document provides an overview of shell scripting, including system calls, the init process, and the purpose of shell scripting. It discusses key concepts such as process control, file management, and the structure of ELF files. Additionally, it covers the differences between static and dynamic linking, forking, and execve, along with categories of Linux commands.

Uploaded by

yzaki1969
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

SHELL SCRIPTING

Mostafa Morgan & Abdelrahman Badawy


AGENDA
1. System calls.

2. Init process.

3. Run levels.

4. Shell scripting history.

5. Main purpose of shell scripting.

6. What is elf file?

7. What is dynamic linker?

8. Forking.

9. Execve.

10. Linux commands Anatomy.

11. File viewing and Editing.


11/20/2024 SEITech Solutions - Public Information 2
SYSTEM CALLS

A system call is a way for programs to interact with the operating


system

Services Provided by System Calls :


1. Process control.
2. File management.
3. Device management.
4. Information maintenance.
5. Communication.

SEITech Solutions - Confidential Information 3


SYSTEM CALLS

Process Control:

fork() : Creates a new process by duplicating the current process.


exit() : Terminates the calling process.
wait() : Makes the calling process wait until one of its child processes terminates.
exec() :Replaces the current process with a new program.
getpid(): Retrieves the process ID of the calling process.
kill() : Sends a signal to a process (used to terminate or interrupt a process)

SEITech Solutions - Confidential Information 4


SYSTEM CALLS

File Management :

open() : Opens a file.


read() : Reads data from a file.
write() : Writes data to a file.
close() : Closes an open file descriptor.
lseek() : Moves the file pointer to a specific location within a file.
stat() : Retrieves information about a file (size, permissions, etc.).
unlink(): Deletes a file (removes its directory entry).

SEITech Solutions - Confidential Information 5


SYSTEM CALLS

Memory Management :

brk() and sbrk() : Used to control the size of the heap.


mmap() : Maps files or devices into memory.
munmap() : Unmaps files or memory regions.
malloc() and free(): Used for allocating and freeing memory in user-space applications.

SEITech Solutions - Confidential Information 6


SYSTEM CALLS

Inter-process communication (IPC):

pipe(): Creates a unidirectional pipe for communication between processes.


shmget() and shmat() : Used for shared memory management.
semget() and semop() : Used for semaphore (locking) operations.
msgget(), msgsnd(), and msgrcv(): Used for message queues.

SEITech Solutions - Confidential Information 7


SYSTEM CALLS

File management System:

mkdir(): Creates a new directory.


rmdir(): Removes a directory.
chdir(): Changes the current working directory.
chmod(): Changes file permissions.
chown(): Changes file ownership.
mount() and umount(): Mount and unmount file systems.

SEITech Solutions - Confidential Information 8


SYSTEM CALL CYCLE

SEITech Solutions - Confidential Information 9


INIT PROCESS

The init process in Linux is the first process the kernel starts after booting.
It plays a critical role in setting up the environment for the system to run
and ensuring all necessary services are started.

Key Aspects of the init Process:

Kernel Boot
init Configuration
Run Levels
Service Scripts
User Login

SEITech Solutions - Confidential Information 10


SYSTEMV INIT PROCESS

Kernel Boot:
When the system boots, the Linux kernel is loaded, and
control is passed to init.
init Configuration:
init reads its configuration from /etc/inittab. This file
contains instructions on what the system should do
based on the current run level. init then moves to the
default run level.

26.09.2022 SEITech Solutions - Confidential Information 11


SYSTEMV INIT PROCESS

Run Levels :
Run level Description
A run level is a state of init and the whole system
System halt ,the system can be safely powered off with no
defines what system services are operating. Run 0
activity.
levels are identified by numbers. Run level is a
mode or state in which a Linux system operates. It 1 Single user mode.
has a crucial role in the management of the Linux
2 Multiple user mode with no NFS (network file system).
System.
Multiple user modes under the command line interface and
3
not under the graphical user interface.

4 User-definable.

Multiple user mode under GUI and this is the standard run
5
level for most of the LINUX-based systems.

6 Reboot which is used to restart the system.

26.09.2022 SEITech Solutions - Confidential Information 12


SYSTEMV INIT PROCESS

Service Scripts:
For each run level, the system has
associated scripts located in directories
like /etc/init.d/. These scripts are executed
in sequence when entering a particular run
level. The scripts are used to start, stop, or
restart system services such as
networking, logging, and daemons.
User Login:
Once the services have been initialized, init
starts the login process

26.09.2022 SEITech Solutions - Confidential Information 13


SYSTEMD INIT PROCESS

Kernel Boot:
kernel loads systemd as the first user-space
process. it uses targets to define system states.
systemd Targets:
Targets in systemd are like run levels in that they
define a set of services that should be started.
However, systemd allows for more fine-grained
control over the dependencies between services.
Common systemd.

26.09.2022 SEITech Solutions - Confidential Information 14


SYSTEMD INIT PROCESS

Unit Files:
system-d uses unit files to manage services. These files define how a
service should start, stop, and be managed. Unit files are stored in
directories like /etc/systemd/system/.Each service has a corresponding
unit file .
Parallelization:
One of the key features of systemd is that it starts services in parallel (as
opposed to the sequential execution in SysVinit). It analyzes service
dependencies and starts services in the most efficient order, speeding
up the boot process.

26.09.2022 SEITech Solutions - Confidential Information 15


SYSTEMD INIT PROCESS

Service Management:
system-d allows administrators to start, stop, restart, or enable/disable
services using the systemctl command.
User Login:
After all necessary services have started, systemd proceeds to launch the
login service (such as Getty for terminal login or a graphical login
manager).

26.09.2022 SEITech Solutions - Confidential Information 16


S H E L L S C R I P T I NG H I S TO RY

A shell is a special user program that provides an interface for the user to
use operating system services. Shell accepts commands from users and
converts them into something which the kernel can understand. The shell
gets started when the user logs in or starts the terminal.

Shell is broadly classified into two categories :


• Command line shell.
• Graphical shell.
several shells:
• BASH (Borne Again Shell).
• CSH (C Shell).
• KSH (Korn Shell).

SEITech Solutions - Confidential Information 17


H O W S H E L L S TA R T E D F O R C L I S Y S T E M ?

Bash was created by Brian Fox in 1987 as part of the GNU Project to replace the Bourne Shell.

Features:
• Command-line editing
• Job control
• Command history
• Tab completion
• Scripting enhancements (functions, arrays, etc.)

SEITech Solutions - Confidential Information 18


MAIN PURPOSE OF SHELL SCRIPTING.

- A shell provides a command-line interface, allowing users to interact with the operating
system and execute commands.

- The shell serves as an interpreter for running programs and scripts. It can execute both
system-level commands and user-created scripts.

What is the meaning by commands ?


It is just program compiled and be as ELF file.

SEITech Solutions - Confidential Information 19


W H AT I S E L F F I L E ?

The Executable and Linkable Format (ELF) is the standard file format for
executable, object code, shared libraries, and core dumps on Linux and
Unix-like systems.

ELF file consists of:

• Elf Header
• Section header and different sections
• Program header and different program sections
• Object code
• Debugging information

SEITech Solutions - Confidential Information 20


W H AT I S E L F F I L E ?
Header file :

• Magic – Bytes 0-3 contain the magic number 0x7F,‘E‘,‘L‘,‘F‘


identifying the file as ELF.

• Class – Byte 4 specifies architecture word size as either


ELFCLASS32 (1) for 32-bit or ELFCLASS64 (2) for 64-bit.

• Encoding – Byte 5 specifies endianness as ELFDATA2LSB (1)


for little-endian or ELFDATA2MSB (2) for big-endian.

• ELF Version – Byte 6 covers the ELF header version. Currently


EV_CURRENT (1) is most common.

• OS ABI – Byte 7 identifies the target OS Application Binary


Interface (ABI). Common values are ELFOSABI_SYSV (0),
ELFOSABI_LINUX (3), or ELFOSABI_NONE (255).

SEITech Solutions - Confidential Information 21


W H AT I S E L F F I L E ?
Header file :

• ABI Version – Byte 8 specifies the ABI version, typically 0.

• Type – Halfword in bytes 16-17 that specifies the object file type
– ET_EXEC (2) for executables, ET_DYN (3) for shared objects,
ET_REL (1) for relocatable files, etc.

• Machine – Halfword in bytes 18-19 that specifies the required


CPU architecture. x86 is EM_386 (3), x86_64 is EM_X86_64
(62).

• Version – Word at bytes 20-23 specifies the ELF format version.


Typically EV_CURRENT (1).

• Entry Point – Word at bytes 24-27 stores the memory address for
the entry point – initial instruction executed when loading
executable.
SEITech Solutions - Confidential Information 22
W H AT I S D Y N A M I C L O A D E R ?

Dynamic Linking
Static Linking

The process of combining all necessary library routines The process of linking external libraries and references at
Definition and external references into a single executable file at runtime, when the program is loaded or executed.
compile-time.

Occurs at runtime.
Linking Time Occurs at compile-time.

Smaller file size, as libraries are linked dynamically at


Generally larger file size, as all required libraries are
File Size runtime.
included in the executable.

Less flexible, as any updates or changes to the libraries More flexible, as libraries can be updated or replaced without
Flexibility require recompilation and relinking of the entire recompiling the program.
program.

Slightly slower program startup due to the additional linking


Faster program startup and direct execution, as all
Performance process, but overall performance impact is minimal.
libraries are already linked.

Executables with file extensions like .exe, .elf, .a, .lib,


Examples Executables with file extensions like .dll, .so, .dylib, etc.
etc.

SEITech Solutions - Confidential Information 23


HOW CALLING HAPPEN
1. Dynamic Linker's Role: When you run a program that uses dynamically linked shared
libraries, the dynamic is responsible for loading these libraries into memory.

2. Memory Mapping: The dynamic linker maps the sections of shared libraries (text, data, etc.) into specific memory
regions within the process's address space. This mapping involves creating memory-mapped regions.

3. Memory Loading: The actual content of the shared libraries on disk is loaded into the memory-mapped regions. This
means that the code and data of the libraries are now resident in the system's physical memory.

4. Symbol Resolution: The dynamic linker resolves symbols (functions, variables) used by the process and updates
references in the process's code to point to the memory-mapped addresses of the shared library.

5. Runtime Execution: During the execution of the process, when functions or data from shared libraries are needed, the
process accesses the memory-mapped regions in memory, not from disk.

6. Memory Protection: The memory-mapped regions are protected by the operating system's memory protection
mechanisms, ensuring that unauthorized access or modification of the shared library's memory is prevented.

7. Sharing Among Processes: Memory-mapped regions for shared libraries can be shared among multiple processes that
use the same libraries. This reduces memory consumption since the same library can be loaded into memory once and
shared among multiple processes.

SEITech Solutions - Confidential Information 24


Forking

The process of creating a new process by duplicating an existing one.

Parent Process: This is the original process that called fork


Child Process: This is the new process created by the fork().

In the parent process, fork() returns the PID of the child.


In the child process, fork() returns 0.

After forking, both processes (parent and child) run independently of


each other. They can have their own execution paths, even though the
child starts as an exact copy of the parent

SEITech Solutions - Confidential Information 25


Execve

The execv() system call is part of the family of exec functions


used in Linux.

used to replace the current process image with a new program.


it loads and runs a different executable within the current
process, effectively replacing the current process with a new
one.

No Return After Success: Once execv() successfully replaces the


process, it does not return to the calling program. The new
program's execution starts immediately.

execv() is commonly used after fork(). When a process wants to


execute a different program.

SEITech Solutions - Confidential Information 26


I-node

An i-node (Index Node) is a data structure on a file


system that stores metadata about a file or directory
Linux.
It is an essential part of how files are managed on disk.
The i-node contains all the information needed to manage
a file except for its name and actual data content.

Each i-node is identified by a unique i-node number


within a file system This number is used internally by the
file system to access the i-node.

SEITech Solutions - Confidential Information 27


I-node Structure

File type: Specifies whether the file is a regular file, directory, symbolic link, or other types.
File permissions: The read, write, and execute permissions for the file.
Owner: The user ID (UID) of the file's owner.
Group: The group ID (GID) of the file’s group.
Size: The total size of the file (in bytes).
Creation time (ctime): Time when the inode was created or last changed.
Modification time (mtime): Time when the file's content was last modified.
Access time (atime): Time when the file was last accessed (read or executed).
Number of links: The number of hard links pointing to this inode.
Pointers to data blocks: Addresses to the actual data blocks on the disk where the content of the file
is stored.
Inode Number: Each inode is identified by a unique inode number within a filesystem. This inode
number is used internally by the filesystem to access the inode

SEITech Solutions - Confidential Information 28


Linux commands categories

1-File and Directory Management.


2-File Viewing and Editing.
3-Controls: Redirection,Piping,Wildcard.
4-File Permissions.
5-File Compression and Archiving.
6-Text Processing.
7-System Information.
8-System Monitoring and Logging.
9-search.
10-Process Management.
11-Networking.
12-User Management.
13-Package Management.
14-System Maintenance.
15-Links.
16- FS.
SEITech Solutions - Confidential Information 29
THANK YOU
QUESTIONS!

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy