Uploaded by maranerid56

COURSEWORK 2 PRESENTATION

advertisement
FIRST SCRIPT
# Function to show date and time. DIREN
The Python code initializes a GTK window (DateTimeWindow) that displays the current date and
time. Here's a breakdown of what happens in the Python code:
The gi module is imported to use the GTK library.
The version of GTK required is specified as 3.0.
The DateTimeWindow class is defined, inheriting from Gtk.Window.
The __init__ method of DateTimeWindow sets up the window by setting the title and adding a label
to display the date and time.
The update_datetime method is defined to update the label with the current date and time. It uses
the datetime module to get the current datetime and formats it as a string.
The update_datetime method is called initially to display the current date and time.
The win object is created as an instance of DateTimeWindow.
The show_all method is called on the win object to display the window and its contents.
Finally, the Gtk.main() function is called to start the GTK main loop and handle events.
In summary, the show_datetime function attempts to create a GTK window in Python that
displays the current date and time. However, there are a few issues with the code that need
to be addressed for it to work correctly. For example, the initialization method should be
named __init__ (with double underscores) instead of _init_ (with single underscore).
Additionally, there is no need for a Bash function to execute this code. It can be executed
directly using the python3 command
# Function to show calendar. DIREN
This line starts a Bash function named show_calendar.
This line uses the zenity command-line tool to display a calendar dialog box to the user. The user can
select a date from the calendar, and the selected date is stored in the selection variable.
This line checks if the selection variable is not empty, meaning the user has selected a date.
This line uses the grep command to check if the selected date exists in the calendar.txt file. The
^$selection: pattern is used to match lines starting with the selected date followed by a colon (:).
If the selected date exists in the file, the relevant information associated with that date is extracted
using the cut command. The information is stored in the relevant_info variable.
If the selected date does not exist in the file, the script proceeds to the else block.
In the else block, the zenity command is used to prompt the user to enter relevant information for
the selected date. The entered information is stored in the relevant_info variable.
After that, the echo command is used to append the date and the relevant information to the
calendar.txt file, separated by a colon (:).
Finally, if a date was selected (not empty), a dialog box is displayed using the zenity command. The
dialog box shows the selected date and the relevant information associated with that date.
That concludes the explanation of the show_calendar function. It prompts the user to select a
date from a calendar, checks if the selected date exists in a file, prompts the user to add
relevant information if it doesn't exist, stores the information in the file, and displays the
selected date and relevant information in a dialog box.
# Function to delete a file. SUHAIL
delete_file() {
This line starts a Bash function named delete_file..
This line uses the zenity command-line tool to prompt the user to enter a directory path. The
entered path is stored in the directory variable.
This line checks if the directory variable is empty. If the user didn't provide a directory path, the
condition evaluates to true.
If no directory path is provided by the user, the script sets the directory variable to the current
directory (.).
This line uses the ls command to get a list of files in the specified directory. The output of ls is
stored in the files variable
This line uses the zenity command to prompt the user to enter a filename to delete. The
filenames in the files variable are displayed as a suggestion in the dialog box. The entered
filename is stored in the filename variable
This line constructs the full path to the file by concatenating the directory and filename variables,
separated by a slash (/). The result is stored in the path variable.
This line uses the zenity command to prompt the user to confirm whether they want to delete the
file. The user's response is stored in the confirm variable
This line checks if the user's response is "y" (yes).
If the user's response is not "y" (yes), the condition evaluates to false, and the script proceeds to
the elif block.
In the elif block, a dialog box is displayed using the zenity command to inform the user that the
file was not deleted.
That concludes the explanation of the delete_file function. It prompts the user to enter a directory
path, gets the list of files in the specified directory, prompts the user to enter a filename to
delete, constructs the full path to the file, prompts the user for confirmation to delete the file,
and either deletes the file or displays a message indicating that the file was not deleted based on
the user's confirmation response.
# Function to move a file to another directory DIREN & SUHAIL
This if condition checks if the $file variable is empty, indicating that no file was selected by the
user. If the condition is true, it displays an error message using the zenity command and returns a
non-zero exit status (1) to indicate an error.
This line uses the zenity command-line tool to prompt the user to select a destination directory.
The selected directory path is stored in the directory variable.
This if condition checks if the $directory variable is empty, indicating that no destination directory
was selected by the user. If the condition is true, it displays an error message using the zenity
command and returns a non-zero exit status (1) to indicate an error.
This part uses the mv command to move the selected file to the specified destination directory.
The $file variable represents the source file, and $directory represents the destination directory.
After executing the mv command, the script checks the exit status of the command using $?. If
the exit status is equal to 0, it means the file was successfully moved, and it displays a success
message using the zenity command.
If the exit status is not 0, indicating an error occurred during the file move, it displays an error
message using the zenity command.
That concludes the explanation of the move_file function. It prompts the user to select a file to
move and a destination directory, then it moves the file to the specified directory. It displays
appropriate success or error messages based on the outcome of the move operation.
# Function to exit the program. SUHAIL
This function named exit_program displays an informational message using the zenity command,
indicating that the program is exiting. It then calls the exit command with a status code of 0, which
terminates the script.
This section sets up a menu system using a while loop. The loop continues indefinitely (while true)
until the user chooses the "Exit" option.
Within the loop, the script presents the user with a menu using the zenity --entry command. The
menu options are defined in the options array, which consists of strings representing each option.
The user's choice is stored in the choice variable. The script then uses a case statement to match the
user's choice with the corresponding action.
•
If the user chooses option 1, the show_datetime function is called.
•
If the user chooses option 2, the show_calendar function is called.
•
If the user chooses option 3, the delete_file function is called.
•
If the user chooses option 4, the move_file function is called.
•
If the user chooses option 5, the exit_program function is called to exit the program.
•
If the user chooses an option that doesn't match any of the cases, an informational message
is displayed using zenity.
After each action is performed or an invalid option is chosen, an empty line is printed to improve
readability.
The loop continues, prompting the user for another option, until the user chooses the "Exit" option,
at which point the script terminates.
That concludes the explanation of the script. It provides a menu-based interface for the user to
choose various actions and calls the corresponding functions accordingly.
Technologies used:




Used functions to contain the scripts for certain tasks to be executed by the user.
While loops.
Used Zenity as a Graphical User Interface.
Used a Calendar.txt file to store the information input but the user on a specific date so that I
can be viewed later when accessing the specific date.
Additional Features and new technologies:


Used a python script to display actual date & time dynamically ( constantly changing ).
A Function to move a file to another directory.
SECOND SCRIPT
# Function to display system configuration information - Operating System Type
This function uses the zenity command-line tool to display system configuration information in a
dialog box. Specifically, it shows the operating system type obtained from the uname -a command.
The zenity command with the --info option creates an information dialog box. The --title option sets
the title of the dialog box to "System Configuration Information". The --text option sets the text
displayed in the dialog box. In this case, it includes the operating system type obtained by command
substitution $(uname -a), where uname -a is a command that retrieves detailed system information,
including the operating system type.
When the function is called, the dialog box is displayed with the operating system type information.
That concludes the explanation of the show_system_info1 function. It provides a simple way to
display the operating system type using the zenity command-line tool.
# Function to display system configuration information - Computer CPU Information
This function uses the zenity command-line tool to display system configuration information related
to the computer's CPU in a dialog box. It retrieves the CPU information by executing the lscpu
command.
The zenity command with the --info option creates an information dialog box. The --title option sets
the title of the dialog box to "System Configuration Information". The --text option sets the text
displayed in the dialog box. In this case, it includes the computer CPU information obtained from the
lscpu command. The $(lscpu) syntax executes the lscpu command and substitutes its output into the
text.
When the show_system_info2 function is called, the dialog box is displayed with the computer CPU
information. That concludes the explanation of the show_system_info2 function. It provides a
simple way to display the computer's CPU information using the zenity command-line tool and the
lscpu command
# Function to display system configuration information - Hard Disk Information
This function uses the zenity command-line tool to display system configuration information related
to memory in a dialog box. It retrieves the memory information by executing the free -m command.
The zenity command with the --info option creates an information dialog box. The --title option sets
the title of the dialog box to "System Configuration Information". The --text option sets the text
displayed in the dialog box. In this case, it includes the memory information obtained from the free m command. The $(free -m) syntax executes the free -m command and substitutes its output into
the text.
When the show_system_info3 function is called, the dialog box is displayed with the memory
information.
That concludes the explanation of the show_system_info3 function. It provides a simple way to
display the system's memory information using the zenity command-line tool and the free -m
command.
# Function to display system configuration information - File System (Mounted)
This function uses the zenity command-line tool to display system configuration information
related to the file systems that are currently mounted in a dialog box. It retrieves the file
system information by executing the mount command.
The zenity command with the --info option creates an information dialog box. The --title
option sets the title of the dialog box to "System Configuration Information". The --text
option sets the text displayed in the dialog box. In this case, it includes the file system
(mounted) information obtained from the mount command. The $(mount) syntax executes the
mount command and substitutes its output into the text.
When the show_system_info5 function is called, the dialog box is displayed with the file
system (mounted) information.
That concludes the explanation of the show_system_info5 function. It provides a simple way
to display the system's file system (mounted) information using the zenity command-line tool
and the mount command
# Function to exit the program
The exit_program function is responsible for displaying a farewell message in a dialog box
and then exiting the program. The zenity command is used to create an information dialog
box with the title "Exit" and the message "Exiting the program...". After displaying the dialog
box, the exit command is used to exit the script with a status code of 0, indicating successful
execution.
The main program starts with a while loop that continues indefinitely using while true. This
allows the program to repeatedly display the menu and prompt the user for options until the
program is exited.
Within the loop, the menu options are defined as an array named options. Each option
corresponds to a specific functionality.
The zenity command is used to display a dialog box that prompts the user to select an option
from the menu. The selected option is stored in the choice variable.
A case statement is used to match the selected option with the corresponding functionality. If
a valid option is chosen, the corresponding function is called.
For example, if option 1 is chosen, the show_system_info1 function is called. Similarly, for
options 2 to 5, the corresponding show_system_info functions are called. If option 6 is
chosen, the exit_program function is called, which displays the farewell message and exits
the program.
If an invalid option is chosen, an error message is displayed in a dialog box using the zenity
command.
The program then loops back to the beginning, prompting the user for another option.
That concludes the explanation of the exit_program function and the overall program. It
provides a menu-based interface using the zenity command-line tool to display system
configuration information or exit the program based on user input.
Technologies used:
Zenity is a command-line tool that enables the creation of simple GUI dialogs from shell scripts. It
provides a way to display dialog boxes for menu options and system information in the script.
uname: The uname command is used to retrieve system information, specifically the operating
system type. It is used with the -a option to display all system information.
lscpu: The lscpu command is used to retrieve detailed information about the computer's CPU. It
provides information such as the CPU architecture, vendor, model, number of cores, and more.
free: The free command is used to display memory information, including total, used, and free
memory. In the script, it is used with the -m option to display memory sizes in megabytes.
df: The df command is used to retrieve information about the available disk space on the system. The
-h option is used to display the disk space in a human-readable format.
mount: The mount command is used to display information about the currently mounted file
systems on the system. It provides details about the mounted partitions and their mount points.
The combination of Bash and Zenity allows the script to provide a user-friendly interface for
accessing and displaying system configuration information.
Download