Content-Length: 3045724 | pFad | https://www.scribd.com/document/662108586/LINUX-UNIT-III

5 Linux Unit Iii | PDF
0% found this document useful (0 votes)
86 views33 pages

Linux Unit Iii

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 33

LINUX UNIT III

SHELL BASICS

SHELL BASICS: Meaning and purpose of shell, Introduction to types of shell. The command line,
standard input and standard output, redirection, pipes, filters special characters for searching
files and pathnames., command separation & grouping, redirection, directory stack
manipulation, processes

1. Linux Shell

The shell can be defined as a command interpreter within an operating system like
Linux/GNU or Unix. It is a program that runs other programs. The shell facilitates every user
of the computer as an interface to the Unix/GNU Linux system. Hence, the user can execute
different tools/utilities or commands with a few input data.

The shell sends the result to the user over the screen when it has completed running a
program which is the common output device. That's why it is known as "command
interpreter".

The shell is not just a command interpreter. Also, the shell is a programming language with
complete constructs of a programming language such as functions, variables, loops,
conditional execution, and many others.

For this reason, GNU/Unix Linux Shell is stronger than the Windows shell.

Broadly, the shell is categorized into two main categories which are explained below:

1|Page
2. Graphical Shells

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 and switching focus among windows as well. Ubuntu OS or Windows OS could be
examined as a good example that offers a graphical user interface to the user to interact with
the program. Various users don't need for typing in any command for all the actions.

Command-line Shell

Various shells could be accessed with the help of a command-line interface by users. 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. The result is further shown to the user on the terminal.

Working on a command-line shell is a complicated for many beginners due to it is hard to


remember several commands. Command-line shell is very dominant and it permits users for
storing commands in a file and run them together. In this way, a repetitive action could be
automated easily. Usually, these files are known as Shell scripts in macOS/Linux systems and
batch files in Windows.

There are various types of shells which are discussed as follows:

2|Page
i. Bash Shell

In the bash shell, bash means Bourne Again Shell. It is a default shell over several
distributions of Linux today. It is a sh-compatible shell. It could be installed over Windows
OS. It facilitates practical improvements on sh for interactive and programming use which
contains:

o Job Control

o Command-line editing

o Shell Aliases and Functions

o Unlimited size command history

o Integer arithmetic in a base from 2-64

ii. Csh/Tcsh Shell

Tcsh is an upgraded C shell. This shell can be used as a shell script command processor and
interactive login shell. Tcsh shell includes the following characteristics:

o C like syntax

o Filename completion and programmable word

o Command-line editor

o Job control

o Spelling correction

iii. Ksh Shell

Ksh means for Korn shell. It was developed and designed by David G. Korn. Ksh shell is a
high-level, powerful, and complete programming language and it is a reciprocal command
language as well just like various other GNU/Unix Linux shells. The usage and syntax of the
C shell are very same as the C programming language.

3|Page
iv. Zsh Shell

Zsh shell is developed to be reciprocal and it combines various aspects of other GNU/Unix
Linux shells like ksh, tcsh, and bash. Also, the POSIX shell standard specifications were
based on the Korn shell.
Also, it is a strong scripting language like other available shells. Some of its unique features
are listed as follows:

• Startup files

• Filename generation

• Login/Logout watching

• Concept index

• Closing comments

• Variable index

• Key index

• Function index and various others that we could find out within the man pages.

All these shells do a similar job but take different commands and facilitate distinct built-in
functions.

v. Fish

Fish stands for "friendly interactive shell". It was produced in 2005. Fish shell was developed
to be fully user-friendly and interactive just like other shells. It contains some good features
which are mentioned below:

• Web-based configuration

• Man page completions

• Auto-suggestions

• Support for term256 terminal automation

• Completely scripted with clean scripts

4|Page
Shell Prompt

It is known as a command prompt and it is issued via the shell. We can type any command while
the prompt is shown. Shell reads our input after we click Enter. It illustrates the command we
want to be run by seeing at the initial word of our input. A word can be defined as the character's
unbroken set. Tabs and spaces separate words.

The below is a common data command example that shows the current time and date:

We can also customize our command prompt with the help of PS1 (environment variable).

Shell Scripting

The common concept of the shell script is the command list. A good shell script will contain
comments which are preceded via # simbol.

Shell functions and scripts are interpreted. It means they aren't compiled.

