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

Linux

This document contains a summary of 43 Linux lab assignments completed by Aman Singh with the student ID 01717702020. The assignments involve basic Linux commands, file operations, scripting and programming tasks. They were submitted to Dr. Meenu Chopra of VIVEKANANDA INSTITUTE OF PROFESSIONAL STUDIES between February 2023 to May 2023.

Uploaded by

Aman narwal
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)
56 views

Linux

This document contains a summary of 43 Linux lab assignments completed by Aman Singh with the student ID 01717702020. The assignments involve basic Linux commands, file operations, scripting and programming tasks. They were submitted to Dr. Meenu Chopra of VIVEKANANDA INSTITUTE OF PROFESSIONAL STUDIES between February 2023 to May 2023.

Uploaded by

Aman narwal
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
You are on page 1/ 63

VIVEKANANDA INSTITUTE OF PROFESSIONAL STUDIES

VIVEKANANDA SCHOOL OF INFORMATION TECHNOLOGY

BACHELOR OF COMPUTER
APPLICATION LINUX LAB PRACTICAL
FILE

Guru Gobind Singh Indraprastha University


Sector - 16C, Dwarka, Delhi - 110078

SUBMITTED TO: SUBMITTED BY:


Dr. Meenu Chopra Aman Singh
Associate Professor 01717702020
VSIT,VIPS BCA-6A
INDEX
S.No Topic Date Page no.
1. Lab Assignment 1 02-03-2023 1

2. 4
Lab Assignment 2 09-03-2023
3. Lab Assignment 3 16-03-23 5

4. Lab Assignment 4 23-03-23


7

5. 10
Lab Assignment 5 06-03-23
6. Lab Assignment 6 13-04-23
12

7. 14
Lab Assignment 7 20-04-23
8. 18
Lab Assignment 8 27-04-23
9. 22
Lab Assignment 9 04-05-23
10. 25
Write a shell script to print the table of 5. 11-05-23
11. Write a shell script to calculate the area 26
and perimeter of the rectangle, when
11-05-23
length and breadth of rectangle is given
by user.
12. Write a shell script to calculate the sum 27
of digits of a three-digit number. 11-05-23
13. Write a shell script which accepts 3 28
arguments a, b, c and prints the value 11-05-23
of a*20-b*2+c to standard output.
14. Write a shell script to print if the number 29
is odd or even accepted from user. 11-05-23
15. Write a shell script which takes a file 30
name along the command line and prints 11-05-23
its size.
16. 31
Write a menu driven shell script
which has the following options for 11-05-23
user to display and run according to
selection.
17. Write a shell script to print all 33
18-05-23
prime numbers from 1 to 100.
18. Write a shell script to find the factorial 34
value of a number entered by the user. 18-05-23
19. Write a shell script to print
18-05-23
35
Fibonacci series.
20. Write a shell script that computes the 36
gross salary of a employee according to 18-05-23
the some rules.
21. Write a shell script to print the sum of 10 37
numbers accepted from user and find the 18-05-23
average.
22. Write a shell script to calculate the grade 38
of student based on some conditions. 18-05-23
Take 5 subjects into consideration.
23. Write a shell script to find the simple 40
interest.
24. Write a shell script to print the weekdays 41
according to the number entered by the 18-05-23
user from 1 to 7 using cases.
25. Write a shell script to display “Good 42
Morning /Afternoon/ Evening/ Night”. 20-05-23
Take time from user and print as
banner.
26. Write a shell script to check, if string 43
entered by the user starts from a vowel or 20-05-23
consonant.
27. Write a shell script to accept 3 numbers 44
from user and find the largest 20-05-23
among them.
28. Write a shell script to print # pattern in 45
the given format. 21-05-23
29. Write a shell script to accept 10 46
21-05-23
numbers from user and sort them in
reverse order.
30. Write a shell script to search for the word 47
“exam” in a file and display total number 21-05-23
of matching word.
31. Write a script to convert seconds 48
accepted from user to hours and minutes. 21-05-23
32. Write a shell script to check whether a 49
string is palindrome or not. 21-05-23
33. Write a shell script to count the 50
21-05-23
number of ordinary files in root
directory.
34. Write a shell script to do following: 51
i)Print all files that have read permissions
21-05-23
only. ii) Print the number of users that are
logged in.
35. Write a shell script to create a directory 52
named BCA6E and then create a file
named p1 within BCA6E.Also display 25-05-23
the present directory as well as the list of
files containing BCA6E.
36. Write a shell script to accept a 53
filename, check if it is regular and show
25-05-23
its
contents. Also display the total number of
words in that file.
37. Write a shell script to accept a dirname, 54
check if it is directory & display its 25-05-23
content.
38. Write a shell script to accept a filename, 55
and create a copy of it named as 25-05-23
“newfile” in the same directory.
39. Write a shell script to accept an extension 56
name such as txt and display all the
filenames with this extension if exist, 25-05-23
otherwise display some appropriate
message.
40. Write a shell script to display “Good 57
Morning”, “Good afternoon”, and “Good 25-05-23
evening” depending on the hour.
41. Write a shell script to create a file named 58
student.txt containing the data provided
25-05-23
(Roll, Name and Marks) and then execute
the given operations.
42. Write a shell script to calculate the 59
maximum of three integers using
command line argument. The script also
25-05-23
displays the value of the special
variables/positional parameters such as
$0, $1 to $9, $$, $#, $*, $@ and $!.
Lab Assignment 1

