Linux and Shell programming ( B.tech 3rd semester)

 

Unix

·        Unix is an Operating System that is truly the base of all Operating Systems like Ubuntu, Solaris, POSIX, etc.

·        Unix operating system is a set of program that play a role as a connection between the computer and the user.

·        It was originally meant for programmers developing software rather than non-programmers.

·        The shell is a command line interpreter.

·        Unix was considered to be the heart of the operating System. The system

linux Architecture:-

The main concept that combines all the versions of linuix is the following four area:

  • ·        Keranl
  • ·        Shell
  • ·        Command and Utilities
  • ·        Files and Directories

 



Structure of Unix OS are as follows

  1. 1UNIX is a family of multitasking, multiuser computer operating systems.
  2. 2    It was originally developed for mini computers. 
  3. 3.     UNIX has a reputation for stability, security, and scalability.

key features of UNIX include:

1.      Multiuser support: UNIX allows multiple users to simultaneously access the same system and share resources.

2.     Multitasking: UNIX is capable of running multiple processes at the same time.

3.     Shell scripting: UNIX provides a powerful scripting language that allows users to automate tasks.

4.     Security: UNIX has a robust security model that includes file permissions, user accounts, and network security features.

5.     Portability: UNIX can run on a wide variety of hardware platforms, from small embedded systems to large mainframe computers.

6.     Communication: UNIX supports communication methods using the write command, mail command, etc.

7.     Process Tracking: UNIX maintains a record of the jobs that the user creates. 

 

Linux Command

Sudo:-

 Linux sudo command stands for Super User Do. Generally, it is applied as a prefix of a few commands that superuser is allowed to execute.

Syntax

$ sudo OPTION... COMMAND   

Option in sudo command:-

1)    -V:     It stands for version. This option enables the sudo command for printing the number of the version and exit.

2)    -l:  It stands for list. This option will help to printout the various commands permitted the user over the current host.

3)     -h or -help:                The h stands for help in this option. 

4)    -v:          It stands for validate.

5)    -k:          It stands for kill. This option invalidates the timestamp of the user to sudo.

6)    -b:          It stands for background

7)     -K:        This option is the same as the -k option. However, it stands for sure kill

8)    -p:  It stands for prompt. This option permits us for overriding the password prompt (default) and apply a custom one. A few percent escapes are permitted which are as follows:

·        %u is developed to the login name of the invoking user.

·        %U is developed to the user's login name

·        %h is developed to the hostname (local) without any name of the domain.

·        %H is developed to the hostname (local) with the name of the domain (only when the hostname of the machine is completely qualified)

·        %% (two consecutive characters) can be broken into an individual % character.

 Man Command

The "man" is a short term for manual page. The man command in Linux is used to display the manual pages for other commands and utilities. It provides detailed documentation about the usage, options, and functionality of commands, making it an essential tool for both beginners and experienced users.

Syntax:-

    $ man [option] [command]

For example: To view the manual for the “ls” command execute the following command:

        $ man ls

Navigating Manual Pages

  • Spacebar: Move forward one page.
  • Enter: Move forward one line.
  • B: Move backward one page.
  • Q: Quit the manual viewer.

Sections in Manual Pages

  • NAME: Provides the name and a brief description of the command.
  • SYNOPSIS: Describes the syntax of the command.
  • DESCRIPTION: Offers a detailed explanation of the command’s functionality.
  • OPTIONS: Lists the available command-line options and their descriptions.
  • EXAMPLES: Provides practical examples demonstrating command usage.
  • SEE ALSO: Suggests related commands or resources.

 echo 

The echo command in Linux is a built-in command that allows users to display lines of text or strings that are passed as arguments. It is commonly used in shell scripts and batch files to output status text to the screen or a file.

Syntax:

$ echo [string]

$ echo "Geeks for Geeks"

Output:- Geeks for Geeks

Options Available in `echo` command in Linux

1. \b :           it removes all the spaces in between the text

          Example:-    echo -e "Geeks \bfor \bGeeks"

                   Output:-  GeeksforGeeks

2. \c :           suppress trailing new line with backspace interpreter ‘-e‘ to continue without emitting new line. text after \c is not printed and omitted trailing new line.

Example:-     echo -e "Geeks \cfor Geeks"

                   Output:-  Geeks

