You are on page 1of 10

CONTENTS

Contents...............................................................................................................................2
Code.....................................................................................................................................3
Output................................................................................................................................10

1
Source Code:
Main Code:-
I.e. – Calendar, Date & Time, Delete File.

#!/bin/bash

# Function to delete selected file


delete_file() {
local directory
directory=$(dialog --title "Enter Directory Name" --inputbox "Enter
the directory name (leave blank for current directory):" 8 60 2>&1
>/dev/tty)
directory="${directory:-.}" # If no directory name provided, use
current directory

local file_list
file_list=$(ls "$directory")

local selected_file
selected_file=$(dialog --title "Select File to Delete" --menu
"Choose a file to delete:" 20 60 10 $(for file in $file_list; do echo
"$file" "$file"; done) 2>&1 >/dev/tty)

if [ -n "$selected_file" ]; then
local confirm_delete
confirm_delete=$(dialog --title "Confirm Deletion" --yesno "Are
you sure you want to delete '$selected_file'?" 8 60 2>&1 >/dev/tty)
if [ $? -eq 0 ]; then
rm -i "$directory/$selected_file"
if [ $? -eq 0 ]; then
dialog --title "File Deleted" --msgbox "'$selected_file' has
been deleted." 8 60
else
dialog --title "Error" --msgbox "Error deleting
'$selected_file'." 8 60
fi
fi

2
fi
}

# Function to show calendar


show_calendar() {
local selected_date=$(dialog --title "Calendar" --calendar "Select
Date" 0 0 2>&1 >/dev/tty)
if [ -n "$selected_date" ]; then
add_event "$selected_date"
fi
}

# Function to add an event for a selected date


add_event() {
local selected_date="$1"
local current_date=$(date +%Y%m%d)
local selected_date_formatted=$(date -d "$selected_date" +%Y
%m%d)

# Check if selected date is before the current date


if [ "$selected_date_formatted" -lt "$current_date" ]; then
dialog --title "Error" --msgbox "Cannot add events for past
dates." 8 60
else
local event=$(dialog --title "Add Event" --inputbox "Enter event
for $selected_date:" 8 60 2>&1 >/dev/tty)
if [ -n "$event" ]; then
dialog --title "Event Added" --msgbox "Event '$event' added
for $selected_date" 8 60
fi
fi
}

# Function to display current date and time using an info box


show_date_time() {
local current_datetime=$(date)

3
dialog --title "Date and Time" --msgbox "\n$current_datetime\n"
10 50
}

# Main menu function


main_menu() {
while true; do
choice=$(dialog --clear \
--backtitle "Main Menu" \
--title "Choose an option" \
--menu "Select one of the following options:" \
15 50 7 \
1 "Show Calendar" \
2 "Show Date and Time" \
3 "Delete File" \
4 "Exit" \
2>&1 >/dev/tty)

case $choice in
1)
show_calendar
;;
2)
show_date_time
;;
3)
delete_file
;;
4)
break
;;
*)
;;
esac
done
}

4
# Call the main menu function to start the program
main_menu

System Info Code:


#!/bin/bash

# Function to show operating system information and processes list


show_os_info_processes() {
lsb_release -a | dialog --title "Operating System Information" --
msgbox "$(lsb_release -a)" 20 80
ps l | dialog --title "Processes List" --msgbox "$(ps l )" 20 80
}

# Function to show computer CPU information


show_cpu_info() {
lscpu | dialog --title "Computer CPU Information" --msgbox "$
(lscpu)" 20 80
}

# Function to show memory information


show_memory_info() {
mem_info=$(free -h | awk 'NR <= 4 {printf "%-18s %-15s %-15s %-
15s %-15s %-15s %-15s\n", $1, $2, $3, $4, $5, $6, $7}')
dialog --title "Memory Information" --msgbox "$mem_info" 20 100
}

# Function to show storage information


show_storage_info() {
# Create a temporary file to store the information
temp_file=$(mktemp /tmp/storage_info.XXXXXX)
# Get the storage information and save it to the temporary file
{
echo "Partitions:"
lsblk
echo -e "Detailed Partitions Information:"
lsblk -o NAME,FSTYPE,SIZE,MOUNTPOINT
echo -e "\nFile System Usage:"

5
df -h
} > "$temp_file"
# Display the information using dialog
dialog --title "Storage Information" --textbox "$temp_file" 30 100
# Remove the temporary file
rm -f "$temp_file"
}

# Function to show mounted file-systems information


show_mounted_info() {
# Create a temporary file to store the information
temp_file=$(mktemp /tmp/mounted_info.XXXXXX)
# Get the mounted file-systems information and save it to the
temporary file
mount | column -t > "$temp_file"
# Display the information using dialog
dialog --title "Mounted File-Systems Information" --textbox
"$temp_file" 30 100
# Remove the temporary file
rm -f "$temp_file"
}

# Function to show network information


show_network_info() {
# Create a temporary file to store the information
temp_file=$(mktemp /tmp/network_info.XXXXXX)
# Get the network interfaces and routing table information and
save it to the temporary file
{
echo "Interfaces:"
ip a
echo
echo "Routing Table:"
ip route
} > "$temp_file"
# Display the information using dialog
dialog --title "Network Information" --textbox "$temp_file" 30 100

6
# Remove the temporary file
rm -f "$temp_file"
}

# Function to show kernel information


show_kernel_info() {
# Get the kernel information
kernel_info=$(uname -a)
# Replace spaces with newline characters
kernel_info_formatted=$(echo "$kernel_info" | tr ' ' '\n')
# Display the formatted information using dialog
dialog --title "Kernel Information" --msgbox
"$kernel_info_formatted" 20 80
}

# Exit program
exit_program() {

clear
exit 0
}

# Main menu
while true; do
choice=$(dialog --title "Main Menu" --menu "Choose an option" 15
60 9 \
1 "Operating System Information & Processes" \
2 "Computer CPU Information" \
3 "Memory Information" \
4 "Storage Information" \
5 "Mounted File-Systems Information" \
6 "Network Information" \
7 "Kernel Information" \
8 "Exit" 3>&1 1>&2 2>&3)
case $choice in
1) show_os_info_processes ;;
2) show_cpu_info ;;

7
3) show_memory_info ;;
4) show_storage_info ;;
5) show_mounted_info ;;
6) show_network_info ;;
7) show_kernel_info ;;
8) exit_program ;;
*)
dialog --title "Invalid Choice" --msgbox "Please try again" 10
60 ;;
esac
done

8
Outputs:
Main menu

9
System Info:

10

You might also like