Because there's no sudo command in Cygwin, scripts that I want to run fail with

./install.sh: line N: sudo: command not found

What's the standard way for getting around this? Editing the scripts to remove sudo? Getting some sudo-like tool for windows?

link|improve this question

feedback

migrated from stackoverflow.com Mar 21 '10 at 17:54

This question came from our site for professional and enthusiast programmers.

3 Answers

up vote 12 down vote accepted

One way is to create a fake "sudo" command with the following content:

#!/usr/bin/bash

"$@"

This will allow the install.sh to continue, because sudo is found.

This doesn't elevate privileges like real sudo does. If you really need elevated privileges start cygwin shell with from an account with administrative privileges (XP) or r-click on cygwin.bat and "run as administrator" (Vista,Win7)

link|improve this answer
3  
Just out of curiosity from someone who doesn't speak fluent bash: Why does this work? The manpage doesn't say anything about $@ doing anything sudo-like. Instead it's just all arguments to the script. And wouldn't the quotes around it be superfluous in that case? Otherwise if you'd do a sudo foo bar then it tries executing "foo bar" as a single command which probably doesn't exist given that irrational fear of spaces on UNIX-like systems. – Joey Mar 23 '10 at 10:29
4  
@Johannes: "$@" (when double-quoted) works differently from "$*": it expands to a separate word for every positional variable. Example: If $1 == "foo bar" and $2 == "baz", then "$@" is "foo bar" baz - one word for each parameter (unlike "$*", which results in "foo bar baz" as one word). See manual of bash, section Parameters, subsection Special parameters. The end result of Peon's script is that it executes its arguments exactly as they were passed. – grawity Mar 23 '10 at 15:22
Ah, ok. And where does the sudo part come in? Above snippet doesn't do anything remotely in that direction, right? – Joey Mar 23 '10 at 15:38
@Johannes: In Unix, a real sudo would raise privileges from mortal to root before running the command. In Cygwin, there is no such thing, so Peon's fake script (which you're supposed to name sudo) just runs the command directly without changing its privileges. (This means you may need to run ./install.sh as Administrator.) – grawity Mar 23 '10 at 15:49
1  
@grawity: runas should work, it doesn't rely on UAC and prompts for a password by itself. I was just confused why the script in the answer apparently didn't do what the name implied which I assumed was the goal. Sorry for my stupidity ;-) – Joey Mar 23 '10 at 17:12
show 1 more comment
feedback

Sudo(Elevate) for Windows™

I do a lot of work on the command line in Windows™.
In cygwin itself i believe you can run a root command with su -c /the/cmd as for sudo itself within Windows™ file-system elevating the users permissions from the command line, If you are an Administrator this will work great for you otherwise use runas and get admin's pass ;).
Now I cannot remember where we got this code but here it is. Hope it helps.

BTW. package we use to compile this was gcc-mingw32


$ i586-mingw32msvc-gcc sudo.c -o sudo.exe
# put sudo.exe in /usr/bin or in your windows path (%homedrive%\windows)
#example:
$ sudo vi /cygdrive/c/windows/system32/drivers/etc/hosts

/**
* (sudo for Windows™)
* @filename sudo.c
*/
#ifndef UNICODE
#define UNICODE
#endif
#include <windows.h>
#include <shellapi.h>
#include <wchar.h>
LPWSTR *mergestrings(LPWSTR *left, LPCWSTR right)
{
    size_t size = ( 1 + lstrlen(*left) + lstrlen(right) ) * sizeof(LPWSTR*);
    if ( *left ) {
        LPWSTR leftcopy = _wcsdup(*left);
        *left = (LPWSTR)realloc(*left, size);
        *left = lstrcpy(*left, leftcopy);
        *left = lstrcat(*left, right);
        free( leftcopy );
    } else *left = _wcsdup(right);
    return left;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE prevInstance, LPSTR lpcommand, int nShowCmd)
{
    DWORD result = 0x2a;
    LPWSTR *argv = NULL;
    int argc = 0;
    if ( argv = CommandLineToArgvW(GetCommandLineW(), &argc) ) {
        if ( argc < 2 ) {
            LPWSTR usagemsg = NULL;
            usagemsg = *mergestrings(&usagemsg, argv[0]);
            usagemsg = *mergestrings(&usagemsg, TEXT(" <command_to_run> [arguments]"));
            MessageBox(NULL, usagemsg, TEXT("Usage:"), MB_OK | MB_ICONEXCLAMATION );
            LocalFree( argv );
            free( usagemsg );
            return ERROR_BAD_ARGUMENTS;
        } else {
            LPWSTR command = argv[1];
            LPWSTR arguments = NULL;
            int c;
            for ( c = 2; c < argc; c++ ) {
                arguments = *mergestrings(&arguments, argv[c]);
                arguments = *mergestrings(&arguments, TEXT(" "));
            }
            result = (DWORD)ShellExecute(NULL, TEXT("runas"), command, arguments, NULL, SW_SHOWNORMAL);
            LocalFree( argv );
            if ( arguments ) free( arguments );
            switch ( result )
            {
                case 0:
                    result = ERROR_OUTOFMEMORY; 
                    break;

                case 27:
                case 31: 
                    result = ERROR_NO_ASSOCIATION;
                    break;

                case 28:
                case 29:
                case 30: 
                    result = ERROR_DDE_FAIL;
                    break;
                case 32:
                    result = ERROR_DLL_NOT_FOUND;
                    break;
                default:
                    if ( result > 32 ) result = 0x2a;
            }
        }
    } else result = GetLastError(); 
    if (result != 0x2a) {
        LPWSTR errormsg = NULL;
        FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER,
                      NULL, result, 0, (LPWSTR)&errormsg, 0, NULL);
        MessageBox(NULL, errormsg, TEXT("Error:"), MB_OK | MB_ICONERROR);
        LocalFree( errormsg );
        return result;
    }  else return NO_ERROR;
}

link|improve this answer
feedback

Windows 7: Launch Cygwin.bat by right-click and "Run as administrator".

link|improve this answer
2  
-1: OP needs his install.sh to be able to execute a command named "sudo"; he is not looking for the privilege-escalation functionality. – Dave Sherohman Apr 12 '11 at 9:28
feedback

Your Answer

 
or
required, but never shown

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