1. Write a command to display the current working directory.


Command: pwd
Output:

2. Write a command to display the current user which we are currently using.
Command: whoami
Output:

3. Write a command to list the directories and files in current working directory.
Command: ls
Output:

4. Write a command to display your current working environment.


Command: env
Output:

5. Write a command to print the provided text.


Command: echo “hello user”
Output:

6. Write a command to show history of all the executed commands.


Commands: history
Output:

Aman Singh 1 01717702020


7. Write a command to change the password for current user.
Command: passwd
Output:

8. Write a command to change directory to base directory.


Command: cd ~
Output:

9. Write a command to change directory to parent directory.


Command: cd ..
Output:

10. Write a command to change directory to root directory.


Command: cd /
Output:

11. Write a command to clear the screen.


Command: clear
Output:
Before:

Aman Singh 2 01717702020


After:

12. Write a command to make directory.


Command : mkdir newfolder Output:

13. Write a command to rename the file/folder.


Command: mv newfolder myfolder
Output:

14. Write a command to move a file/folder.


Command mv myfolder newfolder1
Output:

Aman Singh 3 01717702020


Lab Assignment 2
1. Write a command to display date.
Command: date
Output:

2. Show different date styles using format specifiers


Command: date %format specifier
Output:
+%D

+%D +%T

+%y-%m-%d

+%y/%m/%d

+%A %B %D %T %Y

Aman Singh 4 01717702020


Lab Assignment 3
1. Write command to show the total, used, free, shared memory and swap.
Command: free
Output:

2. Write the command to show to only show the memory in different measurement units.
Command: free -unitspecifier ( -b for byte -g for gigabyte -m for Megabyte)
Output:
-b

-k

-m

-g

Aman Singh 5 01717702020


3. Write a command to display the memory on regular interval
Command: free -s N (N is the time)
Output:

Aman Singh 6 01717702020


Lab Assignment 4
1. Write a command to change your directory to home directory
Command: cd
Output:

2. Write a command to change directory to /boot/grub/


Command: cd /boot/
Output:

3. Write a command to go to parent directory


Command: cd ..
Output:

4. Write a command to go to root directory.


Command: cd /
Output:

5. Write a command to display the list of all the files and directory in the present
directory.
Command: ls
Output:

15. Write a command to show a long list of all the file and folders of the present working
directory.
Command: ls -l
Output:

Aman Singh 7 01717702020


16. Write a command to stay where you are and display contents of /etc.
Command: ls /etc
Output:

17. Write a command to stay where you are and display the contents of /bin and /sbin.
Command: ls /bin /sbin
Output:

18. Write a command to display list of all files in human readable format.
Command: ls -ah
Output:

19. Write a command to display files in /boot in long format and human readable format.
Command: ls /boot -lh
Output:

20. Write a command to make a testdir in your home directory.


Command: mkdir ~testdir

Aman Singh 8 01717702020


