1

I am trying to write a bash script which will switch to user and execute a program. But I am not able to execute the program after switching. This is the script I wrote

    #!/bin/bash
    sudo su
    /opt/genymobile/genymotion/genymotion
    echo "hi"
1
  • 4
    What about about sudo my_script.sh ? Commented Nov 20, 2018 at 5:57

2 Answers 2

2

sudo su gives you a new shell. while this shell is running other command are not executed. try this:

#!/bin/bash
sudo su -c '/opt/genymobile/genymotion/genymotion ; echo "hi"'
Sign up to request clarification or add additional context in comments.

Comments

2

If your script does something that requires root access the system will already check for the permissions and block access.

You can just check for the current user and abort:

if [ ! `id -u` = 0 ]; then 
 echo "ERROR: This script must be run as the root user"
fi

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.