Que.
1 Write script, using case statement to perform basic math
operations(+,-,*,/,%).
Ans..
read -p "Enter the Two Numbers:= " num1 num2
#Perform All Arithmetic Operation !!
echo "Addition of Given Number is:= `expr $num1 + $num2`"
echo "Subtraction of Given Number is:= `expr $num1 - $num2`"
echo "Multiplication of Given Number is:= `expr $num1 \* $num2`"
# Handle Divide by Zero Exception !!
if [ $num1 -ne 0 -a $num2 -ne 0 ]; then
echo "Division of Given Number is:= `expr $num1 / $num2`"
echo "Modulos of Given Number is:= `expr $num1 % $num2`"
else
echo "Division and Modulos by Zero Not Allowed !!"
fi
Output..
Que.2 Write a script to display the according to the time like
Page 1 of 17
good morning, good afternoon, good evening and good
night.
Ans..
# Get the current hour in 12-hour format (1-12) with AM/PM
hour=$(date +"%I")
minute=$(date +"%M")
ampm=$(date +"%p")
hour=$(echo "$hour" | sed 's/^0*//')
#Handling Message based on hour and AM/PM
if [ $ampm = "AM" -a $hour -ge 1 ]; then
echo "Good Morning !! $(echo "Time is:= $hour:$minute
$ampm")"
elif [ $ampm = "PM" -a $hour -ge 1 ]; then
echo "Good Afternoon !! $(echo "Time is:= $hour:$minute
$ampm")"
elif [ $ampm = "PM" -a $hour -ge 5 ]; then
echo "Good Evening !! $(echo "Time is:= $hour:$minute
$ampm")"
else
echo "Good Night !! $(echo "Times:= $hour:$minute $ampm")"
fi
Output..
Que.3 Write script to check inputted file is regular file , directory
or does not exist ..
Page 2 of 17
Ans..
read -p "Enter the file path:= " file_path
if [ -z "$file_path" $1 ]; then
echo "File path is Empty! Please Provide Existing path.."
exit $1
fi
if [ -f "$file_path" ]; then
echo "$file_path" is Regular file....
elif [ -d "$file_path" ]; then
echo "$file_path" is a Directory...
else
echo "$file_path" Does not Exist !!!
fi
Output..
Que.4 Write a script which enters username s& password &
check that if the username = sugc & password=98765 then
display the valid user message. Otherwise invalid user.
[script gives maximum 3 attempts to the user.]
Ans..
correct_username="sugc"
correct_pass="98765"
Page 3 of 17
maxAttemps=3
Existing_Attemp=0
while [ $Existing_Attemp -lt $maxAttemps ]; do
read -p "Enter UserName:= " userName
read -s -p "Enter Password:= " password
echo
if [ "$userName" = "$correct_username" -a "$password" =
"$correct_pass" ]; then
echo "Valid user, Welcome!!"
exit 0
else
echo "Invalid User Try Again.."
fi
((Existing_Attemp++))
if [ $Existing_Attemp -eq $maxAttemps ]; then
echo "Maximum Attemps Reached !! Access Denied.."
exit 1
fi
done
Output..
Page 4 of 17
Que.5 Accept a string from terminal and echo suitable message if
it does not have at least 10 characters.
Ans..
#Accept String Input From User !!
read -p "Enter the String:= " str
# Check The String has 10 Characters or not !!
if [ ${#str} -lt 10 ]; then
echo "String Does not have atleast 10 characters.."
else
echo "The String has 10 characters.."
fi
Output..
Que.6 Write a script to delete all vowels from particular string.
Ans..
Page 5 of 17
#Accept string from user !!
read -p "Enter the String:= " str
#Delete All Vowels From String !!
result=$(echo "$str" | tr -d 'aeiouAEIOU')
#Display String Excluding Vowels..
echo "String Without Vowels: $result"
Output..
Que.7 Write a script to accept a number from user until he enters
0 & find sum of all that numbers.
Ans..
# Initialize Sum variable
sum=0
#Loop To repeatedlly accept number until user entered zero..
while true; do
read -p "Ebter the number (Enter 0 to Stop..):= " n
#If Entered Number is Zero Break the loop..
if [ $n -eq 0 ]; then
break
fi
Page 6 of 17
#Addition Of The Entered Numbers..
sum=$((sum + n))
done
#Display The Addition of Numbers..
echo "Sum of the Entered Numbers is:= $sum"
Output..
Que.8 Write an awk script to print each odd line twice and even
line thrice.
Ans..
#!/usr/bin/awk -f
# Check if the line number is odd
NR % 2 == 1 {
# Print the odd line twice
print $0
print $0
}
# Check if the line number is even
Page 7 of 17
NR % 2 == 0 {
# Print the even line thrice
print $0
print $0
print $0
}
Output..
Que.9 Check whether file is empty or not. And if file name
doesn’t exist than print appropriate msg.
Ans..
#Input File name From user..
read -p "Enter the File Name:= " f_name
#Check The File is Exist or Empty..
if [ ! -e "$f_name" ]; then
echo "File Does Not Exist!!"
elif [ ! -s "$f_name" ]; then
echo "File is Empty!!"
else
echo "The File is not Empty!!"
Page 8 of 17
fi
Output..
Que.10 Write a shell script which takes input of file name and
prints first 10 lines of that file. file name is to be passed
as command line argument. If argument is not passed
then any ‘C’ program from the current directory is
to be selected .
Ans..
#!/bin/bash
# Check if a file name is provided as a command-line argument
if [ -z "$1" ]; then
# No argument provided, select any '.c' file from the current directory
c_file=$(ls *.c 2>/dev/null | head -n 1)
# Check if there is a '.c' file in the directory
if [ -z "$c_file" ]; then
echo "No C file found in the current directory."
exit 1
else
echo "No file provided. Using the C file: $c_file"
file="$c_file"
fi
else
# Use the file passed as an argument
file="$1"
fi
# Check if the file exists
if [ ! -f "$file" ]; then
echo "File does not exist."
exit 1
fi
Page 9 of 17
# Print the first 10 lines of the file
echo "Displaying the first 10 lines of $file:"
head -n 10 "$file"
Output..
Que.11 Write a command to replace ‘UIX KERNEL’ on line no
10 to 20.
Ans..
sed -i '10,20s/UIX KERNEL/replacement_text/g' q10.sh
Que.12 Display all blank lines between line 20 and 30 of file
test.txt.
Ans..
sed -n '20,30{/^$/p}' q12.txt
Output..
Que.13 To list file names consist of only 4 digits.
Ans..
Page 10 of 17
Que.14 Display the lines that do not contain “Unix”.
Ans..
grep -v "Unix" q5.sh
Output..
Que.15 Display the lines which are not starting with 2 at the
beginning.
Ans..
grep -v "^2" filename
Output..
Que.16 Display lines beginning either with alphabet or digit from
Page 11 of 17
file test.txt.
Ans..
grep "^[a-zA-Z0-9]" test.txt
Output..
Que.17 Write a command to display all file name containing only
digits in a filename.
Ans..
Que.18 Display two lines starting from 4th line of file test.txt.
Ans..
sed -n '4,5p' test.txt
Output..
Que.19 To display lines beginning with alphabets of a file test.txt.
Ans..
grep "^[a-zA-Z]" test.txt
Output..
Page 12 of 17
Que.20 To count number of words in line 10 thought 20 of file
test.txt.
Ans..
sed -n '10,20p' test.txt | wc -w
Output..
Que.21 Print the even numbered lines in the data file.
Ans..
$ sed -n '2~2p' test.txt
Output.
Que.22 Print the sums of the fields of every line in file f1.
Ans..
awk '{sum=0; for(i=1; i<=NF; i++) sum += $i; print sum}' f1
Output..
Page 13 of 17
Que.23 Display those words whose length greater than 10
characters and consist of digits only.
Ans..
grep -o '\b[0-9]\{11,\}\b' data.txt
Output..
Que.24 Write AWK script to print 10 to 1 using while loop
Ans..
#!/usr/bin/awk -f
{
num = 10
while (num >= 1) {
print num
num--
}
}
Output..
Page 14 of 17
Que.25 Count the total no. of lines in a file.
Ans..
wc -l f1.txt
Output..
Que.26 Count number of characters in first five lines of filex1.
Ans..
head -n 5 filex1 | wc -c
Output..
Que.27 Display files of current directory whose 1st character is
not digit.
Ans..
find . -maxdepth 1 -type f ! -name '[0-9]*'
Output..
Page 15 of 17
Que.28 Display last 2 lines of working directory.
Ans..
ls | tail -n 2
Output..
Que.29 Display only those files of current directory which is own
by the current user.
Ans..
find . -maxdepth 1 -type f -user $(whoami)
Output..
Que.30 To combine content of file x1 do not use cat command.
Page 16 of 17
Ans..
awk 'FNR==1{print ""} {print}' f1.txt f2.txt
Output..
Page 17 of 17