Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

How can I make custom functions accessible to the sudo command?

I have my Mac OS X set up with Bootcamp, but also with VirtualBox so I can run Windows for testing websites in IE. I have to run this sequence of commands to be able to boot Windows in VirtualBox:

sudo chmod 777 /dev/disk0s4
sudo chown myUserAccount bootcamp.vmdk bootcamp-pt.vmdk
sudo umount /Volumes/BOOTCAMP
VBoxManage list vms
VBoxManage startvm <uid>

It's a pain in the butt having to do this, so I'd like to automate it. I've created a file called bootcamp.sh in a personal development folder I keep. I have tried to include the following in several files:

. /Users/myUserAccount/Development/Bash/bootcamp.sh

I have tried including it in:

  • My personal .bash_profile (not gonna work obviously)
  • /etc/bashrc
  • /private/etc/bashrc
  • /private/etc/profile.

I have also tried to create a symlink in /usr/bin/ called bootcamp (without extension) and that worked partially, allowing me to do:

sudo sh bootcamp

But it still annoys me having to write sh in there. I tried including a shebang (#! /usr/bin/sh) in the top of the bootcamp.sh file, but without luck.

Thanks in advance!

share|improve this question

2 Answers 2

up vote 1 down vote accepted

If the shebang is the correct path, then you just need to make the script executable to avoid needing the sh.

chmod +x /Users/myUserAccount/Development/Bash/bootcamp.sh

And provided /Users/myUserAccount/Development/Bash/ is in the path, you can just do

sudo bootcamp.sh

You don't really need the .sh suffix either, you can rename it to just bootcamp.

The downside of this is that you will be running VBoxManage as root. So I would put the commands

sudo chmod 777 /dev/disk0s4
sudo chown myUserAccount bootcamp.vmdk bootcamp-pt.vmdk
sudo umount /Volumes/BOOTCAMP
VBoxManage list vms
VBoxManage startvm <uid>

into the bootcamp.sh file as they are. Then run it as

bootcamp.sh

You'll be prompted for the first sudo to provide the password, but this means that the VBox commands will be run as a normal user.

share|improve this answer
    
Great, worked like a charm! If I'd just known about chmod +x <file>! Too I didn't consider just keeping the sudo's in the script. Thanks! –  Tixz Dec 26 '11 at 22:46

You have to make the file executable with

chmod +x bootcamp.sh

Also, you don't need sudo inside the script, if you use sudo to call the script.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.