Linux Unit- 2
Linux Unit- 2
The shell in Linux is a powerful and essential tool that allows users to interact with the operating system.
It acts as a command interpreter, enabling users to run commands, manage files, automate tasks, and
configure the system. Below is a detailed explanation of each aspect of shells and their utilities.
o It takes user input, interprets it, and communicates with the kernel to execute the
requested operation.
2. Command Execution:
o Shells are responsible for parsing and executing commands entered by the user.
o They support built-in commands (like cd or echo) and external commands (like ls or
grep).
3. Scripting:
o Shells allow users to write scripts—text files containing sequences of commands—to
automate repetitive tasks.
o Scripts are widely used for system administration, task scheduling, and software
management.
4. Environment Management:
o The shell manages environment variables, such as PATH, HOME, and USER.
o These variables store configuration details about the system and the user environment.
5. Job Control:
o Shells let users manage multiple processes (jobs) by pausing, resuming, or terminating
them as needed.
o Often used for scripting because of its portability across Unix systems.
o Offers command-line editing, improved scripting syntax, and extensive features for
interactive use.
3. C Shell (csh):
o Designed for users familiar with the C programming language.
o Includes advanced features like history substitution, aliasing, and job control.
5. Z Shell (zsh):
o Highly customizable with support for plugins and themes.
6. Dash:
o Lightweight and fast, commonly used for system scripts.
Shell Configuration
Shells use configuration files to define the environment and behavior during initialization.
Examples:
Examples:
~/.bashrc: Loaded for non-login interactive shells, often used for aliases
and functions.
2. Aliases:
o Aliases provide shortcuts for frequently used commands, improving efficiency.
o Example:
3. Filename Expansion:
*: Matches any number of characters (e.g., *.txt matches all .txt files).
4. Redirection:
o Redirect data streams using operators:
o Example:
Pipes
Pipes are used to pass the output of one command as the input to another.
Example:
ls -l | grep ".txt"
o In this example:
Managing Jobs
1. Foreground and Background Jobs:
o Jobs are processes started by the shell.
o By default, processes run in the foreground, but they can be moved to the background
using &.
o Example:
o Use bg to resume the job in the background or fg to bring it back to the foreground.
Shell Scripting
Shell scripting is a powerful way to automate tasks in Linux or UNIX systems. It allows the use of
commands, variables, and control structures to perform complex operations.
1. Types of Statements in Shell Script
In shell scripting, you use different types of statements to perform various actions. These statements can
be broadly categorized into:
Assignment Statements: These are used to assign a value to a variable. In shell scripts,
variables do not need to be declared before use, and their data type is inferred by the value they
hold. The assignment operator is =, but there should be no spaces around it.
Example:
name="John"
age=30
Here, the variable name is assigned the string "John", and the variable age is assigned the value 30.
Control Flow Statements: These statements control the flow of the script. Control flow
allows the script to make decisions or repeat actions.
o if Statement: The if statement checks whether a condition is true and executes a block
of code if it is true. You can also use else to specify what to do if the condition is false.
Example:
else
fi
Here, if the value of age is greater than or equal to 18, it prints "You are an adult.", otherwise it prints
"You are a minor.".
o for Loop: The for loop iterates over a list of values, executing the commands within its
body for each value in the list. Example:
for i in 1 2 3 4 5
do
done
o while Loop: The while loop repeatedly executes a block of code as long as a specified
condition remains true. Example:
counter=1
do
done
This loop continues as long as the value of counter is less than or equal to 5, printing the current value of
counter and then incrementing it.
o case Statement: The case statement is used for pattern matching, where the script will
execute a particular block of code based on the value of a variable. Example:
case $num in
esac
In this example, the num variable is checked against different cases, and the appropriate message is
printed.
Assigning Values to Variables: You can assign a value to a variable simply by using the =
operator, with no spaces around it. After assignment, you can use the variable by referencing it
with a $ sign.
Example:
greeting="Hello, World!"
Default Shell Variables: Shell scripts come with some predefined variables that provide
information about the environment and execution. For example:
o $0: Name of the script.
Example:
Naming Variables:
o Variable names must start with a letter or an underscore (_), followed by letters,
numbers, or underscores.
Example:
myVar="Shell Script"
Using read Command: The read command waits for the user to input data and stores the
input into a variable.
Example:
read name
if and else Statements: These are used to make decisions based on a condition. The if block is
executed if the condition is true, and the else block runs if the condition is false.
Example:
else
fi
In this example:
o If false, the else block runs and prints "You are a minor."
for Loop: The for loop is used to iterate over a list of values and execute a block of code for
each value in the list.
Example:
for i in 1 2 3 4 5
do
done
This script will print the numbers 1 through 5, each on a new line.
while Loop: The while loop repeatedly executes a block of code as long as a condition is true.
Example:
counter=1
do
echo "Counter is $counter"
done
In this example, the loop will continue to run as long as the value of counter is less than or equal to 5,
printing the current value of counter and then incrementing it by 1 after each loop iteration.
case Statement: The case statement allows you to match a variable against several possible
values and execute the corresponding block of code for the matched value.
Example:
case $choice in
esac
This script checks the value of $choice and executes the corresponding code block based on the matched
pattern.
Summary
Assignment and Variables: Shell scripting allows you to define variables and use them
throughout the script. You can store data in variables and later manipulate or display that data.
Control Statements: Shell scripts use control structures like if, for, while, and case to control the
flow based on conditions or to iterate over a series of values.
User Input: Shell scripts can ask for user input using the read command, allowing you to interact
with users and personalize the script behavior based on their responses.