linux unit 3
linux unit 3
UNIT 3
Meaning and purpose of shell:
What is a Shell?
A shell is a type of computer program called a command-line interpreter that lets Linux and Unix
users control their operating systems with command-line interfaces. Shells allow users to
communicate efficiently and directly with their operating systems.
Shell scripting is not a single language but, because it uses some natural language commands,
it’s easy to learn, even without a programming background. However, each shell scripting dialect
is considered a language, and if you plan more complex activities, shells take a lot of practice.
Shell scripting is one of the simplest ways to set up automation. Using Linux or Unix commands,
shell scripting gives data scientists, DevOps, and TechOps people conditional and loop control
structures to repeat commands.
Shell scripts don’t always go by the same name. Sh, Bash (the most common), csh, and tesh are
all shell scripts. While in IBM’s VM operating system, they’re called EXEC; in DOS, shell
scripts are called batch files.
A shell has two categories, command-line shell and graphical shell. Command-line shells are
accessed using command-line interfaces where systems receive input in human-readable
commands and then use the command-line interfaces to display the output.
Graphical shells use a graphical user interface (GUI) to perform interactions and essential
operations such as opening, closing, and saving files.
As cool as the name may imply, the Bourne shell has nothing to do with spies and high-speed car
chases. The Bourne shell, the second most commonly used Unix shell, was created in 1979 by
Stephen Bourne at Bell Labs. Like its predecessor, the Thompson shell, Bourne’s executable
filename is sh.
The Bourne shell is the default shell of the Solaris OS. Despite its age, the Bourne shell is
popular today because of its speed and compactness. However, the Bourne shell isn’t very
interactive and can’t recall its command history. It also lacks logical and arithmetic expression.
The C shell, filename csh, and its predecessor filename tcsh, is another Unix shell from the late
70s. It was created by Bill Joy, who at the time was a graduate student from the University of
California, Berkeley.
Unlike the Bourne shell, the C shell is interactive and recognizes command history and aliases. C
shells also include C-like expression syntax and built-in arithmetic.
The Korn Shell, filename ksh, was developed in the early 1980s by David Korn at Bell Labs.
Korn Shell includes many of the features of the C shell, and it’s a super-set of, and therefore is
backward-compatible, with the Bourne shell.
The Korn Shell runs faster than the C-shell. It runs Bourne shell scripts and features C-like
arrays, functions, and string-manipulation facilities. In addition, it features built-in arithmetic.
:
Bourne-Again shell
The GNU Bourne-Again, or Bash, shell, is an open-source alternative to the Bourne shell. It was
designed by Brian Fox for the GNU Project and was released in 1989. Not only is it fully
compatible with the Bourne shell, it takes many of the best features from KornShells and C
shells. The GNU Bourne-Again shell has automatically mapped arrow keys for editing and
command recall.
dirs
dirs [-clpv] [+N | -N]
Display the list of currently remembered directories. Directories are added to the list
with the pushd command; the popd command removes directories from the list. The
current directory is always the first directory in the stack.
-c
Clears the directory stack by deleting all of the elements.
-l
Produces a listing using full pathnames; the default listing format uses a tilde to
denote the home directory.
-p
Causes dirs to print the directory stack with one entry per line.
-v
Causes dirs to print the directory stack with one entry per line, prefixing each entry
with its index in the stack.
+N
Displays the Nth directory (counting from the left of the list printed by dirs when
invoked without options), starting with zero.
-N
Displays the Nth directory (counting from the right of the list printed by dirs when
invoked without options), starting with zero.
popd
popd [-n] [+N | -N]
Removes elements from the directory stack. The elements are numbered from 0
starting at the first directory listed by dirs; that is, popd is equivalent to popd +0.
When no arguments are given, popd removes the top directory from the stack and
changes to the new top directory.
-n
Suppresses the normal change of directory when removing directories from the
stack, so that only the stack is manipulated.
+N
Removes the Nth directory (counting from the left of the list printed by dirs),
starting with zero, from the stack.
-N
Removes the Nth directory (counting from the right of the list printed by dirs),
starting with zero, from the stack.
If the top element of the directory stack is modified, and the -n option was not
supplied, popd uses the cd builtin to change to the directory at the top of the stack. If
the cd fails, popd returns a non-zero value.
Otherwise, popd returns an unsuccessful status if an invalid option is encountered, the
directory stack is empty, or a non-existent directory stack entry is specified.
If the popd command is successful, Bash runs dirs to show the final contents of the
directory stack, and the return status is 0.
pushd
pushd [-n] [+N | -N | dir]
Adds a directory to the top of the directory stack, or rotates the stack, making the new
top of the stack the current working directory. With no arguments, pushd exchanges
the top two elements of the directory stack.
-n
Suppresses the normal change of directory when rotating or adding directories to
the stack, so that only the stack is manipulated.
+N
Brings the Nth directory (counting from the left of the list printed by dirs, starting
with zero) to the top of the list by rotating the stack.
-N
Brings the Nth directory (counting from the right of the list printed by dirs, starting
with zero) to the top of the list by rotating the stack.
Processes in Linux/Unix
Last Updated : 07 Dec, 2022
A program/command when executed, a special instance is provided by the system to
the process. This instance consists of all the services/resources that may be utilized by
the process under execution.
Whenever a command is issued in Unix/Linux, it creates/starts a new process. For
example, pwd when issued which is used to list the current directory location the
user is in, a process starts.
Through a 5 digit ID number Unix/Linux keeps an account of the processes, this
number is called process ID or PID. Each process in the system has a unique PID.
Used up pid’s can be used in again for a newer process since all the possible
combinations are used.
At any point of time, no two processes with the same pid exist in the system
because it is the pid that Unix uses to track each process.
Initializing a process
A process can be run in two ways:
Method 1: Foreground Process : Every process when started runs in foreground by
default, receives input from the keyboard, and sends output to the screen. When
issuing pwd command
$ ls pwd
Output:
$ /home/geeksforgeeks/root
When a command/process is running in the foreground and is taking a lot of time, no
other processes can be run or started because the prompt would not be available until
the program finishes processing and comes out.
$ kill 19
Terminated
If a process ignores a regular kill command, you can use kill -9 followed by the
process ID.
$ kill -9 19
Terminated
Other process commands:
bg: A job control command that resumes suspended jobs while keeping them running
in the background
Syntax:
bg [ job ]
For example:
bg %19
fg: It continues a stopped job by running it in the foreground.
Syntax:
fg [ %job_id ]
For example
fg 19
top: This command is used to show all the running processes within the working
environment of Linux.
Syntax:
top
nice: It starts a new process (job) and assigns it a priority (nice) value at the same
time.
Syntax:
nice [-nice value]
nice value ranges from -20 to 19, where -20 is of the highest priority.
renice : To change the priority of an already running process renice is used.
Syntax:
renice [-nice value] [process id]
df: It shows the amount of available disk space being used by file systems
Syntax:
df
Output:
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/loop0 18761008 15246876 2554440 86% /
none 4 0 4 0% /sys/fs/cgroup
udev 493812 4 493808 1% /dev
tmpfs 100672 1364 99308 2% /run
none 5120 0 5120 0% /run/lock
none 503352 1764 501588 1% /run/shm
none 102400 20 102380 1% /run/user
/dev/sda3 174766076 164417964 10348112 95% /host
free: It shows the total amount of free and used physical and swap memory in the
system, as well as the buffers used by the kernel
Syntax:
free
Output:
total used free shared buffers
cached
Mem: 1006708 935872 70836 0 148244
346656
-/+ buffers/cache: 440972 565736
Swap: 262140 130084 132056
Types of Processes
1. Parent and Child process : The 2nd and 3rd column of the ps –f command shows
process id and parent’s process id number. For each user process, there’s a parent
process in the system, with most of the commands having shell as their parent.
2. Zombie and Orphan process : After completing its execution a child process is
terminated or killed and SIGCHLD updates the parent process about the
termination and thus can continue the task assigned to it. But at times when the
parent process is killed before the termination of the child process, the child
processes become orphan processes, with the parent of all processes “init” process,
becomes their new pid.
A process which is killed but still shows its entry in the process status or the
process table is called a zombie process, they are dead and are not used.
3. Daemon process : They are system-related background processes that often run
with the permissions of root and services requests from other processes, they most
of the time run in the background and wait for processes it can work along with for
ex print daemon.
When ps –ef is executed, the process with ? in the tty field are daemon processes.