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

linux unit 3

The document provides an overview of shell programming in Linux, explaining the purpose and types of shells, including Bourne, C, Korn, and Bash shells. It covers shell scripting basics, process management, and commands for tracking and manipulating processes, as well as piping in Unix/Linux for data transfer between commands. Additionally, it describes different process types, including parent, child, zombie, orphan, and daemon processes.

Uploaded by

Atul Gaur
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 views10 pages

linux unit 3

The document provides an overview of shell programming in Linux, explaining the purpose and types of shells, including Bourne, C, Korn, and Bash shells. It covers shell scripting basics, process management, and commands for tracking and manipulating processes, as well as piping in Unix/Linux for data transfer between commands. Additionally, it describes different process types, including parent, child, zombie, orphan, and daemon processes.

Uploaded by

Atul Gaur
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/ 10

LINUX AND SHELL PROGRAMMING

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.

Here are some sample shell commands:

To find out what directory you’re in: pwd

To find a command in the manual: man command

To make a text file scrollable: less file 1 or more file 1


Different types of shells
Bourne Shell

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.

Bourne prompts include:

 Command full-path: /bin/sh and /sbin/sh


 Non-root user default: $

 Root user default: #


The C shell

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.

command-line prompts include:

 Command full-path: /bin/csh

 Non-root user default hostname: %


 Root user default hostname: #
KornShell

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.

Directory stack manipulation

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.

Arguments, if supplied, have the following meanings:

-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.

Arguments, if supplied, have the following meanings:

-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.

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.
Adding & along with the command starts it as a background process
$ pwd &
Since pwd does not want any input from the keyboard, it goes to the stop state until
moved to the foreground and given any data input. Thus, on pressing Enter:
Output:
[1] + Done pwd
$
That first line contains information about the background process – the job number
and the process ID. It tells you that the ls command background process finishes
successfully. The second is a prompt for another command.

Tracking ongoing processes


ps (Process status) can be used to see/list all the running processes.
$ ps

PID TTY TIME CMD


19 pts/1 00:00:00 sh
24 pts/1 00:00:00 ps
For more information -f (full) can be used along with ps
$ ps –f

UID PID PPID C STIME TTY TIME CMD


52471 19 1 0 07:20 pts/1 00:00:00f sh
52471 25 19 0 08:04 pts/1 00:00:00 ps -f
For single-process information, ps along with process id is used
$ ps 19

PID TTY TIME CMD


19 pts/1 00:00:00 sh
For a running program (named process) Pidof finds the process id’s (pids)
Fields described by ps are described as:
 UID: User ID that this process belongs to (the person running it)
 PID: Process ID
 PPID: Parent process ID (the ID of the process that started it)
 C: CPU utilization of process
 STIME: Process start time
 TTY: Terminal type associated with the process
 TIME: CPU time is taken by the process
 CMD: The command that started this process
There are other options which can be used along with ps command :
 -a: Shows information about all users
 -x: Shows information about processes without terminals
 -u: Shows additional information like -f option
 -e: Displays extended information
Stopping a process:
When running in foreground, hitting Ctrl + c (interrupt character) will exit the
command. For processes running in background kill command can be used if it’s pid
is known.
$ ps –f

UID PID PPID C STIME TTY TIME CMD


52471 19 1 0 07:20 pts/1 00:00:00 sh
52471 25 19 0 08:04 pts/1 00:00:00 ps –f

$ 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.

Piping in Unix or Linux



A pipe is a form of redirection (transfer of standard output to some other destination)
that is used in Linux and other Unix-like operating systems to send the output of one
command/program/process to another command/program/process for further
processing. The Unix/Linux systems allow the stdout of a command to be connected
to the stdin of another command. You can make it do so by using the pipe
character ‘|’.
The pipe is used to combine two or more commands, and in this, the output of one
command acts as input to another command, and this command’s output may act as
input to the next command, and so on. It can also be visualized as a temporary
connection between two or more commands/ programs/ processes. The command line
programs that do the further processing are referred to as filters.
This direct connection between commands/ programs/ processes allows them to
operate simultaneously and permits data to be transferred between them continuously
rather than having to pass it through temporary text files or through the display
screen.
Pipes are unidirectional i.e., data flows from left to right through the pipeline.
Syntax:
command_1 | command_2 | command_3 | .... | command_N

You might also like