0% found this document useful (0 votes)
13 views30 pages

Shell Scripting Basics

The document provides an overview of Unix and shell scripting, explaining the roles of the kernel and shell, as well as the structure of commands and files. It introduces shell scripting concepts, including syntax, variables, control loops, and practical examples for automating tasks. The document emphasizes the importance of mastering these elements for effective shell scripting.

Uploaded by

elsa6.6.2023
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views30 pages

Shell Scripting Basics

The document provides an overview of Unix and shell scripting, explaining the roles of the kernel and shell, as well as the structure of commands and files. It introduces shell scripting concepts, including syntax, variables, control loops, and practical examples for automating tasks. The document emphasizes the importance of mastering these elements for effective shell scripting.

Uploaded by

elsa6.6.2023
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Shell Scripting

CSLR42 OS Lab 2
What is Unix?

• A set of programs that act as a link between the computer and the user.
• The computer programs that allocate the system resources and coordinate
all the details of the computer's internals is called the operating system or
the kernel.
• Users communicate with the kernel through a program known as the shell.
• The shell is a command line interpreter; it translates commands entered by
the user and converts them into a language that is understood by the kernel.
Main Concepts
• Kernel: The kernel is the heart of the operating system. It interacts with the hardware and most of
the tasks like memory management, task scheduling and file management.

• Shell: The shell is the utility that processes your requests. When you type in a command at your
terminal, the shell interprets the command and calls the program that you want. The shell uses
standard syntax for all commands. C Shell, Bourne Shell and Korn Shell are the most famous
shells which are available with most of the Unix variants.

• Commands and Utilities: There are various commands and utilities which you can make use of in
your day to day activities. cp, mv, cat and grep, etc. are few examples of commands and utilities.
There are over 250 standard commands plus numerous others. All the commands come along
with various options.

• Files and Directories: All the data of Unix is organized into files. All files are then organized into
directories. These directories are further organized into a tree-like structure called the filesystem.
Types of Shells in Unix
In Unix, there are two major shells :

• Bourne shell — If you are using a Bourne-type shell, the $ character is the
default prompt.
• C shell — If you are using a C-type shell, the % character is the default
prompt.
• The Bourne Shell has the following subcategories –
 Bourne shell (sh)
 Korn shell (ksh)
 Bourne Again shell (bash)
 POSIX shell (sh)
Types of Shells in Unix contd…

• The different C-type shells follow –


C shell (csh)
TENEX/TOPS C shell (tcsh)

• Bourne shell was the first shell to appear on Unix systems, thus it is referred
to as "the shell".

• Bourne shell is usually installed as /bin/sh on most versions of Unix.

• For this reason, it is the shell of choice for writing scripts that can be used
on different versions of Unix.
What is a terminal?

• A program which is responsible for providing


an interface to a user so that he/she can
access the shell.
• It basically allows users to enter commands
and see the output of those commands in a
text-based interface.
Introduction to Shell Scripting
Usually, shells are interactive, which means they accept commands as input from users and execute
them.
However, sometimes we want to execute a bunch of commands routinely, so we have to type in all
commands each time in the terminal.

Each shell script is saved with `.sh` file extension e.g., myscript.sh.

• A shell script has syntax just like any other programming language.
• A shell script comprises the following elements –
• Shell Keywords – if, else, break etc.
• Shell commands – cd, ls, echo, pwd, touch etc.
• Functions
• Control flow – if..then..else, case and shell loops etc.
Shell Scripts
• Example Script Assume we create a test.sh script.
• Note all the scripts would have the .sh extension.
• Before you add anything else to your script, you need to alert the system that a shell script is
being started.
• This is done using the shebang construct.
• For example − #!/bin/sh
• This tells the system that the commands that follow are to be executed by the Bourne shell.
• It's called a shebang because the # symbol is called a hash, and the ! symbol is called a bang.
Advantages of Shell Scripting
• Automates tasks on Unix/Linux systems.
Contains commands executed by the shell.
• - Helps manage system processes.
• - Boosts productivity.
Basic Shell Commands

| Command | Description |
|----------|-----------------------------|
| pwd | Print working directory |
| ls | List files and directories |
| cd | Change directory |

| echo | Print text or variables |


Writing a Shell Script
Steps to Create and Run a Shell Script:
• 1. Create the file: `nano Hello.sh`
• 2. Add the shebang line: `#!/bin/bash`
• 3. Write your script: `echo "Hello, World!"`
• Esc :wq for saving
• 4. Make it executable: `chmod +x Hello.sh`
• 5. Run the script: `./Hello.sh`
Variables and Control Loops in
Shell Scripting
Variables in Shell Scripting

• Definition: Variables store data that can be


accessed and manipulated in scripts.
• Syntax:
• variable_name="value"
Accessing Variables

• To access the value of a variable, prepend it


with the $ symbol:
• echo $variable_name
Examples of Variables

• # Defining variables
• name="John"
• age=25

• # Accessing and using variables


• echo "My name is $name"
• echo "I am $age years old"
Reading Input into Variables

• Using the `read` command to capture user


input into a variable:
• echo "Enter your name:"
• read name
• echo "Hello, $name!"
Variable Types

• String Variables: Store text values


• Numeric Variables: Store numeric values
(Arithmetic operations use `expr` or `$(( ))`)
Arithmetic Operations Example

• a=10
• b=5
• result=$((a + b))
• echo $result
Unsetting Variables

• Removing a variable:
• unset variable_name
Relational Operators
Control Loops in Shell Script

• Loops: Allow repetition of commands.


For Loop Example

• # Loop through a sequence of numbers


for i in {1..5}
do
echo "Number $i"
done
While Loop Example

• # Loop while a condition is true


count=1
while [ $count -le 5 ]
do
echo "Count is $count"
((count++))
done
If-Else Example

• # Using if-else for conditional execution


if [ $age -ge 18 ]
then
echo "You are an adult."
else
echo "You are a minor."
fi
Arrays
Declaring Arrays:

arr=("apple" "banana" "cherry")


echo ${arr[0]} # Output: apple
echo ${arr[@]} # All elements
Looping through Arrays:
#!/bin/bash
arr=("dog" "cat" "mouse")
for item in "${arr[@]}"; do
echo $item
done
Practical Examples
File Operations:
#!/bin/bash
if [ -e myfile.txt ]; then
echo "File exists"
else
echo "File does not exist"
fi
Summary
- Shell scripting is a powerful tool for
automation.
- Master variables, control statements, loops,
and arrays.
- Combine commands for complex workflows.
- Practice by creating small scripts.

You might also like