3. \n :          this option creates a new line from where it is used.

Example:-    echo -e "Geeks \nfor \nGeeks"

          Output:-       Geeks

                             For

                             Geeks

4. \t :           this option is used to create horizontal tab spaces.

          Example:  echo -e "Geeks \tfor \tGeeks"

          Output:-       Geeks for Geeks

5. \r :           carriage return with backspace interpreter ‘-e‘ to have specified carriage return in output. Text before \r is not printed. 

Example:  echo -e "Geeks \rfor Geeks

          Output:-  for Geeks

 6. \v :          this option is used to create vertical tab spaces.

Example: echo -e "Geeks \vfor \vGeeks

Output:-  Geeks

for

Geeks

 

7. \a :          alert return with backspace interpreter ‘-e‘ to have sound alert.

Example: echo -e "\aGeeks for Geeks"

 

8. echo *:     this command will print all files/folders, similar to ls command.

Example: echo *

10.     Redirecting `echo` Output:-

The output of the `echo` can be redirected to a file instead of displaying it on the terminal. We can achieve this by using the `>` or `>>` operators for output redirection.

Example:  echo "Welcome GFG" > output.txt

This will write the output of the echo command to the file name `output.txt`. File will be overwritten if it already exists.

If we want to append the output in an existing file, we use `>>` instead of `>`.


Printf command in Linux 

printf” command in Linux is used to display the given string, number or any other format specifier on the terminal window. It works the same way as “printf” works in programming languages like C.

Syntax:

$ printf [-v var] format [arguments]

Note: printf can have format specifiers, escape sequences or ordinary characters.

Format Specifiers: The most commonly used printf specifiers are %s, %b, %d, %x and %f.


Script command in Linux 

script command in Linux is used to make typescript or record all the terminal activities. After executing the script command it starts recording everything printed on the screen including the inputs and outputs until exit. By default, all the terminal information is saved in the file typescript.

Syntax:

 $ script [options] [file]




Password command

The passwd command in Linux is a powerful tool that allows system administrators and users to manage password-related tasks.

Syntax

 $ passwd [options] [username] 





Uname command

The term “uname” stands for “Unix Name,”.

This command ‘uname‘ displays the information about the system.
Syntax
uname [OPTIONs]





Umask:-

Umask is a shell built in command though it is also exists as an external command. A user can also use this command to set a new default value.

The UNIX system has the following default permission for all files and directories

·        rw-rw-rw- (octal 666) for regular files

·        rwxrwxrwx (octal 777) for directories

Syntax:- $ umsak

Output- 022

Gzip:-

The gzip command in Linux is popular file comparison utility that can compress individual file to save disk space and make then easier to move to backups or archives.

Syntax:- gzip file_name

Output:- above file name is converting in zip file(file_name.gz)

Tar:-

For creating a disk archive that contain a group of file or directory structure, we need to use tar.

Option:-

-c – create an archive

-x – extract files from archive

-t – display files in archive

Find:-

·        It is recursively examines all files in the directories specified in path_list

·        It then match each files for one or more selecton_criteria.

·        It take some action on those selected files.

Telnet:-

·        It is used for remote communication and control.

·        Execute the talent commend followed by the host name or Ip address.

Syntax: $ telnet 192.168.35.12

ftp: -

·        It is used to transfer files between hosts.

·        Syntax:- $ ftp Saturn

Satrun is host name.

·        After establishing a connection with satrun, ftp  prompt for the username and password.

·        To quit ftp, break the connection with close.

unlink:-

·        It is used for delete a specified file name.

·        Unlink can only remove one file at a time.

·        Suntax:- $ unlink file_name

 

du and df:-

du command measurers the disk space occupied by files and directories

df command define the number of blocks used, number of block available and the directories where the file system is mounted.

Mount:-

Mount command is used to mount the filesystem found on a device a big tree structure rooted at ‘/’.

Syntax:- $ mount

Umount;-

Umound command can be used to detach these devices from the tree.

ulimit:-

Ulimit is a admin access required Linux shell command which is used to see, set, or limit the resources usage of current user.

Suntax:- $ ulimit

Arp:-

Arp stands for address resolution protocol.

It handle the mapping between an internet protocol (IP)address and media access control (MAC) address.

