Unit 3 Part-1
Unit 3 Part-1
UNIT - III
1) Shell
The shell is a command interpreter. It is the layer between the operating system and kernel and
the user. The role of a shell is to interact with a system by entering command, executing
programs, and performing various tasks (file manipulation, process management, and system
administration).
3) Shell as an interface
The shell is a command line interface, it allows users to interact with the operating system. It
interprets and executes commands, it acts as an intermediary between the user and the underlying
system's kernel.
The shell provides two main modes for interaction:
Interactive Mode:
In interactive mode the shell reads and executes commands from the user one at a time,
providing immediate feedback. This is typically used when you open a terminal and interact
directly with the shell, such as typing commands.
Non-interactive Mode (Shell Scripting):
In non-interactive mode, commands are written into a file (called a script), and the shell executes
them without user interaction. It is useful for automating tasks, running scheduled jobs, or
managing large-scale operations that require the execution of multiple commands.
4) Shell features
The shell offers a variety of powerful features that make interacting with the operating system
efficient and flexible. Here are some of the key features:
Command Execution:
The primary function of the shell is to allow users to execute commands. These can be simple
tasks like listing files, creating directories, or more complex tasks like running programs.
Scripting:
Shell scripting refers to writing a series of commands in a file (called a script), which can be
executed as a single unit. Scripts automate repetitive tasks and make system management easier.
Input/Output Redirection:
Input/output (I/O) redirection allows users to control where the shell reads input from or sends
output to. Instead of reading input from the keyboard or sending output to the screen, redirection
allows input/output to be handled by files, other commands, or devices.
Pipes (|):
Pipes are used to pass the output of one command as the input to another command. This allows
for chaining commands and processing data in a step-by-step manner.
Arguments: Arguments specify the target or the data on which the command operates. This can
be files, directories, or any other objects the command needs to work with. Where an argument
indicates what the command is to perform its action, usually a file or series of files.
Example:
Syntax: command -[option] [option] [option]
$ls -alR
- will perform a long list on all files in the current directory and recursively perform the list
through all sub-directories.
- for most commands you can separate the options, preceding each with a hyphen
$ls -a -l -R
- Options and syntax for a command are listed in the man page for the command.
6) UNIX METACHARACTERS (SPECIAL CHARACTERS)
● These are also called special characters or wildcard characters.
● Special characters have a special meaning to the shell.
● Meta-characters in Unix are special characters that have a unique meaning.
● They are used to perform various functions such as pattern matching input/output
redirection, and command chaining.
● The purpose of these characters help in making command execution flexible, dynamix
and more powerful by enabling operations such as file expansion, command substitution,
and automation.
● The most commonly used metacharacters in UNIX
a) Asterisk ( * ) : The * ( asterisk) meta character is used to match any and all characters.
Example:
- List all files in the working directory that begin with the letter s regardless of what
characters comes after it
- The * (asterisk) meta character can be used anywhere in the filename.
- It does not necessarily have to be the last character.
$ ls *.txt
list all files having .txt
$ ls file*
lists all files that start with the name file
$ ls *file
list all files that end with the name file
b) Question mark ( ? ): The question mark meta character is used to match a single
character in a file name.
Example:
- List all files in the working directory that has a single character, or two characters
or three characters
$ls ?
$ls ??
$ls ???
- List all files in the working directory that has three characters starting with k.
$ls k??
- Like the asterisk, the ? (question mark) meta character can be used as a substitution for
any character in the filename.
$ ls file?.txt
- list files like file1.txt, fileA.txt, but not file10.txt
$ ls file?
- lists all files that have one character after file
$ ls ??file
- list all files that have two characters before file
c) Brackets [ ] :
- Brackets ( [....] ) are used to match a set of specified characters.
- A comma separates each character within the set.
Example:
List all files beginning with “ a “, “ b “, or “ c “.
$ ls [a,b,c]*
- It is also represented as,
$ ls [abc]* # without giving comma separator
- list all files that have a single digit after ‘file’
$ ls file [0-9]
- list all files that have any one letter before ‘file’
$ ls [a-z]file
- [^ ] matches any character except those inside the brackets. List all files that have digits
after file’. file1, file2, file3, file33
$ ls file [^0-9]
d) Hyphen - :
- Using the - (hyphen) metacharacter with [ ] (brackets) is used to match a specified range
of characters.
Example:
- List all files beginning with a lowercase letter of the alphabet
$ ls [a-z]*
- If there are directory names in the specified range. their name and contents will
also be displayed.
e) Pipe ( | ) : The pipe character takes the output of one command and uses it as the input
for another command.
$ ls -l | grep "Oct"
This lists files with details and pipes the result to grep, which searches for the string "Oct".
f) && (Logical AND) : Executes the second command only if the first command succeeds
(returns 0).
$ mkdir new_folder && cd new_folder
If mkdir new_folder succeeds, then cd new_folder is executed.
g) || (Logical OR) : Executes the second command only if the first command fails (non-zero
exit status).
$ mkdir existing_folder || echo "Folder exists"
If mkdir existing_folder fails, the message "Folder exists" is printed.
h) ; (Command Separator) : Allows you to run multiple commands sequentially,
regardless of whether the previous command succeeds or fails.
$ echo "First command"; echo "Second command"
Both commands will be executed one after the other.
i) ' ' (Single Quotes) : Encloses a string literally, preventing any expansion or interpretation
of special characters within it.
$ echo 'This is $100'
The output will be This is $100.
j) " " (Double Quotes) : Encloses a string but allows variable and command substitution.
$ name="John" $ echo "Hello, $name"
The output will be Hello, John.
k) \\ (Backslash) : Used to escape special characters, treating them literally.
$ echo "This is a dollar sign: \$"
The output will be This is a dollar sign: $.
l) & (Background Process) : Sends a command to run in the background, freeing the
terminal for further input.
$ long_running_command &
Special Meta-Characters for Process Substitution
m) $() (Substitution) : Command substitution replaces a command with its output.
$ echo "Current time: $(date)"
n) $? (Exit Status) : Retrieves the exit status of the last executed command.
$ echo $?
Standard streams: UNIX defines three standard streams (files) that are used by
commands. Stream - is simply a sequence of byte
● Standard input : The stream representing input, which is connected to the
keyboard.
● Standard output : The stream representing output, which is connected to the
display.
● Standard error : The stream representing the error message that produced from
shell,. this is also connected to display.
Unix assigns a file descriptor to each stream
● Standard input STDIN 0
● Standard output STDOUT 1
● Standard error STDERR 2
- Unix treats everything as a file, Regular file, Directory and even Device files.
- Whenever we execute a program/command at the terminal, 3 files will open (std input,
std output, std error)
- Redirection - changing the standard input / output devices while executing a command.
- It is possible to change the source from where the input is taken by a program as well as
the destination to where the output is sent by a program.
- This mechanism of changing the input source and/or destination is called redirection.
- The UNIX redirection operators are
● Standard output redirection ( > )
● Standard output redirection with appending (>>)
● Standard input redirection ( < )
● Error redirection ( > )
- The input source is redirected using the < (less than) operator.
- The output destination is redirected using the > (greater than) or >> (double greater than)
operators.
- The file descriptors 0 and 1 are implicitly prefixed to the redirection operators < and >
respectively by the shell.
- The output of a program can be redirected using either > or >> operator.
- when > is used, the destination files are overwritten.
- when >> is used, the present output will be appended to an existing file.
- In either case if the destination file does not exist, it is created.
It allows you to send the output of a command to a file, overwriting its contents if the file already
exists.
Example: If you want to save the output of the ls command to a file named list.txt:
ls > list.txt
This will write the list of files in the current directory to list.txt. If list.txt already
exists, its content will be replaced by the new output.
This will add the text "hello world" to the end of output.txt. If the file doesn't exist, it will
be created.
Example:
If you have a file input.txt containing some text, and you want to count the number of
words in the file using the wc command:
wc -w < input.txt
wc -w command counts the words in input.txt by reading from it instead of waiting for
keyboard input.
Unix allows you to redirect error messages (stderr) to a file. By default, errors are displayed on
the terminal but with error redirection the error can be stored in a file.
Example: If you try to list a non-existent directory and want to capture the error in a file
error.txt:
ls non_existent_directory 2> error.txt
Here 2 stands for standard error and any error message (e.g., "No such file or directory") will be
saved in error.txt instead of being printed on the terminal.
Example (1) :
$pwd
/home/501
$ echo ‘pwd’ > sample #Redirecting pwd command to a file called sample
$cat sample
pwd
$sh < sample #Executing sample fie like pwd command using sh command
/home/501
Example (2) : (As an internal command)
- To count number of users with pipeline frequently, the command used is
$ who | wc -l
3
- An ordinary file can be created to contain ‘who | wc -l’
$echo ‘who | wc -l’ > sample2
- The program input can be redirected to a file rather than the terminal.
$ who
501 dev/pts/0 jul 13 10:20
502 dev/pts/1 jul 13 10:30
503 dev/pts/2 jul 13 10:40
$ cat sample2
$who | wc -l
$sh < sample2
3
- The output is the same as the initial command.
Example (3) : (As an external command)
- To count number of users with pipeline frequently, the command used is
$ who | wc -l
3
- an ordinary file can be created to contain ‘who | wc -l’
$ echo ‘who | wc -l’ > sample2
- the program input can be redirected to a file rather than the terminal
$ who
501 dev/pts/0 jul 13 10:20
502 dev/pts/1 jul 13 10:30
503 dev/pts/2 jul 13 10:40
$ cat sample2
$who | wc -l
$cd bin
bin] $sample2
permission denied #Because there is no execution permission to the User/Owner
bin] $chmod u+x sample2 #Giving execute permission to user using chmod command
- The shell programs will interpret the arguments such as filenames and options while
running the program.
- command line arguments ( also known as positional parameters ) are the arguments
specified at the command prompt with a command or script to be executed.
- The locations at the command prompt of the arguments as well as the location of the
command, or the script itself are stored in corresponding variables. These variables are
special shell variables.
Variable Description