Linux Programming Unit 1
Linux Programming Unit 1
Linux Programming
History of Linux: Architecture & Components of Linux System:
• Linux is a Unix clone written from scratch by Linus
Torvalds with assistance from a loosely-knit team of
hackers across the Net.
• Unix is a multitasking, multi-user computer
operating system originally developed in 1969 by a
group of AT&T employees at Bell Labs.
• Linux and Unix strive to be POSIX compliant.
• POSIX an acronym for "Portable Operating System
Interface", is a family of standards specified by the
IEEE for maintaining compatibility between
operating systems.
Development started in 1991
◦ Linus Torvalds wanted to create a free
implementation of UNIX
◦ By 1993 there were 12000 Linux users
◦ Today Linux rivals UNIX in stability and
scalability
• Linux is a UNIX clone
– It can run on 32 bit and 64 bit hardware
– Linux is a true multitasking environment
– Fully capable of taking advantage of
multiple processors
– Can address up to 64 GB of RAM
– Partial POSIX Compliance
Features of Linux:
Linux is free
o Anyone can download and compile the The Linux Operating System’s architecture primarily has
these components: the Kernel, Hardware layer, System
source
library, Shell and System utility.
o The code can be modified by anyone
• The kernel is the core part of the operating system,
provided the modifications are released to
which is responsible for all the major activities of
the community
the LINUX operating system. This operating system
Linux is not an Operating System
consists of different modules and interacts directly
Linux is a kernel
with the underlying hardware. The kernel offers the
A kernel is a program that allocates and controls
required abstraction to hide application programs or
hardware resources in a system
low-level hardware details to the system. The types
Linux Distrobutions use the Linux kernel together
of Kernels are as follows:
with the GNU Operating System
• Monolithic Kernel
Portable(Multiplatform)
• Micro kernels
Multitasking
• Exo kernels
Multi User
• Hybrid kernels
Multiprocessor (SMP) Support
2. System libraries are special functions, that are used to
Multithreading Support
implement the functionality of the operating system and do
Virtual Memory
not require code access rights of kernel modules.
Hierarchical File System
3. System Utility programs are liable to do individual, and
Graphical User Interface (X Window System,
specialized-level tasks.
Wayland)
4. Hardware layer of the LINUX operating system consists
Wide Hardware Support
of peripheral devices such as RAM, HDD, CPU.
Dynamically Linked Shared Libraries as well as
5. The shell is an interface between the user and the kernel,
Static Libraries
and it affords services of the kernel. It takes commands from
POSIX Compliant (Almost)
the user and executes kernel’s functions. The Shell is present
Multiple Virtual Consoles
in different types of operating systems, which are classified
Multitple Filesystem Support
into two types:command line shells and graphical shells.
Multiple Networking
Shell
file time stamp (time and date created/modified) Abbreviations used by chmod:
Examples:
1. tr abc X5Z // translates a into X, b into 5
and c into Z.
It can have a range of characters using special
(limited)
2. tr can be used with [-] characters as follows
brightness_4
# To print elements in range
echo ${arr[@]:1:4}
#!/bin/sh echo ${arr[@]:2:3}
echo ${arr[0]:1:3}
NAME[0]="Zara" To count length of Array.
NAME[1]="Qadir"
NAME[2]="Mahnaz" # Size of an Array
NAME[3]="Ayan" echo ${#arr[@]}
NAME[4]="Daisy" echo ${#arr[*]}
echo "First Method: ${NAME[*]}" Output:
echo "Second Method: ${NAME[@]}" 6
OUTPUT: 6
$ksh main.ksh
First Method: Zara Qadir Mahnaz Ayan Daisy
Second Method: Zara Qadir Mahnaz Ayan Daisy trap funcname 1 2 15
Shell Script Interrupts
handling shell interrupts is something you should consider. funcname
As a user is interacting with your script, they may decide to # Function to handle interrupts
interrupt it by typing Ctrl-C, for example. Typically this will {
interrupt your shell script execution, forcing it to exit. echo "`basename $0`: Ouch! User Aborted." 1>&2
exit $exit_user_abort
Depending on what your shell script is doing, this could leave }
behind temporary files, or leave other files in a broken state.
It would be useful if you could trap the interrupt, and handle Shell programs:
it safely, before exiting the script. To find the area of a triangle
This can be achieved on most shells using the ‘trap’ $cat tri.sh
command. The trap command takes the following syntax: echo Area of the Triangle
trap [OPTIONS] [[ARG] SIGSPEC ... ] echo Enter the Base
The ARG is the command to be executed on signal delivery, read b
while SIGSPEC is the name of the signal(s) to trap. Options echo Enter the Height
include -h for help, -l to list signal names, or -p to print all read h
defined signal handlers. echo " scale=2; 0.5 * $b * $h " | bc
For example, to always return a ‘user aborted’ error code, the
following line in your script could be used. Whatever value OUTPUT:
given to $exit_user_abort would be returned.
trap 'echo "`basename $0`: Ouch! User Aborted." $sh tri.sh
1>&2; exit $exit_user_abort' 1 2 15 Area of the Triangle
The numbers 1, 2 and 15 at the end of this example define Enter the Base
which interrupts we’re interested in trapping. These numbers 10
correspond to different kinds of interrupts. A short list is Enter the Height
given here, but you can use ‘trap -l’ for a complete list. 3
15.0