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.

I need sudo to work, well not sudo itself but a way of allowing the sudo commands to work as described here.

This would be great however the sudo lines have extra arguments, like :

sudo -u user bash -c 'uptime'

And if I were to use the bash in the link above I simply get the output

/usr/bin/sudo: line 3: -u: command not found

Is there anyway around this? To make it run from the quote, instead of perhaps -c.

share|improve this question

2 Answers 2

up vote 1 down vote accepted

If you're sure that the command that's sent will always look exactly like

sudo -u user command...

then your fake sudo script can just throw out its first two arguments:

#!/bin/bash
shift 2
exec "$@"

Otherwise, you have to do a little argument parsing:

#!/bin/bash
while getopts :u: opt
do
  # normally you'd process options and arguments here,
  # but in this case just ignore them
done
shift $((OPTIND-1))  # throw out processed options and arguments
exec "$@"

getopts reads and returns command-line options and arguments, until there are no more. You can read about it in bash(1) (man bash) if you want to know more about how to process the command-line arguments.

share|improve this answer
    
The lines are sent remotely, and they're in that format. The user they run from is not important in this case, just need the rest to execute –  Marcus Hughes Nov 9 '11 at 10:29
    
OK, I see. Updated. –  Andrew Schulman Nov 9 '11 at 10:39

This is what I used for sudo to run Ansible under babun:

#!/bin/bash
count=0

for var in "$@"
  do
    (( count++ ))
  done

shift $count
exec "$@"
share|improve this answer
    
Maybe you can use $# for the count. # Expands to the number of positional parameters in decimal. –  Hastur Apr 25 at 12:14

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.