using sudo in shell?

Ben Rubinstein benr_mc at cogapp.com
Mon Jun 26 07:04:15 EDT 2006


On 26/6/06 10:57, Sarah Reichelt wrote:
> On 6/26/06, Ben Rubinstein <benr_mc at cogapp.com> wrote:
>> The 'shell' command is a very useful extension to Rev's native powers.
>> However, there are some things, at least on OS X, which I want to do that
>> require the shell command to be issued in super-user mode.
>>
>> Is there any way to make this happen?   If you are working in the 
>> terminal,
>> you can issue the command as "sudo <x>" and you will be asked for the
>> password.  Prefix your parameter to "shell" with "sudo", and the 
>> result will
>> be the single line prompt "Password:".
>>
>> I don't even know if this is a Rev question, or a bash question, or an 
>> OS X
>> question.  But does anybody know of a way that one can in effect pass a
>> password to shell in this situation, or in any other way get round 
>> this problem?
> 
> Hi Ben,
> 
> Here is how I do it (this example sets the system clock):
> 
> put "#!/bin/sh" & cr into tScript
> put "pw=" & quote & tPass & quote & cr after tScript
> put "echo $pw | sudo -S date " & tDate & tTime & cr after tScript
> -- build the command lines, the command you need to run goes after the -S
> put shell(tScript) into tCheck  -- do the command & get the result
> 
> You have to quote your admin password, but you can ask for that when
> running the script, or store it in a custom property.

Thanks Sarah, that's excellent.  Completely solves my problem.

Playing around, I note that you can even use your trick as a oneliner, eg

	put "echo" && quote & tPass & quote & "| sudo -S" into tSudoPrefix
	put shell(tSudoPrefix && tCommand)

with the only apparent difference being that in the full version, the first 
line of the result is "Password:", whereas when I do it all one line, that 
password prompt doesn't appear.  Do you know if the one line version should be 
considered less reliable in some way?

Many thanks,

   Ben Rubinstein               |  Email: benr_mc at cogapp.com
   Cognitive Applications Ltd   |  Phone: +44 (0)1273-821600
   http://www.cogapp.com        |  Fax  : +44 (0)1273-728866




More information about the use-livecode mailing list