There are also conditional tests like value Y is greater than value Z, loops permitting us to
proceed by massive data amounts, files to store and read data, and variables to store and
read data, and these scripts may contain functions.

The shells are usually interactive which means they receive commands as input through the
users and run them. Although sometimes we routinely wish to run a set of commands, hence,
we have to type within the commands all-time inside the terminal.

A shell script includes syntax similarly to other programming languages. When we have prior
experience along with a programming language such as C/C++, Python, etc. It will be very
easy to begin with it. The shell script combines the below components:

Functions

• Control flow: if, else, then, shell loops, case, etc.

• Shell commands: touch, pwd, echo, ls, cd, etc.

• Shell keywords: break, if, else, etc.

Shell Scripts Need

There are several causes to write these shell scripts:

5|Page
• For avoiding automation and repetitive work

• Shell scripting is used by system admins for many routine backups

• Including new functionalities to the shell

• System monitoring etc.

Shell Script Advantages

• The syntax and command are exactly similar to those entered directly in a command
line. Thus, the programmers don't have to switch to completely different syntax

• Interactive debugging

• Quick start

• It is much quicker to write the shell scripts etc.

Shell Script Disadvantages

• A single error can modify the command which could be harmful, so prone to very
costly errors.

• Design flaws in the language implementation and syntax.

• Slower execution speed.

• Offer minimal data structure dissimilar other scripting languages.

• Not well suited for complex and large tasks etc.

Script Example

Assume we make a test.sh script. We have to alert a system that the shell script is started
before we include anything else in our script.

Note: Every script will have a .sh extension.

It can be done with the help of the shebang construct.

For example:

#!/bin/sh

6|Page
It shows the system that several commands that pursue are to be run by the Bourne shell. It
is known as the shebang due to the symbol # is known as hash and the symbol ! It is known
as the bang.

To make a script including these commands, we put the shebang construct line first, and after
that add any command:

1. #!/bin/bash

2. pwd

3. Is

Comments in shell

We can put our comments in our script below:

#!/bin/bash
Make the script runnable and save the content mention above:

$chmod +x test.sh

Now, the shell script is ready to be run:

$./test.sh

Under execution, we will get the result as follows:

/home/amrood

index.htm unix-basic_utilities.htm unix-directories.htm

test.sh unix-communication.htm unix-environment.htm

Note: - For executing a program present in the latest directory, we can use the ./program_name

Extended Shell Scripts

The shell scripts contain several needed constructs that define the shell platform when to do
it and what to do. Most of the scripts are more complicated than the above scripts.

7|Page
After all, the shell is an actual programming language that is complete with control
structures, variables, and so on. Still, it is only the commands list executed sequentially no
matter how complex a script gets.

The following script is using the read command that gets the input through the keyboard and
appoints it as the variable PERSON value and prints it over STDOUT finally.

The command line, standard input and standard output, redirection

Whenever running any command in the terminal, stdin, stderr, and stdout are three data
streams that bash creates. If you’re familiar with the command line, you may already have
taken advantage of these features. Essentially, they allow piping/redirecting data from one
command to another.

Let’s check out how stdin, stderr, and stdout works and how you can use them as well.

stdin, stdout and stderr

In computing, the term stream refers to something that can transfer data. Here, all three
streams carry text as the data.

Similar to water streams, data streams also have two endpoints. There are a source and an
outflow. Whatever command you’re running in the terminal will be at either point of the
stream. Using the stream, you can connect two terminal windows, two different commands,
and even files!

Let’s have a quick breakdown of the special streams.

• stdin: Stands for standard input. It takes text as input.

• stdout: Stands for standard output. The text output of a command is stored in
the stdout stream.

• stderr: Stands for standard error. Whenever a command faces an error, the
error message is stored in this stream.

In Linux, almost all the streams are treated as if they were files. Just like you can read/write
a file, you can read/write data from these streams.

8|Page
An easy way to access any file is by using the unique file descriptor number associated with
it. In the case of these streams, there are unique values assigned to each one of them.

• 0: stdin

• 1: stdout

• 2: stderr

stdin, stdout, and stderr in action

Let’s get started by learning more about these streams through action, we will start with
stdin.

Run the following command.

$ read

The command will require input from the keyboard. Here, the read tool is getting the input
from stdin. Now let’s look at stdout.

Run the command first.

$ ls -l

