0% found this document useful (0 votes)
2 views7 pages

Shelll

Uploaded by

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

Shelll

Uploaded by

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

What is Shell Scripting?

Shell Scripting is a way of writing multiple Linux commands in a file so they can be executed together
automatically.

Instead of typing commands one by one, we save them in a file and run it.

Example:

 Creating folders

 Copying files

 Taking backups

 Automating daily tasks

What is a Shell?

A Shell is a program that acts as a bridge between the user and the Linux operating system.

Flow:

User → Shell → Kernel → Hardware

Common shells:

 bash (most commonly used)

 sh

 zsh

In this course, we use bash shell.

First Linux Commands (Revision)

pwd # shows current directory

ls # lists files and folders

date # shows current date and time

whoami # shows current user

These commands are executed by the shell.

What is a Shell Script?

A Shell Script is a text file containing Linux commands.

 File extension: .sh


 Executed using bash shell

First Shell Script (Example 1)

Step 1: Create a file

nano [Link]

Step 2: Write the script

#!/bin/bash

echo "Hello Students"

date

pwd

Step 3: Give execute permission

chmod +x [Link]

Step 4: Run the script

./[Link]

Explanation of Script Lines

 #!/bin/bash → tells the system which shell to use

 echo → prints output

 date, pwd → normal Linux commands

Variables in Shell Script

A variable stores data.

Example 2: Variable Usage

#!/bin/bash

name="Sairam"

echo "My name is $name"

Rules:

 No space around =

 Use $ to access variable value


Taking Input from User

Example 3: User Input

#!/bin/bash

echo "Enter your name:"

read uname

echo "Welcome $uname"

read → takes input from user

If-Else Statement

Used for decision making.

Example 4: Check Voting Eligibility

#!/bin/bash

echo "Enter age:"

read age

if [ $age -ge 18 ]

then

echo "You are eligible to vote"

else

echo "You are not eligible"

fi

Loops in Shell Scripting

Loops are used to repeat tasks.

Example 5: For Loop

#!/bin/bash

for i in 1 2 3 4 5

do

echo "Number: $i"

done
While Loop Example

#!/bin/bash

count=1

while [ $count -le 5 ]

do

echo "Count: $count"

count=$((count+1))

done

Functions in Shell Script

Functions help reuse code.

Example 6: Function

#!/bin/bash

sayHello() {

echo "Hello from function"

sayHello

Real-Time Example: Backup Script

#!/bin/bash

mkdir backup

cp *.txt backup/

echo "Backup completed"

Advantages of Shell Scripting

 Saves time

 Reduces manual work

 Easy automation

 Widely used in DevOps & Linux administration


Summary

 Shell scripting automates Linux commands

 Bash is the most used shell

 Scripts contain commands, variables, loops, and conditions

Problems:

1. Write a shell script program of Arithmetic operations -using expr


Answer:
#!/bin/bash
read a b

add=`expr $a + $b`
sub=`expr $a - $b`
mult=`expr $a \* $b`
div=`expr $a / $b`

echo "$add"
echo "$sub"
echo "$mult"
echo "$div"

[Link] leap year or not

#!/bin/bash

read num

if [ $((num % 4)) -eq 0 ]

then

echo "leap year"

else

echo "not a leap year"

fi

[Link] a number

#!/bin/bash

echo "Enter a number:"

read num
rev=0

while [ $num -gt 0 ]

do

rem=$((num % 10))

rev=$((rev * 10 + rem))

num=$((num / 10))

done

echo "Reversed number = $rev"

4. Fibonacci series

#!/bin/bash

echo "Enter number of terms:"

read n

a=0

b=1

echo "Fibonacci series:"

echo "$a"

echo "$b"

for (( i=3; i<=n; i++ ))

do

c=$((a + b))

echo "$c"

a=$b

b=$c

done

You might also like