No Linux No Life – Use a Function to Receive User Input for rm command

User input for rm command.

Another day, another bash script.

Allow me to share a valuable skill that holds practical relevance in the real world’s bash scripting. Think about a situation where you want to delete a file owned by another user, say root. If you wish to alert the user before deleting the file with a pop-up message in your program, you can utilize a function. Considering the potentially hazardous nature of the rm command, which deletes files without confirmation, it is essential to incorporate additional precautions and user prompts to ensure safe file deletion.

This is precisely where the utilization of a function becomes crucial. The reason for implementing the “delete_file()” function is due to the inherent limitation of the rm command, which does not directly support user input within the scripting context. By encapsulating the file deletion process within a function, we can incorporate the necessary user prompts and ensure a safer and more controlled deletion procedure.

#!/bin/bash

if [[ $# -lt 1 ]]; then
    echo "Error: At least one argument is required."
    exit 1
fi

# Delete file function
delete_file() {
    # Add your delete file command here
    # For example: rm filename.txt
    rm "$1"
}

arg1=$1

# Prompt user for confirmation
read -p "Do you want to delete the file? (y/n): " answer

# Branch the process based on the user's input
if [[ "$answer" == "y" || "$answer" == "Y" ]]; then
    output=$(delete_file ${arg1} 2>&1)  # Execute delete_file command and capture output

    # Check if the output contains the "Permission denied" message
    if [[ "$output" == *"Permission denied"* ]]; then
        echo "You don't have permission to delete the file."
    else
        echo "File deleted successfully."
    fi
else
    echo "File deletion canceled."
fi

The description:

This code is a bash script that prompts the user to delete a specified file. It checks if the user has provided at least one argument (file name) when running the script. If no argument is provided, it displays an error message and exits.

The script defines a function called delete_file() that is responsible for deleting the file. In this example, the rm command is used to delete the file, and the function takes the file name as an argument.

Afterward, the script prompts the user for confirmation to delete the file, expecting a ‘y’ or ‘n’ response. Based on the user’s input, the script branches to different sections. If the user confirms deletion, the delete_file() function is called with the provided argument (file name). The output of the function is captured, and if the output contains the “Permission denied” message, it informs the user that they don’t have permission to delete the file. Otherwise, it displays a success message indicating that the file was deleted.

If the user chooses not to delete the file or cancels the deletion, the script displays a message indicating that the file deletion was canceled.

Leave a Reply