Shell script - Sudo-permissions lost over time

后端 未结 6 1827
情歌与酒
情歌与酒 2020-12-09 00:22

I\'ve made a simple bash script that need to keep it\'s super-user privileges throughout the script. Unfortunately, but understandable the script looses its sudo

6条回答
  •  离开以前
    2020-12-09 01:00

    This my way:

    #!/bin/sh
    echo "Working..."
    # add you pass
    echo "yourpass" >> file.pass ;sleep 5 
    # Check if root
    if [ `cat file.pass | sudo -S su root -c whoami` != "root" ]; then
    echo "Not running as root. Exiting..."
    sleep 2
    echo "Cleaning..."
    sleep 1
    srm file.pass
    echo "Cleaned"
    exit 0
    else
    echo "Running as root. Good"
    sleep 2
    # and run any sudo with
    cat file.pass | sudo -S su root -c ls #
    fi
    
    sleep 5
    echo `cat file.pass | sudo -S su root -c whoami` "say bay bay"
    # if pass no longer need
    srm file.pass
    echo "End session :)"
    exit 0
    

提交回复
热议问题