Lab_assignment_3[1]
Lab_assignment_3[1]
#!/bin/bash
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
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
read name
read age
Exercise_5- write code to check whether file exist in current directory or not using If ELSE condition.
#!/bin/bash
read file
if [ -f "$file" ]; then
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
read num
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
read ch
case $ch in
3) exit;;
esac
Exercise_8- using the file created in Exercise_7 find out the wordcount.
#!/bin/bash
read file
wc $file
Exercise_9- Write code to find the word from the file.
#!/bin/bash
read file
read word
a. Factorial of a number
b. Fibonacci series
Solution a:
#!/bin/bash
read num
fact=1
do
fact=$((fact * i))
done
Solution b:
#!/bin/bash
read n
a=0
b=1
do
fn=$((a + b))
a=$b
b=$fn
done
#!/bin/bash
fruits+=("orange" "grape")
# Step 3: Print all fruits with their index using printf (no loop)
# Divider
echo "----------------------------"
declare -A userProfile
userProfile[name]="Prashant"
userProfile[age]=28
userProfile[country]="India"
Declare a string.
#!/bin/bash
myStr="BashScriptingIsFun"
newStr=${myStr/Is/CanBe}
# Step 8: Prompt user to enter a string and slice the first 3 characters
sliced=${userInput:0:3}
//else
//fi
# Get initials
initials="${firstName:0:1}${lastName:0:1}"