9|Page
Here, the ls command lists the file(s) in the current directory. The list is sent to stdout and
the terminal prints it out. Let’s check stderr now.

There are different ways an error may occur. For this example, sending ls an invalid
argument will result in an error.

$ ls -l anything

Here, there’s no file named anything. That’s why the message ls returns is sent to stderr.

Piping

This is a common technique that takes full advantage of the stdin and stdout streams. Let’s
explain it with an example.

$ echo "hello world" | grep hello

Here, the | sign is responsible for piping. The output echo generates is written in
the stdout stream. Then, the piping redirects the content of stdout to stdin for the grep
command. That’s how grep knows what content to perform the operation on.

If you want to pipe both the stderr and stdout to the next command, then use the “|&” instead.

$ echo “hello world” |& cat

10 | P a g e
$ anything |& cat

Redirecting streams

Now we know how these streams work, let’s have a look at how you can redirect them.
Piping is a form of redirection. However, it only involves stdin and stdout. Bash allows
specific control over all three of the streams.

To redirect the stdout content to a file, add the “>” angle followed by the target file name.

$ echo “hello world” > hello.txt

Here, the output of the echo command will be stored in the hello.txt file.

If the file did already exist, then the command above will overwrite it. To avoid it, make
sure that the file name is unique. If you don’t want to overwrite, you may want to use “>>”
instead. It appends the output at the end of the target file.

$ echo "hello world" >> hello.txt

11 | P a g e
The goal of stdin is to work with input. This can also be redirected. For example, instead of
typing the input from the keyboard, it can be loaded from a file.

In this command, cat will take its input directly from the hello.txt file.

$ cat < hello.txt

Input Output Redirection in Linux

The work of any command is either taking input or gives an output or both. So, Linux has
some command or special character to redirect these input and output functionalities. For
example: suppose we want to run a command called “date” if we run it will print the output
to the current terminal screen. But our requirement is different, we don’t want the output
to be displayed on the terminal. We want the output to be saved in a file. This could be done
very easily with output redirection. Redirection here simply means diverting the output or
input.

Similarly, if we have a command that needs input to be performed. Let take a command
“head” this needs input to give output. So either we write input in the form of command
directly or redirect input from any other place or file. Suppose we have a file called “file.txt”
to print the starting some lines of the file we could use the “head”. So, let’s see how this all
is done on the terminal.

Types of Redirection

1. Overwrite

• “>” standard output

• “<” standard input

2. Appends

• “>>” standard output

12 | P a g e
• “<<” standard input

3. Merge

• “p >& q” Merges output from stream p with stream q

• “p <& q” Merges input from stream p with stream q

Implementation: So whatever you will write after running this command, all will be
redirected and copied to the “file.txt”. This is standard output redirection.

cat > file.txt

Now, this is standard input redirection, cat command will take the input from “file.txt”
and print it to the terminal screen. This line of code also shows the real working and
meaning of the cat command that is copy and paste. Many people have a misconception
that the cat is used to create a file, but it is not true, the main work of the cat is to
copy the input and give the output to the screen.

cat < file.txt

Let’s see an example to understand the real work of cat command

cat

Just type cat on the terminal and hit enter. It will ask for the input lines, you could
write your name and hit enter. You will see your input will be reprinted.

13 | P a g e
(base) [root@localhost ~]# cat

Hello this is GeeksForGeeks

Hello this is GeeksForGeeks

This is used when we want to append some lines to the existing content of the file. If you
use only a single angular bracket all the content of the file will be lost.

cat >> file.txt

To see the working of append standard input:

A here-document is used to redirect input into an interactive shell script or program. You
can run any program within a shell script without user action by supplying the required
input for the interactive program, or interactive shell script.

The general form for a here document is −

Syntax:

command << delimiter


document
delimiter
(base) [root@localhost ~]# cat << helo.txt
> Hello This is
> GeeksForGeeks
helo.txt
Hello This is
GeeksForGeeks

14 | P a g e
(base) [root@localhost ~]#
Note: Here, helo.txt is a delimiter.

The delimiter marks the ending point of the document. Without it, the shell continues
to read the input forever. The delimiter must be a single word that does not contain spaces
or tabs.

Error Redirection:

Error redirection is transferring the errors generated by some false commands to a file
rather than STDOUT.