Cmp:-

·        It is used for comparing two files

·        Syntax: $ cmp file1 file2

Comm:-

comm command compare two sorted files line by line and write to standard  output.

Syntax:- $ comm file1 file2

Tr:-

Tr command is used for translating or deleting characters.

Syntax: cat file_name | tr [a-z] [A-Z]

Output:- all file character display in capital letters

Cpio:-

Cpio stands for copy in copy out.

It is used for processing the archieve files like *.cpuo or *.tar.

Option:

-i -extract file

-o -create the archive file and it runs only in copy out mode

-p -pass-through run-in copy pass mode

-t - print a table of content



Unit-2

Linux File System

·        A Linux file system is a structured collection of files on a disk drive or a partition.

·        A partition is a segment of memory and contains some specific data. In our machine, there can be various partitions of the memory.

·        The general-purpose computer system needs to store data systematically so that we can easily access the files in less time. 

·         It stores the data on hard disks (HDD) or some equivalent storage type. There may be below reasons for maintaining the file system:

·        Primarily the computer saves data to the RAM storage; it may lose the data if it gets turned off. However, there is non-volatile RAM (Flash RAM and SSD) that is available to maintain the data after the power interruption.

·        Data storage is preferred on hard drives as compared to standard RAM as RAM costs more than disk space. The hard disks costs are dropping gradually comparatively the RAM.

The Linux file system contains the following sections:

·        The root directory (/)

·        A specific data storage format (EXT3, EXT4, BTRFS, XFS and so on)

·        A partition or logical volume having a particular file system.



The Architecture of a File System:

A file system mainly consists of 3 layers. From top to bottom:

Logical file system interacts with the user application with the help of an API to provide open, read, close, etc. operations and passes requests to the layer below.

Virtual file system enables multiple instances of the physical file system to run concurrently.

Physical file system handles the physical aspect of the disk while managing and storing physical memory blocks being read and written.



Linux File System Structure

Linux file system has a hierarchal file structure as it contains a root directory and its subdirectories. All other directories can be accessed from the root directory. A partition usually has only one file system, but it may have more than one file system.

Linux file system contains two-part file system software implementation architecture. Consider the below image:



 

File descriptors in linux

File Descriptors are non-negative integers that act as an abstract handle to “Files” or I/O resources (like pipes, sockets, or data streams). These descriptors help us interact with these I/O resources and make working with them very easy.

Every process has it’s own set of file descriptors. Most processes (except for some daemons) have these three File Descriptors :

Descriptive Name

File Number

Description

Standard In

0

Input from the keyboard

Standard Out

1

Output to the console

Standard Error

2

Error output to the console

 

System calls

·        A system calls is a procedure that provides the interface between a process and the operating system. It is the way by which a computer program requests a service from the kernel of the operating system.

·        Different operating systems execute different system calls.

·        In Linux, making a system call involves transferring control from unprivileged user mode to privileged kernel mode; the details of this transfer vary from architecture to architecture. The libraries take care of collecting the system-call arguments and, if necessary, arranging those arguments in the special form necessary to make the system call.

System calls are divided into 5 categories mainly :

·         Process Control

·         File Management

·         Device Management

·         Information Maintenance

·         Communication




Process Control :

This system calls perform the task of process creation, process termination, etc.

The Linux System calls under this are fork() , exit() exec().

·         fork()

  • A new process is created by the fork() system call.
  • A new process may be created with fork() without a new program being run-the new sub-process simply continues to execute exactly the same program that the first (parent) process was running.
  • It is one of the most widely used system calls under process management.

·         exit()

  • The exit() system call is used by a program to terminate its execution.
  • The operating system reclaims resources that were used by the process after the exit() system call.

·         exec()

  • A new program will start executing after a call to exec()
  • Running a new program does not require that a new process be created first: any process may call exec() at any time. The currently running program is immediately terminated, and the new program starts executing in the context of the existing process.

 

File Management :

File management system calls handle file manipulation jobs like creating a file, reading, and writing, etc. The Linux System calls under this are open(), read(), write(), close().

·         open():

  • It is the system call to open a file.
  • This system call just opens the file, to perform operations such as read and write, we need to execute different system call to perform the operations.

