0% found this document useful (0 votes)
0 views

Lab_assignment_3[1]

The document contains a series of exercises focused on writing shell scripts, covering basic commands, variable usage, conditional statements, loops, and array manipulation. Each exercise provides a specific task, such as printing messages, checking file existence, and performing mathematical calculations. The document serves as a practical guide for learning shell scripting through hands-on examples.

Uploaded by

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

Lab_assignment_3[1]

The document contains a series of exercises focused on writing shell scripts, covering basic commands, variable usage, conditional statements, loops, and array manipulation. Each exercise provides a specific task, such as printing messages, checking file existence, and performing mathematical calculations. The document serves as a practical guide for learning shell scripting through hands-on examples.

Uploaded by

mr.robot2622
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Exercise_1 - Write a shell script that prints “Shell Scripting is FUN!” on the screen.

#!/bin/bash

echo “Shell Scripting is Fun!”

Exercise_2 - Modify the shell script from exercise 1 to include a variable. The variable will hold the
contents of the message “Shell Scripting is Fun!”

#!/bin/bash

NAME=”Shell Scripting is Fun!”


echo $NAME

Exercise_3 - Store the output of the command “hostname” & current date time in a variable.
Display “This script is running on _ & _.” where “_” is the output of the “hostname” command &
system date time.

#!/bin/bash

HOSTNAME=$(hostname)

now=$(date)

echo “This script is running on: $HOSTNAME and current date and time : $now”

echo “ “

https://www.shellscript.sh/loops.html

Exercise_4- Display the output as “Hello Your_name, you are ---years old.” Using READ command.

#!/bin/bash

echo "Enter your name:"

read name

echo "Enter your age:"

read age

echo "Hello $name, you are $age years old."

Exercise_5- write code to check whether file exist in current directory or not using If ELSE condition.

#!/bin/bash

echo "Enter filename:"

read file

if [ -f "$file" ]; then

echo "File exists."

else
echo "File does not exist."

fi

Exercise_6- Write code using CASE condition to print whether the number given by user as input is
ODD or EVEN.

#!/bin/bash

echo "Enter a number:"

read num

case $((num % 2)) in

0) echo "Even";;

1) echo "Odd";;

esac

Exercise_7- Display the output as shown and perform necessary action of Creating & Deleting the
file.

#!/bin/bash

echo "1. Create File"

echo "2. Delete File"

echo "3. Exit"

echo "Enter your choice:"

read ch

case $ch in

1) echo "Enter filename:"; read f; touch $f;;

2) echo "Enter filename:"; read f; rm -i $f;;

3) exit;;

esac

Exercise_8- using the file created in Exercise_7 find out the wordcount.

#!/bin/bash

echo "Enter filename:"

read file

wc $file
Exercise_9- Write code to find the word from the file.

#!/bin/bash

echo "Enter filename:"

read file

echo "Enter word to search:"

read word

grep "$word" $file

Exercise_10- using FOR LOOP concept write script to find the

a. Factorial of a number
b. Fibonacci series

Solution a:

#!/bin/bash

echo "Enter a number:"

read num

fact=1

for (( i=1; i<=num; i++ ))

do

fact=$((fact * i))

done

echo "Factorial of $num is: $fact"

Solution b:

#!/bin/bash

echo "Enter the number of terms:"

read n

a=0

b=1

echo "Fibonacci Series up to $n terms:"

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

do

echo -n "$a "

fn=$((a + b))
a=$b

b=$fn

done

Exercise_11 - Create a Bash script that:

Stores a list of fruits using an indexed array.

Adds more fruits to the list.

Displays all fruits with their index.

Stores user profile info (name, age, country) in an associative array.

Prints the profile info.

#!/bin/bash

# Step 1: Declare and initialize an indexed array

fruits=("apple" "banana" "cherry")

# Step 2: Append more fruits

fruits+=("orange" "grape")

# Step 3: Print all fruits with their index using printf (no loop)

echo "List of Fruits:"

printf " %s\n" "${fruits[@]}"

# Divider

echo "----------------------------"

# Step 4: Declare and assign associative array

declare -A userProfile

userProfile[name]="Prashant"

userProfile[age]=28

userProfile[country]="India"

# Step 5: Print associative array using printf (no loop)

echo "User Profile:"

printf "%s: %s\n" "${!userProfile[@]}" "${userProfile[@]}"


Exercise_12 - Create a Bash script that:

Declare a string.

Print the entire string

Print the length of the string.

Extract the first 4 characters.

Extract characters from index 4 to 12.

Replace a part of the string

Convert to lowercase and uppercase

Prompt user to enter a string and slice the first 3 characters

#!/bin/bash

# Step 1: Declare a string

myStr="BashScriptingIsFun"

# Step 2: Print the entire string

echo "Original String: $myStr"

# Step 3: Print the length of the string

echo "Length of the string: ${#myStr}"

# Step 4: Extract the first 4 characters

echo "First 4 characters: ${myStr:0:4}"

# Step 5: Extract characters from index 4 to 12 (9 characters)

echo "Characters 5 to 13: ${myStr:4:9}"

# Step 6: Replace 'Is' with 'CanBe'

newStr=${myStr/Is/CanBe}

echo "After replacement: $newStr"


# Step 7: Convert to lowercase and uppercase

echo "Lowercase: ${myStr,,}"

echo "Uppercase: ${myStr^^}"

# Step 8: Prompt user to enter a string and slice the first 3 characters

read -p "Enter a word: " userInput

sliced=${userInput:0:3}

echo "First 3 characters of your input: $sliced"

//# Step 9: Check if a substring exists

//if [[ "$myStr" == *"Script"* ]]; then

// echo "Substring 'Script' found in '$myStr'"

//else

// echo "Substring 'Script' not found in '$myStr'"

//fi

# Bonus Challenge: Full Name input

read -p "Enter your full name (First Last): " fullName

# Extract first and last name using cut

firstName=$(echo $fullName | cut -d' ' -f1)

lastName=$(echo $fullName | cut -d' ' -f2)

# Get initials

initials="${firstName:0:1}${lastName:0:1}"

echo "First Name: $firstName"

echo "Last Name: $lastName"

echo "Initials: $initials"

You might also like