Whenever a program is executed at the terminal, 3 files are generated: standard input(0),
standard output(1), standard error(2). These files are always created whenever a
program is run. By default, an error stream is displayed on the screen.

Examples:

1. In the below-mentioned example, the file descriptor used above is 2(STDERR). Using
“2>” re-directs the error output to a file named “error.txt” and nothing is displayed
on STDOUT.

$ somerandomcommand 2>error.txt

15 | P a g e
2. Here, 2>&1 means that STDERR redirects to the target of STDOUT. More
formally, the error message generated by “2” gets merged with the current output “1“.

$ ls GEEK GFG > error.txt 2>&1

In the above example, the directory GEEK is not present. The error output is merged with
the standard output which in turn is being re-directed to “error.txt“.

Pipes and Filters in Linux/Unix

Pipes in UNIX

The novel idea of Pipes was introduced by M.D Mcllroy in June 1972– version 2, 10 UNIX
installations. Piping is used to give the output of one command (written on LHS) as input
to another command (written on RHS). Commands are piped together using vertical bar
“ | ” symbol. Syntax:

command 1|command 2

16 | P a g e
Example:

• Input: ls|more

• Output: more command takes input from ls command and appends it to the
standard output. It displays as many files that fit on the screen and
highlighted more at the bottom of the screen. To see the next screen hit enter or
space bar to move one line at a time or one screen at a time respectively.

Filters in UNIX

In UNIX/Linux, filters are the set of commands that take input from standard input stream
i.e. stdin, perform some operations and write output to standard output stream i.e. stdout.
The stdin and stdout can be managed as per preferences using redirection and pipes.
Common filter commands are: grep, more, sort.

1. grep Command: It is a pattern or expression matching command. It searches for a


pattern or regular expression that matches in files or directories and then prints found
matches.

Syntax:

$grep[options] "pattern to be matched" filename

Example:

Input : $grep 'hello' ist_file.txt

Output : searches hello in the ist_file.txt and outputs/returns the lines containing 'hello'.

17 | P a g e
Options in grep command are: Grep command can also be used with meta-
characters:

18 | P a g e
Example:

Input : $grep 'hello' *

Output : it searches for hello in all the files and directories.

* is a meta-character and returns matching 0 or more preceding characters

2. sort Command: It is a data manipulation command that sorts or merges lines in a


file by specified fields. In other words it sorts lines of text alphabetically or
numerically, default sorting is alphabetical.

Syntax:

$sort[options] filename

The options include

19 | P a g e
3. more Command: It is used to customize the displaying contents of file. It displays the
text file contents on the terminal with paging controls. Following key controls are used:

• To display next line, press the enter key

• To bring up next screen, press spacebar

• To move to the next file, press n

• To quit, press q.

Syntax:

$more[options] filename

Example:

cat fruits.txt | more

20 | P a g e
While using more command, the bottom of the screen contains more prompt where
commands are entered to move through the text.

Split Command in Linux with Examples

Split command in Linux is used to split large files into smaller files. It splits the files into
1000 lines per file(by default) and even allows users to change the number of lines as per
requirement.

The names of the files are PREFIXaa, PREFIXab, PREFIXac, and so on. By default, the
PREFIX of files name is x and the default size of each split file is 1000 lines per file and both
the parameters can be changed with ease. It is generally used with log and archive files as
they are very large and have a lot of lines, so in order to break them into small files for
analysis split command is used.

Syntax:

split [options] name_of_file prefix_for_new_files

Working with Split Command

1. Split file into short files . Assume a file name with name index.txt. Use below split
command to break it into pieces.

split index.txt

21 | P a g e
2. Split file based on number of lines.

split -l 4 index.txt split_file

Index.txt file is split into short files based on the number of lines which we want using -l
option as shown.

3. Split command with verbose option. We can also run split command in verbose
mode by using ‘–verbose’. It will give a diagnostic message each time a new split file is
created.

22 | P a g e
split index.txt -l 4 –verbose

Here, we have created a file with name index.txt which will be split into short files and
verbose will give us the details of what are the tasks performed.

Note: Here -l 4 is not necessary to use. It is used just for understanding purposes.

4. Split file size using ‘-b’ option.

split -b 16 index.txt index

Here, it will split the file index.txt into separate files called indexaa, indexab, …..with each
file containing 16 bytes of data in it.

23 | P a g e
5. Change in suffix length. By default, the suffix length is 2. We can also change it
using ‘-a’ option.

