Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· OpenAIAi2 WildChat-4.8M (AI2)

Write a bash script to allow oracle user to run script with root permission that…

  1. User

    Write a bash script to allow oracle user to run script with root permission that includes other scripts in it

  2. Assistant

    Here's an example of a bash script that allows the oracle user to execute a script with root permissions while including other scripts within it:

    #!/bin/bash
    
    # Check if the script is executed by the oracle user
    if [ "$USER" != "oracle" ]; then
        echo "This script can only be run by the oracle user."
        exit 1
    fi
    
    # Prompt for root password
    read -s -p "Enter root password: " ROOT_PASSWORD
    echo
    
    # Run the script with root permissions
    echo $ROOT_PASSWORD | sudo -S sh -c '
        # Include other scripts
        source /path/to/your/other-script.sh
    
        # Root-level operations
        # ...
    
        # Exit with appropriate status
        exit 0
    '
    

    In the above script, the first few lines check if the script is being executed by the oracle user. If not, it displays an error message and exits.

    Next, the script prompts the user for the root password using the read command and captures it in the ROOT_PASSWORD variable.

    Later, the script runs the desired script with root permissions using the sudo command. It includes the -S flag to read the password from standard input and the -c flag to execute the specified command as root. Within the command, you can include other scripts by sourcing them (source /path/to/your/other-script.sh in the example).

    Please make sure to replace /path/to/your/other-script.sh with the actual path to the other scripts that you want to include.

    Keep in mind that granting scripts with root permissions should be done carefully and only when necessary. Ensure that the scripts being included or executed as root are trustworthy and do not pose a security risk to your system.