This is the mail archive of the cygwin mailing list for the Cygwin project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: scripting elevated privilege on Windows 7

On Fri, Apr 16, 2010 at 12:33 PM wrote:
> Thank you Robert Pendell! I wrote this shell script. Any suggestions
> for optimization?
> #!/bin/bash
> if [ $# -eq 1 ]
> then
> Â Â Â Âecho "Usage: elev program arg1 arg2 ..."
> Â Â Â Âexit 1
> fi
> prog="$1"
> shift
> exec cygstart --action=runas `which "$prog"` "$@"

It looks fine (beyond the quoting) but there is an error.

In your if test you check the number of parameters and check to see if
there is exactly one then throw the message.  It should be zero rather
than one because it doesn't count the current process name.

On that note I might suggest replacing 'elev' in the Usage text with
$0 so it can reflect the file it was called as.

On and while quoting works there are better ways as Eric had already
suggested.  I didn't bother changing it but the following script works
well and is tested.

if [ $# -eq 0 ]
     echo "Usage: $0 program arg1 arg2 ..."
     exit 1
exec cygstart --action=runas `which "$prog"` "$@"

On that note if you plan on reusing the script on xp or older (for
portability) then you might want to check the platform os so that you
don't use that action setting on them.  The runas action is only valid
on Windows Vista and Windows 7 since that is when it was added.

Robert Pendell
CAcert Assurer
"A perfect world is one of chaos."

Problem reports:
Unsubscribe info:

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]