split -l 4 -a 4 index.txt

In this it has suffix length 4 on the split files.

Note: Here -l 4 is not necessary to use. It is used just for understanding purposes.

6. Split files created with numeric suffix. In general, the output has a format of
x** where ** are alphabets. We can change the split files suffix to numeric by using the ‘-d’
option.

split -l 4 -d index.txt

24 | P a g e
Note: Here -l 4 is not necessary to use. It is used just for understanding purposes.

7. Create n chunks output files. If we want to split a file into three chunk output
files then use the ‘-n’ option with the split command which limits the number of split
output files.

split -n 3 index.txt

25 | P a g e
It will create three chunks of split files.

8. Split file with customize suffix. With this command, we can create split output
files with customizing suffix. Assume, if we want to create split output files
with index suffix, execute the following command.

split -l 4 index.txt split_index_

Split output files with index suffix will be created.

Note: Here -l 4 is not necessary to use. It is used just for understanding purposes.

9. Avoid zero-sized split files. There are situations when we split a small file into a
large number of chunk files and this may lead to zero size split output files. They do not
add any value so to avoid it we use the option ‘-e’.

split -l 4 -e index.txt

26 | P a g e
By using this no zero size split output files will be created.

Note: Here -l 4 is not necessary to use. It is used just for understanding purposes.

10. Split the file into two files of equal length. To split a file equally into two
files, we use the ‘-n’ option. By specifying ‘-n 2’ the file is split equally into two files.

split -n 2 index.txt

27 | P a g e
Linux Groups

Users can be listed in different groups. Group allow us to set permission on the group level
instead of setting the permission on individual level.

Every Linux distribution have a graphical tool to manage groups. Groups can be managed by
graphical tools, command line tools and by vi or vigr depending upon the user's experience.
Only experienced users should use vi or vigr to manage groups, since it will do proper
locking or changes in the file.

groupadd

The groupadd command creates or add a group in our system.

Syntax:

groupadd <groupName>

Example:

groupadd php
groupadd java
groupadd android
groupadd spring

In the above screenshot, groups php, java, android and spring are created with groupadd
command.

Group File

The /etc/group file defines the group membership. A user can be a member of more than
one group.

28 | P a g e
Syntax:

/etc/group

Look at the above snapshot, first column indicates group name, second is the group's
encrypted password which may remain empty also, third is group identification (GID) and
fourth is the list of members. Fourth list is empty as these groups do not have members.

Groups

The group command tells about the group where current user belongs to.

Syntax:

groups

Look at the above snapshot, user jtp and sssit belongs to the different groups.

29 | P a g e
usermod

The group members can be edited with usermod or useradd command. If a group is not
listed then by default, usermod command will remove the user from every group of which
he is a member. Here, -a (append) option is used to prevent this from happening.

Syntax:

usermod -a -G <group> <userName>

Example:

usermod -a -G php akki


usermod -a -G php abc
usermod -a -G java jtp

Look at the above snapshot, we have displayed the list of /etc/group. User akki and abc are
added into the group php, user jtp is added into java.

groupmod

With the help of groupmod command you can change the name of an already existing group.

Syntax:

groupmod -n <oldGroup> <newGroup>

Example:

groupmod -n sql spring

30 | P a g e
Look at the above snapshot, group spring is changed into sql.

groupdel

The command groupdel will delete a group permanently from the system.

Syntax:

groupdel <group>

Example:

groupdel sql

Look at the above snapshot, group sql is deleted from the system.

gpasswd

Control of group membership can be passed on to another user with gpasswd command.

Syntax:

31 | P a g e
gpsswd -A <user> <group>

Example:

gpasswd -A jtp java

Look at the above snapshot, we have passed the membership of java to the user with
command "gpasswd -A jtp java". Then we su to jtp and add aaa to java.

Group administrators need not to be a member of the group. They can add or remove a
member without being a member of that group.

File /etc/gshadow keeps the information about the group administrators as shown in below
snapshot.

To remove all the administrators from a group, set an empty administrator list.

Syntax:

32 | P a g e
gpasswd -A "" <group>

Example:

gpasswd -A "" java

Look at the above snapshot, administrator jtp is removed from the group java.

Directory stack manipulation

33 | P a g e

You might also like









ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: https://www.scribd.com/document/662108586/LINUX-UNIT-III

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy