This commit is contained in:
hhftechnologies 2024-11-25 16:05:44 +05:30
parent a0a80b8365
commit c72b22b0fa

View file

@ -1,9 +1,5 @@
#!/bin/bash #!/bin/bash
# Run this script with:
# wget https://raw.githubusercontent.com/pitterpatter22/Script-Repo/main/Testing/test.sh --no-check-certificate && sudo bash script-template.sh
#-----------------------------------# #-----------------------------------#
# VARIABLES # # VARIABLES #
#-----------------------------------# #-----------------------------------#
@ -20,17 +16,36 @@ success=0
USER_TO_RUN_AS="${1:-$SUDO_USER}" USER_TO_RUN_AS="${1:-$SUDO_USER}"
USER_HOME=$(eval echo ~$USER_TO_RUN_AS) USER_HOME=$(eval echo ~$USER_TO_RUN_AS)
#-----------------------------------# #-----------------------------------#
# FORMATTER # # FORMATTER #
#-----------------------------------# #-----------------------------------#
# Download and source the formatter with error handling
download_formatter() {
if [ ! -f "task_formatter.sh" ]; then if [ ! -f "task_formatter.sh" ]; then
wget $formatter_url --no-check-certificate -O task_formatter.sh > /dev/null 2>&1 if ! wget "$formatter_url" --no-check-certificate -O task_formatter.sh > /dev/null 2>&1; then
echo "Error: Failed to download task_formatter.sh"
exit 1
fi
fi fi
source ./task_formatter.sh if [ ! -f "task_formatter.sh" ]; then
echo "Error: task_formatter.sh not found after download attempt"
exit 1
fi
# Make the formatter executable
chmod +x task_formatter.sh
# Source the formatter with error checking
if ! source ./task_formatter.sh; then
echo "Error: Failed to source task_formatter.sh"
exit 1
fi
}
# Call the download_formatter function
download_formatter
#-----------------------------------# #-----------------------------------#
# FUNCTIONS # # FUNCTIONS #
@ -49,6 +64,7 @@ update_upgrade_packages() {
example_function_1() { example_function_1() {
echo -e "This is example function 1. $CHECK_MARK" echo -e "This is example function 1. $CHECK_MARK"
sleep 2 sleep 2
return 0
} }
# Example function 2 # Example function 2
@ -57,6 +73,7 @@ example_function_2() {
sleep 2 sleep 2
echo -e "Done $CHECK_MARK" echo -e "Done $CHECK_MARK"
sleep 2 sleep 2
return 0
} }
# Example function 3 with an error # Example function 3 with an error
@ -85,48 +102,30 @@ remove_script() {
rm task_formatter.sh rm task_formatter.sh
fi fi
echo -e "Cleaned up $CHECK_MARK" echo -e "Cleaned up $CHECK_MARK"
return 0
} }
# Remove created files on Failure # Remove created files on Failure
cleanup_files() { cleanup_files() {
echo -e "Cleaned up $CHECK_MARK" echo -e "Cleaned up $CHECK_MARK"
return 0
} }
#-----------------------------------# #-----------------------------------#
# MAIN LOGIC # # MAIN LOGIC #
#-----------------------------------# #-----------------------------------#
''' # Check if print_header function exists
Run functions with this format: if ! command -v print_header >/dev/null 2>&1; then
echo "Error: print_header function not found. Formatter may not be properly sourced."
format_output {function_name} "Function Description" exit 1
Example:
format_output update_upgrade_packages "Update and Upgrade Packages"
To add in success monitoring when script is run from master.sh, run like this:
if ! format_output {function_name} "Function Description"; then
cleanup_files
success=1
fi fi
if ! format_output {function_name} "Function Description"; then
cleanup_files
success=1
fi
'''
# Print header # Print header
print_header "$this_script_name" "$this_script_url" print_header "$this_script_name" "$this_script_url"
echo -e "Running as User: $USER_TO_RUN_AS\nUser Home: $USER_HOME\n" echo -e "Running as User: $USER_TO_RUN_AS\nUser Home: $USER_HOME\n"
# Run the functions with formatted output # Run the functions with formatted output
if ! format_output example_function_1 "Example Function 1"; then if ! format_output example_function_1 "Example Function 1"; then
cleanup_files cleanup_files
@ -150,6 +149,12 @@ fi
format_output remove_script "Cleaning up" format_output remove_script "Cleaning up"
# Check if final_message function exists
if ! command -v final_message >/dev/null 2>&1; then
echo "Error: final_message function not found. Formatter may not be properly sourced."
exit 1
fi
# Print final message # Print final message
final_message "$this_script_name" $success final_message "$this_script_name" $success