·         read():

  • This system call opens the file in reading mode
  • We can not edit the files with this system call.
  • Multiple processes can execute the read() system call on the same file simultaneously.

·         write():

  • This system call opens the file in writing mode
  • We can edit the files with this system call.
  • Multiple processes can not execute the write() system call on the same file simultaneously.

·         close():

  • This system call closes the opened file.

 

Device Management :

Device management does the job of device manipulation like reading from device buffers, writing into device buffers, etc. The Linux System calls under this is ioctl().

·         ioctl():

  • ioctl() is referred to as Input and Output Control.
  • ioctl is a system call for device-specific input/output operations and other operations which cannot be expressed by regular system calls.

 

Information Maintenance:

It handles information and its transfer between the OS and the user program. In addition, OS keeps the information about all its processes and system calls are used to access this information. The System calls under this are getpid(), alarm(), sleep().

·         getpid():

  • getpid stands for Get the Process ID.
  • The getpid() function shall return the process ID of the calling process.
  • The getpid() function shall always be successful and no return value is reserved to indicate an error.

·         alarm():

  • This system call sets an alarm clock for the delivery of a signal that when it has to be reached.
  • It arranges for a signal to be delivered to the calling process.

·         sleep():

  • This System call suspends the execution of the currently running process for some interval of time
  • Meanwhile, during this interval, another process is given chance to execute

 

Communication :

These types of system calls are specially used for inter-process communications.

Two models are used for inter-process communication

1.     Message Passing(processes exchange messages with one another)

2.     Shared memory(processes share memory region to communicate)

The system calls under this are pipe() , shmget() ,mmap().

·         pipe():

  • The pipe() system call is used to communicate between different Linux processes.
  • It is mainly used for inter-process communication.
  • The pipe() system function is used to open file descriptors.

·         shmget():

  • shmget stands for shared memory segment.
  • It is mainly used for Shared memory communication.
  • This system call is used to access the shared memory and access the messages in order to communicate with the process.

·         mmap():

  • This function call is used to map or unmap files or devices into memory.
  • The mmap() system call is responsible for mapping the content of the file to the virtual memory space of the process.

Device drivers

Drivers are used to help the hardware devices interact with the operating system. In windows, all the devices and drivers are grouped together in a single console called device manager. In Linux, even the hardware devices are treated like ordinary files, which makes it easier for the software to interact with the device drivers. When a device is connected to the system, a device file is created in /dev directory. 

 

Most Common types of devices in Linux:

1.    Character devices – These devices transmit the data character by characters, like a mouse or a keyboard.

2.    Block devices – These devices transfer unit of data storage called a block, USB drives, hard drives, and CD ROMs

Disk and Driver Commands

 1. fdisk – It stands for format disk. This command is used to display the partitions on a disk and other details related to the file system.

Syntax: $sudo fdisk -l

2. sfdisk – This command displays the partitions on the disk, the size of   each partition in MB, etc.

3. parted – This command helps list and modify the partitions of the disk.

Syntax: $sudo parted -l

4. df – Displays the details of the file system. Using grep we can filter real hard disk files.

Syntax: $df -h | grep ^/dev

5. lsblk – List details about the block devices.

Syntax: $lsbl

6. Lseek:- The lseek function in Linux is used to change the file offset (position) within an open file. It allows you to move the read/write pointer to a specific position within the file.
Syntax: off_t lseek(int fd, off_t offset, int whence);
7.     Symlink:-
·        It is also called symbolic link or soft link.
·        It is similar to shortcuts and points to another file or folder on your computer.
·        Sym(soft) link are created using “ln” command with -s option
·        Syntax: $ ln -s option
 $ ln -s(path-to-file) symboliclink_file_name
·        Example: ln -ltrh
Display total file
·         $ ln -s file.txt filename
$ ln -ltrh
8.     Unlink:- It remove the symbolic link
Syntax:- $ unlink
9.     Stat:- stat is a linux command line utility that displays a detailed information about a file or a file system. It retrieves information such as file type; access rights in octal and human-readable; SELinux security context string; time of file creation, last data modification time,






Comments

Popular posts from this blog

Class 10th IT(402) sample paper

CLASS 12TH WEBAPPLICATION(CODE 803) PRACTICAL FILE

class 12th Unit-3 Web Scripting - Java Script