X-Recipient: archive-cygwin@delorie.com
X-SWARE-Spam-Status: No, hits=-1.0 required=5.0 	tests=BAYES_00,SARE_MSGID_LONG45
X-Spam-Check-By: sourceware.org
MIME-Version: 1.0
In-Reply-To: <20100416163317.GA27371@panix.com>
References: <20100416163317.GA27371@panix.com>
Date: Sat, 17 Apr 2010 13:38:24 -0400
Received: by 10.216.172.73 with SMTP id s51mr4043400wel.113.1271525905208;  	Sat, 17 Apr 2010 10:38:25 -0700 (PDT)
Message-ID: <q2ra13b5a591004171038xff212ab7la937e1e8d2fc9220@mail.gmail.com>
Subject: Re: scripting elevated privilege on Windows 7
From: Robert Pendell <shinji@elite-systems.org>
To: cygwin@cygwin.com
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
X-IsSubscribed: yes
Mailing-List: contact cygwin-help@cygwin.com; run by ezmlm
Precedence: bulk
List-Id: <cygwin.cygwin.com>
List-Unsubscribe: <mailto:cygwin-unsubscribe-archive-cygwin=delorie.com@cygwin.com>
List-Subscribe: <mailto:cygwin-subscribe@cygwin.com>
List-Archive: <http://sourceware.org/ml/cygwin/>
List-Post: <mailto:cygwin@cygwin.com>
List-Help: <mailto:cygwin-help@cygwin.com>, <http://sourceware.org/ml/#faqs>
Sender: cygwin-owner@cygwin.com
Mail-Followup-To: cygwin@cygwin.com
Delivered-To: mailing list cygwin@cygwin.com

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
> =C2=A0 =C2=A0 =C2=A0 =C2=A0echo "Usage: elev program arg1 arg2 ..."
> =C2=A0 =C2=A0 =C2=A0 =C2=A0exit 1
> fi
> prog=3D"$1"
> shift
> exec cygstart --action=3Drunas `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.

#!/bin/bash
if [ $# -eq 0 ]
then
     echo "Usage: $0 program arg1 arg2 ..."
     exit 1
fi
prog=3D"$1"
shift
exec cygstart --action=3Drunas `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
shinji@elite-systems.org
CAcert Assurer
"A perfect world is one of chaos."

--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple

