163 OS Week-3
163 OS Week-3
… Date:……21/08/2024……
OPERATING SYSTEMS
ASSIGNMENT-3
1.AIM : Write a shell script for printing all file related information (i.e. size, permissions etc.)
Description :
Loop (for file in *): Iterates over each file in the current directory.
File Information:
CODE :
#!/bin/bash
for file in *; do
if [ -f "$file" ]; then
echo "---------------------------------"
echo ""
fi
34
Page No …………………. Signature of the Faculty………………………...
Roll No:160122733163 Exp. No:…3.… Date:……21/08/2024……
done
else
Fi
OUTPUT :
2. AIM : Write a shell script that read a number and generate a pattern given below:
12
123
1234
Description :
Shebang (#!/bin/bash): Specifies that the script should be executed using Bash.
• · echo -n "Enter a number: ": Prompts the user to enter a number without a newline.
• read number: Reads the user's input and stores it in the variable number.
Generate Pattern:
• · for ((i=1; i<=number; i++)): Outer loop that iterates from 1 to the user-provided
number.
• for ((j=1; j<=i; j++)): Inner loop that iterates from 1 to the current value of i, printing
numbers in a row.
35
Page No …………………. Signature of the Faculty………………………...
Roll No:160122733163 Exp. No:…3.… Date:……21/08/2024……
• echo -n "$j ": Prints each number followed by a space on the same line.
• echo: Moves to the next line after printing the row of numbers.
CODE :
#!/bin/bash
read number
do
do
done
echo
done
OUTPUT :
3. AIM : Write a shell script to compare larger integer values from ‘n’ number of arguments
using command line arguments.
Description :
Argument Check:
36
Page No …………………. Signature of the Faculty………………………...
Roll No:160122733163 Exp. No:…3.… Date:……21/08/2024……
Initialize:
CODE:
#!/bin/bash
if [ $# -eq 0 ]; then
exit 1
fi
largest=$1
do
largest=$num
fi
done
OUTPUT :
37
Page No …………………. Signature of the Faculty………………………...
Roll No:160122733163 Exp. No:…3.… Date:……21/08/2024……
4. AIM: Write a shell script that prints a given number in reverse order.
Description :
Argument Check:
Read Input:
• while [ $number -ne 0 ]: Loops while there are digits remaining in the number.
• digit=$(( number % 10 )): Extracts the last digit of the number.
• reverse=$(( reverse * 10 + digit )): Appends the digit to the reversed number.
• number=$(( number / 10 )): Removes the last digit from the number.
CODE :
#!/bin/bash
if [ $# -eq 0 ]; then
exit 1
fi
number=$1
reverse=0
do
digit=$(( number % 10 ))
38
Page No …………………. Signature of the Faculty………………………...
Roll No:160122733163 Exp. No:…3.… Date:……21/08/2024……
number=$(( number / 10 ))
done
OUTPUT :
5. AIM : Write a shell script for generating random 8-character password including alpha
numeric characters.
Description :
•/dev/urandom: This is a special file that generates random data. It’s often used for
generating random numbers or strings.
· head -c 8: The head command outputs the first part of files. The -c 8 option restricts the
output to the first 8 characters.
·Piping: The | (pipe) operator is used to pass the output of one command as input to another.
Here, the random data generated by /dev/urandom is passed through tr, and the resulting
string is trimmed to 8 characters by head.
CODE :
#!/bin/bash
OUTPUT :
39
Page No …………………. Signature of the Faculty………………………...
Roll No:160122733163 Exp. No:…3.… Date:……21/08/2024……
6. AIM :Write a shell script that takes any number of directories as command-line arguments
and then lists the contents of each of these directories.
Description :
· Check for Arguments (if [ $# -eq 0 ]; then): If no arguments are passed, the script lists the
contents of the current directory (".").
· Loop Through Directories (for dir in "$@"; do): If arguments are provided, the script
loops through each one.
· Check if Directory Exists (if [ -d "$dir" ]; then): The script checks whether each argument
is a valid directory. If it is, it lists the contents; if not, it prints an error message.
· Listing Contents Without Using ls: The script avoids using the ls command and instead
relies on a loop to iterate through the contents of the directory.
CODE:
#!/bin/bash
list_directory_contents() {
if [ -e "$item" ]; then
fi
done
if [ $# -eq 0 ]; then
list_directory_contents "."
else
if [ -d "$dir" ]; then
40
Page No …………………. Signature of the Faculty………………………...
Roll No:160122733163 Exp. No:…3.… Date:……21/08/2024……
list_directory_contents "$dir"
echo
else
fi
done
fi
OUTPUT:
7. AIM : Write a shell to count the number of users with user IDs between 500 and 100000
on the system.
Description :
Default UID Range: The script initializes the default min_uid to 500 and max_uid to 10000.
These are standard ranges for regular user accounts on many Linux systems.
· Command Line Arguments: The script checks if custom min_uid and max_uid values are
provided as command-line arguments. If two arguments are provided, it updates the range.
· Reading /etc/passwd: The script reads each line of the /etc/passwd file. The IFS=: read -r
username _ uid _ splits each line by the : character, assigning the username to $username and
the UID to $uid. Other fields are ignored.
· UID Check: For each user, the script checks if the UID falls within the specified range. If it
does, the username and UID are printed, and the count is incremented.
· Counting Users: The script keeps track of how many users fall within the specified range
and prints the total at the end.
CODE :
41
Page No …………………. Signature of the Faculty………………………...
Roll No:160122733163 Exp. No:…3.… Date:……21/08/2024……
#!/bin/bash
min_uid=500
max_uid=10000
if [ $# -ge 2 ]; then
min_uid=$1
max_uid=$2
echo "Error: Please provide both minimum and maximum UID values."
exit 1
fi
user_count=0
user_count=$((user_count + 1))
fi
echo "Total number of users with UID between $min_uid and $max_uid: $user_count"
OUTPUT:
42
Page No …………………. Signature of the Faculty………………………...
Roll No:160122733163 Exp. No:…3.… Date:……21/08/2024……
8. AIM : For each directory in the $PATH, display the number of executable files in that
directory.
Description:
• IFS=':' sets the Internal Field Separator to a colon, so that read splits the $PATH
string into an array of directories.
• · The script loops through each directory stored in the directories array.
• · Before processing, the script checks whether each directory in $PATH actually
exists. This avoids errors if the directory does not exist.
• · The script loops through each file in the directory ("$dir"/*) and checks if it is both
a regular file ([ -f "$file" ]) and executable ([ -x "$file" ]).
• · After counting the executables in a directory, the script prints the directory name
and the number of executable files found.
• · Finally, the script prints the total number of executable files across all directories in
the $PATH.
43
Page No …………………. Signature of the Faculty………………………...
Roll No:160122733163 Exp. No:…3.… Date:……21/08/2024……
CODE :
#!/bin/bash
total_executables=0
if [ -d "$dir" ]; then
executable_count=0
executable_count=$((executable_count + 1))
fi
done
Echo
total_executables=$((total_executables + executable_count))
else
fi
done
44
Page No …………………. Signature of the Faculty………………………...
Roll No:160122733163 Exp. No:…3.… Date:……21/08/2024……
OUTPUT:
9. AIM : Write a shell script to generate Fibonacci numbers less than or equal to a given
number ‘n’.
Description :
· Argument Handling:
• · The script expects exactly one argument (the boundary number n).
• It checks if the argument is a positive integer.
CODE :
#!/bin/bash
generate_fibonacci() {
local limit=$1
local a=0
local b=1
45
Page No …………………. Signature of the Faculty………………………...
Roll No:160122733163 Exp. No:…3.… Date:……21/08/2024……
echo $a
local temp=$a
a=$b
b=$((temp + b))
done
if [ $# -ne 1 ]; then
exit 1
fi
exit 1
fi
generate_fibonacci $1
OUTPUT:
c) Home directory
46
Page No …………………. Signature of the Faculty………………………...
Roll No:160122733163 Exp. No:…3.… Date:……21/08/2024……
i) CPU information
j) Memory information
Description :
· Functions:
· Main Loop:
· Execution:
• Save the script to a file, e.g., system_info.sh, make it executable with chmod +x
system_info.sh, and run it with ./system_info.sh.
CODE :
#!/bin/bash
display_logged_users() {
who
47
Page No …………………. Signature of the Faculty………………………...
Roll No:160122733163 Exp. No:…3.… Date:……21/08/2024……
display_script() {
cat "$0"
display_home_directory() {
echo "$HOME"
display_os_info() {
uname -a
display_cwd() {
pwd
display_user_count() {
who | wc -l
display_shells() {
cat /etc/shells
48
Page No …………………. Signature of the Faculty………………………...
Roll No:160122733163 Exp. No:…3.… Date:……21/08/2024……
display_disk_info() {
df -h
display_cpu_info() {
lscpu
display_memory_info() {
free -h
display_fs_info() {
df -T
display_processes() {
ps aux
show_menu() {
clear
49
Page No …………………. Signature of the Faculty………………………...
Roll No:160122733163 Exp. No:…3.… Date:……21/08/2024……
while true; do
show_menu
read choice
case $choice in
1) display_logged_users ;;
2) display_script ;;
3) display_home_directory ;;
4) display_os_info ;;
5) display_cwd ;;
6) display_user_count ;;
50
Page No …………………. Signature of the Faculty………………………...
Roll No:160122733163 Exp. No:…3.… Date:……21/08/2024……
7) display_shells ;;
8) display_disk_info ;;
9) display_cpu_info ;;
10) display_memory_info ;;
11) display_fs_info ;;
12) display_processes ;;
0) exit 0 ;;
esac
echo
Done
OUTPUT :
51
Page No …………………. Signature of the Faculty………………………...