Output:

21. Write a command to change to the /etc directory, stay here and create a directory newdir
in your home directory.
Command: cd /etc; mkdir ~newdir Output:
22. Write a command to remove the test dir.

Command: rm testdir or rmdir testdir


Output:

Aman Singh 9 01717702020


Lab Assignment 5
1. Write a command to display the file type of /bin/cat, /etc/passwd and /usr/bin/passwd.
Command: file /bin/cat /etc/passwd /usr/bin/passwd
Output:

2. Write a command to create a directory ~/touched and enter it.


Command: mkdir ~/touched; cd ~/touched
Output:

3. Write a command to create today.txt and yesterday.txt


Command: touch today.txt yesterday.txt
Output:

4. Write a command to create copy of yesterday.txt


Command:cp yesterday.txt copy_yesterday.txt
Output:

5. Write a command to create a dir ~/test_backup and copy all files of ~/touched in the
~/test_backup
Command: mkdir ~/testbackup ; cp -r ~/touched ~/testbackup/ Output:
6. Write a command to delete testbackup and all its contents

Command:rm -rf ~/testbackup


Output:

7. Write a command to copy all .conf files from etc to ~/etc


Command: cp -r /etc/*.conf ~/etcbackup

Aman Singh 10 01717702020


Output:

8. Write a command to rename all .conf files to *.backup where * is a number.


Command:
Output:

Aman Singh 11 01717702020


Lab Assignment 6
1. Write a command to check when you type passwd, which file is executed ?
Command: which passwd
Output:

2. Write a command to check what type of file is /usr/bin/passwd.


Command: file /usr/bin/passwd or which passwd | file
Output:

3. Execute pwd command twice.


Command: pwd; pwd
Output:

4. Execute ls after cd /etc but only if cd /etc does not error.


Command: cd /etc && ls
Output:

5. Execute cd /etc after cd etc, but only if cd etc fails.


Command: cd etc || cd /etc
Output:

6. Execute sleep 6, what is this command doing ?


Command: Sleep 6s
Output:

7. Execute sleep 200 in background (do not wait for it to finish).


Command: sleep 200s &

Aman Singh 12 01717702020


Output:

Aman Singh 13 01717702020


Lab Assignment 7
1. Does the file /bin/cat exist ? What about /bin/dd and /bin/echo. What is the type of
these files ?
Command: ls /bin/cat ; file /bin/cat
ls /bin/dd ; file /bin/dd
ls /bin/echo ; file /bin/echo
Output:

2. What is the size of the Linux kernel file(s) (vmlinu*) in /boot ?


Command: ls -lh /boot/vm*
Output:

3. Issue the following two commands, and look at the first character of each output Line.
Command: ls -l /dev/sd* /dev/hd*
ls -l /dev/tty* /dev/input/mou*
Output:

Aman Singh 14 01717702020


4. Use cat to display /etc/hosts and /etc/resolv.conf. What is your idea about the
purpose of these files ?
Command : cat /etc/hosts
Cat /etc/resolv.conf Output:

5. Are there any files in /etc/skel/ ? Check also for hidden files.
Command: ls -al /etc/skel/
Output:

Aman Singh 15 01717702020


6. Display /proc/cpuinfo. On what architecture is your Linux running ?
Command: cat /proc/cpuinfo
Output:

7. Can you enter the /root directory ? Are there (hidden) files ?
Command: cd /root; ls -al
Output:

Aman Singh 16 01717702020


8. Is /var/log a file or a directory ? What about /var/spool ?
Commands: ls -lh /var/log /var/spool
Output:Lab

Aman Singh 17 01717702020


Lab Assignment 8
1. Display the first 12 lines of /etc/services.
Command: head -12/etc/services Output:
2. Display the last line of /etc/passwd.

Command: tail -1 /etc/passwd Output:

3. Use cat to create a file named count.txt that looks like this:
Command: cat > count.txt
Output:

4. Use cp to make a backup of this file to cnt.txt.


Command: cp count.txt count_backup.txt Output:

5. Display catcnt.txt, but with all lines in reverse order (the last line first).
Command:
Output:

Aman Singh 18 01717702020


6. Use more to display /etc/services.
Command: more /etc/services
Output:

7. Display the readable character strings from the /usr/bin/passwd command.


Command: strings /usr/bin/passwd
Output:

8. Use ls to find the biggest file in /etc.


Command: ls -lrS /etc
Output:

9. Use cat to create a file named tailing.txt that contains the contents of tailing.txt
followed by the contents of /etc/passwd.
Command:cat /etc/passwd >> tailing.txt
Output:

Aman Singh 19 01717702020


10. Use cat to create a file named tailing.txt that contains the contents of tailing.txt
preceded by the contents of /etc/passwd.
Command: mv tailing.txt tmp.txt ; cat /etc/passwd tmp.txt > tailing.txt Output:
11. Use echo to display Hello followed by your username. (use a bash variable!)

Command: echo hello $USER


Output:

12. List all current shell variables.


Command: set
Output:

13. List all exported shell variables.


Command: env
export
declare -x
Output:

Aman Singh 20 01717702020


14. Do the env and set commands display your variable ?
Command: env | more
Set | more
Output:

15. Create two variables, and export one of them.


Command: var1=1; export var2=2
Output:

16. Display the exported variable in an interactive child shell.


Commands: bash
Echo $var2

Aman Singh 21 01717702020


Lab Assignment 9
1. Create a test directory and enter it.
Command: mkdir test_Dir; cd test_Dir
Output:

2. Create the following files:


file1
file10
file11
file2
file3
file33
fileAB
filea
fileA
fileAA
A file(
file 2
Commands:touch file1 file10 file11 file2 File2 File3
touch file33 fileAB filea fileA fileAAA
touch “file(“
touch “file 2”
3. List all files starting with file
Command: ls file*
Output:

4. List all files starting with File


Command: ls File*
Output:

5. List (with ls) all files starting with file and ending in a number. ls
file*[0-9]
Output:

6. List (with ls) all files starting with file and ending with a letter ls
file*[a-z]
Output:

Aman Singh 22 01717702020


7. List (with ls) all files starting with File and having a digit as fifth character. ls
File[0-9]*
Output:

8. List (with ls) all files starting with File and having a digit as fifth character and nothing else.
ls File[0-9]
Output:

9. List (with ls) all files starting with a letter and ending in a number. ls
[a-z]*[0-9]
Output:

10. List (with ls) all files that have exactly five characters. ls
?????
Output:

11. List (with ls) all files that start with f or F and end with 3 or A. ls
[fF]*[3A]
Output:

12. List (with ls) all files that start with f have i or R as second character and end in a
number.
ls f[iR]*[0-
9] Output:

13. List all files that do not start with the letter F. ls
[!F]*

14. Copy the value of $LANG to $MyLANG.


MyLANG=$LANG
Output:

Aman Singh 23 01717702020


15. You receive information that one of your servers was cracked, the cracker probably replaced
the ls command. You know that the echo command is safe to use. Can echo replace ls ? How
can you list the files in the current directory with echo ?
Command: echo *
Output:

16. Put a sorted list of all logged on users in onlineusers.txt.


Command: who | cut -d' ' -f1 | sort > onlineusers.txt Output:

17. Make a list of all filenames in /etc that contain the string conf in their filename.
Command: ls /etc | grep conf
Output:

18. Make a sorted list of all files in /etc that contain the case insensitive string conf in their
filename.
Command: ls /etc | grep -i conf | sort Output:

19. Write a line that receives a text file, and outputs all words on a separate line.
$ cat text2
it is very cold today without the sun
Command: $ cat text2 | tr ‘ ‘ ‘\n’

Aman Singh 24 01717702020


Ques 47. Write a Shell Script to print the table of 5. Ans.
#!/bin/bash

# Read the number from the user read -p


"Enter a number: " number

echo "Table of $number:"

# Loop to calculate and print the table up to 10 for


((i=1; i<=10; i++))
do
result=$((number * i))
echo "$number x $i = $result"
done

Output:

Aman Singh 25 01717702020


Question 48. Write a Shell Script to calculate the area and perimeter of the rectangle, when length
and breadth of rectangle is given by user.

Answer:
#!/bin/bash

# Read the length from the user


read -p "Enter the length of the rectangle: " length

# Read the breadth from the user


read -p "Enter the breadth of the rectangle: " breadth

# Calculate the area area=$((length


* breadth))

# Calculate the perimeter perimeter=$((2


* (length + breadth)))

echo "Area of the rectangle: $area"


echo "Perimeter of the rectangle: $perimeter" echo

"Perimeter of the rectangle: $perimeter"

Aman Singh 26 01717702020


Ques 49. Write a Shell Script to calculate the sum of digits of a three-digit number.

Answer:
#!/bin/bash

# Read the three-digit number from the user read -p


"Enter a three-digit number: " number

# Check if the number is three digits


if ((number < 100 || number > 999)); then
echo "Invalid input. Please enter a three-digit number." exit 1
fi

# Calculate the sum of digits


digit1=$((number % 10))
digit2=$(((number / 10) % 10))
digit3=$((number / 100)) sum=$
((digit1 + digit2 + digit3))

echo "Sum of digits: $sum" Output:

Aman Singh 27 01717702020


Question 50: Write a shell script which accepts 3 arguments a, b and c and prints the value of a*20-
b*2+c to standard output.
Answer:
#!/bin/bash

# Check if three arguments are provided if [


$# -ne 3 ]; then
echo "Invalid number of arguments. Please provide values for a, b, and c." exit 1
fi

# Extract the values of a, b, and c from the arguments a=$1


b=$2
c=$3

# Perform the calculation result=$


((a * 20 - b * 2 + c))

echo "Result: $result"

Output:

Aman Singh 28 01717702020


Question 51. Write a shell script to check whether number entered is even or odd. Answer:
#!/bin/bash

# Read the number from the user read -p


"Enter a number: " number

# Check if the number is even or odd if


((number % 2 == 0)); then
echo "$number is even."
else
echo "$number is odd."
fi

Output:

Aman Singh 29 01717702020


Question 52: Write a shell script which take the file name as command line arguments and prints its
size.
Answer:
#!/bin/bash

# Check if a file name is provided as a command line argument if [ $#


-ne 1 ]; then
echo "Usage: $0 <file_name>"
exit 1
fi

# Get the file name from the command line argument


file_name=$1

# Check if the file exists


if [ ! -f "$file_name" ]; then
echo "File '$file_name' does not exist." exit
1
fi

# Get the size of the file


file_size=$(du -h "$file_name" | awk '{print $1}')

echo "Size of file '$file_name': $file_size"

Output:

Aman Singh 30 01717702020


Question 53: Write a menu driven shell script which has the following options for user to display
and run according to selection.
Answer:
#!/bin/bash

# Function to display the menu


display_menu() {
echo "Menu:" echo
"1. Option 1"
echo "2. Option 2"
echo "3. Option 3"
echo "4. Exit"
}

# Function for option 1


option1() {
echo "You selected Option 1"
# Add the logic for Option 1 here
}

# Function for option 2


option2() {
echo "You selected Option 2"
# Add the logic for Option 2 here
}

# Function for option 3


option3() {
echo "You selected Option 3"
# Add the logic for Option 3 here
}

# Main script logic


while true; do
display_menu
read -p "Enter your choice (1-4): " choice

case $choice in
1) option1 ;;
2) option2 ;;
3) option3 ;;
4) echo "Exiting..."; exit ;;
*) echo "Invalid choice. Please enter a valid option." ;; esac

echo
done

Aman Singh 31 01717702020


Output:

Aman Singh 32 01717702020


Question 54: Write a shell script to print all prime numbers from 1 to 100. Answer:
#!/bin/bash

# Function to check if a number is prime is_prime()


{
num=$1

if [ $num -lt 2 ]; then


return 1
fi

for ((i=2; i*i<=num; i++))


do
if [ $((num % i)) -eq 0 ]; then
return 1
fi
done

return 0
}

# Iterate from 1 to 100 and print prime numbers for


((num=1; num<=100; num++))
do
is_prime $num

if [ $? -eq 0 ]; then
echo $num
fi
done

Output:

Aman Singh 33 01717702020


Question 55: Write a shell script to print the factorial of a number.

Answer:
#!/bin/bash

factorial() {
number=$1
fact=1
for ((i=2; i<=number; i++)); do
fact=$((fact * i))
done echo
$fact
}

echo "Enter a number:"


read num

if ! [[ $num =~ ^[0-9]+$ ]]; then


echo "Invalid input. Please enter a positive integer." exit 1
fi

result=$(factorial $num)
echo "The factorial of $num is: $result"

Output:

Aman Singh 34 01717702020


Question 56: Write a shell script to print Fibonacci series.
Answer:
#!/bin/bash

echo "Enter the number of terms:"


read n

a=0
b=1

echo "Fibonacci series:"


echo $a
echo $b
for ((i=2; i<n; i++)); do
c=$((a + b))
echo $c
a=$b
b=$c
done
Output:

Aman Singh 35 01717702020


Question 57: Write a shell script that computes the gross salary of an employee according to the some
rules.
Answer:
#!/bin/bash

echo "Enter basic salary:" read


basic_salary

if ! [[ $basic_salary =~ ^[0-9]+(\.[0-9]+)?$ ]]; then


echo "Invalid input. Please enter a valid salary amount." exit 1
fi

hra=$((basic_salary * 10 / 100))
da=$((basic_salary * 15 / 100)) gross_salary=$
((basic_salary + hra + da))

echo "Gross salary: $gross_salary" Output:

Aman Singh 36 01717702020


Question 58: Write a shell script to print the sum of 10 numbers accepted from user and find the
average.
Answer:
#!/bin/bash
sum=0
echo "Enter 10 numbers:"

for ((i=1; i<=10; i++)); do


echo "Enter number $i:"
read num
sum=$((sum + num))
done

average=$((sum / 10)).$(printf "%02d" $(( (sum * 100 / 10) % 100 ))) echo

"Sum: $sum"
echo "Average: $average"
Output:

Aman Singh 37 01717702020


Question 59: Write a shell script to calculate the grade of student based on some conditions. Take 5
subjects into consideration.
Answer:
#!/bin/bash

echo "Enter marks for Subject 1:"


read subject1

echo "Enter marks for Subject 2:"


read subject2

echo "Enter marks for Subject 3:"


read subject3

echo "Enter marks for Subject 4:"


read subject4

echo "Enter marks for Subject 5:"


read subject5

total_marks=$((subject1 + subject2 + subject3 + subject4 + subject5)) percentage=$((total_marks / 5))

grade=""

if ((percentage >= 90)); then


grade="A"
elif ((percentage >= 80)); then
grade="B"
elif ((percentage >= 70)); then
grade="C"
elif ((percentage >= 60)); then
grade="D"
else
grade="F"
fi

echo "Total Marks: $total_marks"


echo "Percentage: $percentage"
echo "Grade: $grade"

Aman Singh 38 01717702020


Output:

Aman Singh 39 01717702020


Question 60: Write a shell script to find the simple interest.
Answer:
#!/bin/bash

echo "Enter principal amount:" read


principal

echo "Enter rate of interest:"


read rate

echo "Enter time (in years):"


read time

interest=$(( (principal * rate * time) / 100 ))

echo "Simple Interest: $interest"


Output:

Aman Singh 40 01717702020


Question 61: Write a shell script to print the weekdays according to the number entered by the user
from 1 to 7 using cases
Answer:
#!/bin/bash

echo "Enter a number (1-7):"


read number

case $number in
1)
echo "Monday"
;;
2)
echo "Tuesday"
;;
3)
echo "Wednesday"
;;
4)
echo "Thursday"
;;
5)
echo "Friday"
;;
6)
echo "Saturday"
;;
7)
echo "Sunday"
;;
*)
echo "Invalid number. Please enter a number from 1 to 7."
;;
esac
Output:

Aman Singh 41 01717702020


Question 62: Write a shell script to display “Good Morning /Afternoon/ Evening/ Night”. Take time
from user and print as banner.
Answer:
#!/bin/bash

echo "Enter the time (24-hour format, e.g., 09:30):"


read time

hour=$(date -d "$time" +%H)

if [[ $hour -ge 0 && $hour -lt 12 ]]; then


greeting="Good Morning"
elif [[ $hour -ge 12 && $hour -lt 17 ]]; then
greeting="Good Afternoon"
elif [[ $hour -ge 17 && $hour -lt 21 ]]; then
greeting="Good Evening"
else
greeting="Good Night"
fi

echo "$greeting!"
Output:

Aman Singh 42 01717702020


Question 63: Write a shell script to check, if string entered by the user starts from a vowel or consonant.
Answer:
#!/bin/bash

echo "Enter a string:"


read string

first_char="${string:0:1}"

first_char_lower=$(echo "$first_char" | tr '[:upper:]' '[:lower:]') if

[[ $first_char_lower == [aeiou] ]]; then


echo "The string starts with a vowel." else
echo "The string starts with a consonant."
f
i Output:

Aman Singh 43 01717702020


Question 64: Write a shell script to accept 3 numbers from user and find the largest among them.
Answer:
#!/bin/bash

echo "Enter the first number:"


read n1

echo "Enter the second number:"


read n2

echo "Enter the third number:" read


n3

largest=$n1

if [ $n2 -gt $largest ]; then


largest=$n2
fi

if [ $n3 -gt $largest ]; then


largest=$n3
fi

echo "The largest number is: $largest"


Output:

Aman Singh 44 01717702020


Question 65 : Write a shell script to print # pattern in the given format. Answer:
#!/bin/bash

echo "Enter the number of rows:"


read rows

for ((i = 1; i <= rows; i++)); do


for ((j = 1; j <= i; j++)); do
echo -n "#"
done
echo ""
done

Output:

Aman Singh 45 01717702020


Question 66: Write a shell script to accept 10 numbers from user and sort them in reverse order.
Answer:
#!/bin/bash

numbers=()

echo "Enter 10 numbers:"

for ((i = 0; i < 10; i++)); do


read num
numbers+=($num)
done

sorted_numbers=($(printf '%s\n' "${numbers[@]}" | sort -rn))

echo "Sorted numbers in reverse order: ${sorted_numbers[@]}"


Output:

Aman Singh 46 01717702020


Question67: Write a shell script to search for the word “exam” in a file and display total number of
matching word.
Answer:
#!/bin/bash

echo "Enter the file name:" read


filename

if [ ! -f "$filename" ]; then
echo "File not found!"
exit 1
fi

count=$(grep -o -i -w "exam" "$filename" | wc -l)

echo "Total number of occurrences of the word 'exam': $count"


Output

Aman Singh 47 01717702020


Question 68: Write a script to convert seconds accepted from user to hours and minutes. Answer:
#!/bin/bash

echo "Enter the duration in seconds:"


read seconds

hours=$((seconds / 3600)) minutes=$((


(seconds % 3600) / 60 ))

echo "Duration: $hours hour(s) $minutes minute(s)" Output:

Aman Singh 48 01717702020


Question 69: Write a shell script to check whether a string is palindrome or not. Answer:
#!/bin/bash

echo "Enter a string:"


read string

string=$(echo "$string" | tr -d '[:space:]' | tr '[:upper:]' '[:lower:]') reverse_string=$

(echo "$string" | rev)

if [ "$string" = "$reverse_string" ]; then


echo "The string is a palindrome." else
echo "The string is not a palindrome."
fi
Output:

Aman Singh 49 01717702020


Question 70: Write a shell script to count the number of ordinary files in root directory. Answer:
#!/bin/bash

count=0

for item in /.* /; do


if [[ -f "$item" ]]; then
((count++))
fi
done

echo "Number of ordinary files in the root directory: $count" Output:

Aman Singh 50 01717702020


Question 71: Write a shell script to do following:

i)Print all files that have read permissions only.

ii) Print the number of users that are logged in.

Answer:
#!/bin/bash

script_dir=$(dirname "$(readlink -f "$0")")

echo "Files with read permissions only in the script's directory:"


find "$script_dir" -maxdepth 1 -type f -perm /u=r -perm /g= -perm /o= 2>/dev/null

echo "Number of users logged in:" who |


wc -l

Output:

Aman Singh 51 01717702020


Question 72: Write a shell script to create a directory named BCA6E and then create a file named p1
within BCA6E.Also display the present directory as well as the list of files containing BCA6E.

Answer:
#!/bin/bash

mkdir BCA6E
touch BCA6E/p1
echo "Present directory:"
pwd
echo "List of files containing BCA6E:"
find . -name '*BCA6E*'

Output:

Aman Singh 52 01717702020


Question 73: Write a shell script to accept a filename, check if it is regular and show its contents.
Also display the total number of words in that file

Answer:

#!/bin/bash

echo "Enter the filename:" read


filename
if [ ! -f "$filename" ]; then
echo "File not found!"
exit 1
fi

if [ ! -f "$filename" ]; then
echo "Not a regular file!"
exit 1
fi

echo "File Contents:"


cat "$filename"

word_count=$(cat "$filename" | wc -w) echo


"Total number of words: $word_count" Output:

Aman Singh 53 01717702020


Question 74: Write a shell script to accept a dirname, check if it is directory & display its content.

Answer:

#!/bin/bash

echo "Enter the directory name:"


read dirname
if [ ! -d "$dirname" ]; then echo
"Directory not found!" exit 1
fi

echo "Directory Contents:" ls


-l "$dirname"
Output:

Aman Singh 54 01717702020


Question 75: Write a shell script to accept a filename, and create a copy of it named as “newfile” in
the same directory.
Answer:
#!/bin/bash

echo "Enter the filename:" read


filename

if [ ! -f "$filename" ]; then
echo "File not found!"
exit 1
fi

directory=$(dirname "$filename") cp

"$filename" "$directory/newfile"

echo "File copied as 'newfile' in the same directory."

Output:

Aman Singh 55 01717702020


Question 76: Write a shell script to accept an extension name such as txt and display all the filenames
with this extension if exist, otherwise display some appropriate message.
Answer:
#!/bin/bash

echo "Enter the extension name:" read


extension

if ls *."$extension" 1>/dev/null 2>&1; then


echo "Files with extension '.$extension' exist:" ls
*."$extension"
else
echo "No files with extension '.$extension' found."
fi
Output:

Aman Singh 56 01717702020


Question 77: Write a shell script to display “Good Morning”, “Good afternoon”, and “Good
evening” depending on the hour.
Answer:
#!/bin/bash
current_hour=$(date +%H)
if (( current_hour >= 0 && current_hour < 12 )); then echo
"Good Morning!"
elif (( current_hour >= 12 && current_hour < 17 )); then echo
"Good Afternoon!"
else
echo "Good Evening!"
fi
Output:

Aman Singh 57 01717702020


Question 78: Write a shell script to create a file named student.txt containing the data
provided (Roll, Name and Marks) and then execute the given operations.
Answer:
#!/bin/bash
echo "101,Max Will,85" > student.txt
echo "102,Pulkit,92" >> student.txt
echo "103,Test Johnson,78" >> student.txt echo
"104,Sarah Williams,95" >> student.txt

echo "Contents of student.txt:"


cat student.txt

total_students=$(wc -l < student.txt)


echo "Total number of students: $total_students"

total_marks=$(awk -F ',' '{ total += $3 } END { print total }' student.txt)


average_marks=$(echo "scale=2; $total_marks / $total_students" | bc) echo
"Average marks: $average_marks"
highest_marks=$(sort -t ',' -k3 -nr student.txt | head -n 1) echo
"Student with the highest marks: $highest_marks" Output:

Aman Singh 58 01717702020


Question 79. Write a shell script to calculate the maximum of three integers using command line
argument. The script also displays the value of the special variables/positional parameters such as
$0, $1 to $9, $$, $#, $*, $@ and $!.

Answer:
#!/bin/bash

num1=$1
num2=$2
num3=$3

max=$num1
if ((num2 > max)); then
max=$num2
fi
if ((num3 > max)); then
max=$num3
fi

echo "Maximum value: $max" echo

"Script name: $0"


echo "First argument: $1" echo
"Second argument: $2" echo
"Third argument: $3" echo
"Process ID: $$"
echo "Number of arguments: $#"
echo "All arguments (separated by spaces): $*"
echo "All arguments (separated by individual quotes): $@" echo
"Exit status of last command: $!"

Output:

Aman Singh 59 01717702020

You might also like