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
- 1UNIX is a family of multitasking,
multiuser computer operating systems.
- 2 It was originally developed for mini
computers.
- 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
Uname command
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.
Link:-
· A connection between a file name and actual data on the disk.
· We can call it a shortcut.
· “ln” used for link.
· There are two types:
1. Soft link 2. Hard link
Soft link :-
· Soft links are very common. It represents a virtual or abstract location of the file.
· It is just like the shortcuts created in Windows.
· A soft link doesn't contain any information or content of the linked file, instead it has a pointer to the location of the linked file.
· Link will be removed if original file is deleted or removed.
· Syntax:- $ ln -s filename.txt file_softlink_name
· Soft link show in blue color.
· Inode no. are different of same file in soft link command.
Hard link:-
· Deleted , renaming or removing the original file will not effect the link.
· $ ln myfile myfile_hard_link
· Original copy and hard link copy inode no. is same.
· Remove file also show in case of hard link.
Chmod:-
· Chmod stand for change mode.
· Command allows an administrator to set or modify a file permission.
· Syntax:- $ chmod <operation> file/directoryname
· There are two type of operation:
1. User level operation 2. File level operation
User level operation :
U- grant permission to user
g - grant permission to group
o - grant permission to others
a - grant permission to all
File level operation:
r - grant read permission
w - grant write permission
x – grant execute permission
· Example:-
$ chmod +r filename (+r means add read permission)
$ chmod -r filename (-r means remove read permission)
$ chmod a+rwx filename (change permission to all r,w,x)
Chown:-
· Chown stand for change owner.
· To change user ownership of file or directory.
· Ownership is a key to understanding the file access, security and resource management.
· Chown work with sudo command.
· Syntax:- $sudo chown -c file1 file2
Ask password of linux: enter password of linux
Finally change the fileownership
Vi editor:-
· Vi editor Used for edit file in linux system
· It can used for write program code in linux system.
· Following mode in vi editor:
v Press ESC key à normal mode
v Press i key à insert mode
v Press :q! à exit mode
v Press :wq! à save the updated file
v Press :w filename.txtà save file as .txt file
Environment & path setting:-
Environment variable:- environment store information about the shell session & the working environment.
Var_name= value
· Syntax:- $ printenv
· Specific environment variable
$ printenv HOME
$ echo $ username
· Change the user name
$ su -Username
Environment in linux:-
v USER à current logged
v HOME à Directory of current user
v SHELL à path of current user
v LANG à current language
v MAIL à location of where the current user mail stored
Type of environment variable:
1. Global variable
2. Local variable
Global environment variable:- when defined in a terminal, enjoy accessibility from anywhere within that terminal environment.
Setting global environment variable in terminal:
$ export NAME=value
Local environment variable:- Only working in current cell.
Setting local environment variable : $ NAME= value
Delete environment variable:-
Delete environment variable in current session use the “unset” command followed by the name of the variable.
Syntax: $ unset NAME
UNIT-3 Shell Basics
Shell:- It is a special user program that provide an interface for the user to use operating system services.
Type of shell:-
1. Command line shell
2. Graphics shell
Command Line shell:- Shell can be accessed by user using a command line interface. A specific program called terminal in linux.
A unique program known as Command prompt in Windows or Terminal in macOS/Linux is offered for typing in the human-understandable commands like "ls", "cat", etc and after that, it is being run.
Graphics Shell:-
These shells specify the manipulation of programs that are based on the graphical user interface (GUI) by permitting for operations like moving, closing, resizing, and opening windows. Users don't need for typing in any command for all the actions.
Shells in linux:-
1. Bash Shell:-
v Bash means Bourne Again Shell.
v It is a default shell over several distributions of Linux today.
v It is a sh-compatible shell
2. Csh/Tcsh Shell:-
v Tcsh is an upgraded C shell.
v This shell can be used as a shell script command processor and interactive login
shell.
3. Ksh Shell:-
v Ksh means for Korn shell.
v Ksh shell is a high-level, powerful, and complete programming language
4. Zsh Shell
5. Fish:-
Fish stands for "friendly interactive shell"
Shell scripting:-
· Shells are interactive, which means they accept commands as input from users and execute them.
· shell can also take commands as input from file, we can write these commands in a file and can execute them in shell
to avoid this repetitive work. These files are called Shell Scripts or Shell Programs.
· Each shell script is saved with `.sh` file extension e.g., myscript.sh.
· A shell script comprises the following elements –
· Shell Keywords – if, else, break etc.
v Shell commands – cd, ls, echo, pwd, touch etc.
v Functions
v Control flow – if..then..else, case
and shell loops etc.
Need shell scripts:-
There are many reasons to write shell scripts:
· To avoid repetitive work and automation
· System admins use shell scripting for routine backups.
· System monitoring
· Adding new functionality to the shell etc.
Standard Input:-
· It is denoted by stdin
· Input stream (stdin) used for getting data from the user.
· Keyboard is standard input.
Standard output:-
· It is denoted by stdout
· The screen is default standard output, which means that the result of a command are
displayed on the screen.
Standard error:-
· It is denoted by stderr
· The screen is also defaulting standard error, which means that error message is
displayed on the screen.
Redirection:-
·
Redirection is a feature in Linux which can be used to change the standard input device
(keyboard) or standard output device (screen) during the execution of a
command.
· The basic process of any Linux command is that it takes an input and gives output.
Type of Redirection: -
1. Standard input(stdin)
2. Standard output (stdout)
3. Standard error (stderr)
File Descriptors: -
· file descriptor is a number that uniquely identifies an open file.
· It describes a data resource, and how that resource may be accessed.
· There are three standard file descriptors.
1. Standard input (stdin) file descriptor 0
2. Standard output (stdout) file descriptor 1
3. Standard error(stderr) file descriptor 2
output (stdout-1) :-
· Output of command shown in terminal.
· To route output in file using “>”
$ ls > file.txt
· To
append output in existing file using “>>”
$ pwd >> file.txt
· Example:- $ echo “hello” > filename
$ cat filename
o/p à hello
$ echo “buddy” >> filename
$ cat filename
o/p à hello
buddy
Error (stderr-2):-
If any command gives you error then is considered as stderr-2. We can redirect the error to a file.
Example:- $ cd/root/2 > error_file
$ cat error_file
o/pà -bash : cd : /root/ : permission denied
· To redirect both error & output in file
$ ls && >> error.txt
Pipes(piping):-
· The pipe is used to combine two or mor commands and in this, the output of one
command acts as i/p to another command.
· Pipe command denoted by “|” symbol
· It can also be visualized as a temporary connection b/w two or more command/programs.
· This Type command line program that does the further processing is referred to as
filters.
· Syntax:-
$ command1 | command2 | command3
· Example:
$ ls -1 | wc -l
Output:- display total no. of file
$ cat mits.txt mit.txt | sort
Output:- display combine both file in
sorted order
$cat mits.txt | sort | uniq
Output:- Display unique value from file
$ cat mits.txt | head -38
Output:- display starting 37 lines
Tee command:-
Tee command reads standard input and copies it both to stdout and to a file.
Syntax:- $ ls | tee files.txt
Output:- all file name save as text
in file.txt file
Xargs:-
It convert the stdinput into command line argument.
Syntax:- $ ls | xargs
echo
Grep command or filters special characters for searching files and pathnames:-
· The 'grep' command stands for "global regular expression print"
· Grep command search for particular string/keywords from a file & print lines
matching a pattern.
· It checks line by line & print lines matching given pattern.
· We can use grep anywhere like with files searching for files, directories etc.
· Grep command is case sensitive
· Syntax:
- $ grep [option] pattern[file]
· Example:
$ grep Name file.txt
Option decription
–I Ignores, case for matching
-n Display the matched lines and their line numbers.
-c This prints only a count of the lines that match a pattern
-v This prints out all the lines that do not matches the pattern
Command separation & grouping
groups command is a straightforward tool in Linux for listing
user groups. It shows the groups associated with the current user when run
without arguments, and can also display groups for any specified user.
Syntax:- $ group
Output:- display all group name
$ less /etc/group
Output:- display all information about group
Role of group:-
1. Permission management
2. Efficient user management
3. Security
Manage the group account: -
1. Add a group: $ groupadd
2. Modify a group : $ groupmod
3. Delete a group : $ groupdel
Manage user
1. Useradd
2. Usermod
3. Userdel
Create a User :-
$ useradd username
(for eg. Username is MITS)
$ id username
Output:- display all information related to the username
$ useradd -g QA -s /bin/bash -c “ part of QA’ -m -d /home/victor victor
(-g denote group, -s denote shell type, -c denote comment, -m
denote home, -d denaote where add user, QA denote group name)
Delet user:-
$ userdel -r username
Modify user :-
$ sudo usermod -G QA username
Stack manipulation in linux:-
The pushd built in command pushes directories onto a stack and popd command removes them.
The ‘pushd’ command is a built-in shell command that simplifies the navigation and management of directories by using a directory
stack. This stack operates on the Last In, First Out (LIFO) principle, allowing
users to push directories onto the stack and easily switch between them.
Syntax of ‘pushd’ command:
$ pushd ~/Desktop (add directory in stack)
pushd ~/Templates
pushd ~/Videos
pushd ~/Downloads
pushd ~/Music
pushd ~/Downloads
popd command is used to remove directories from the directory stack. The “d” in popd stands
for the directory as it removes the directory path onto the stack. After this
command is executed, the present directory stack is displayed as a list of
space-separated directories. The directory stack decreases in size after each
popd command. This directory stack is based on the Last In First Out (LIFO)
principle.
Syntax:-
$ popd (remove last directory from the satck)
Process in linux:-
Processes are multiple instances of executing program & scripts. There are many processes running at any given time in linux and theyshare the CPU.
Process can be run in two ways(method):
Method 1: Foreground Process : Every process when started runs
in foreground by default, receives input from the keyboard, and sends output to
the screen.
Syntax: $ fg
$ fg jobname
Method 2: Background Process: It runs in the background without
keyboard input and waits till keyboard input is required. Thus, other processes
can be done in parallel with the process running in the background since they
do not have to wait for the previous process to be completed.
Syntax: $ bg
Example:- $ sleep 50s
Press ctrl+z to stopped the process
kill by PID:- $ kill 1001
$ job (display running job)
UNIT-4
Write and execute shell script
1. Open the terminal
2. Open the text editor i.e VI editor using filename $ vi filename
3. Write any text in vi editor with the help of “echo” command echo “hello world”
press “esc” key à normal mode
:wq! à save the file and exit from vi editor
Press “ I “ key in vi editor à insert mode
4. Run the program
$ sh filename
Output:- hello world
Different type of shell
1) .bash 2) .sh 3) .ksh 4) .fish 5) .zsh
Check default shell
$ echo $o
Check which shell present
$ cat /etc/shells
Three method to run the program
1. sh filename
2. ./filename.sh
a) permission denied
b) chmod +x filename.sh
c) ./filename.sh
3. ./path/filename.sh
Comments
Post a Comment