Root user/sudo equivalent in Cygwin?

后端 未结 17 2279
旧时难觅i
旧时难觅i 2020-12-04 04:23

I\'m trying to run a bash script in Cygwin.

I get Must run as root, i.e. sudo ./scriptname errors.

chmod 777 scriptname does no

17条回答
  •  悲哀的现实
    2020-12-04 05:28

    Based on @mat-khor's answer, I took the syswin su.exe, saved it as manufacture-syswin-su.exe, and wrote this wrapper script. It handles redirection of the command's stdout and stderr, so it can be used in a pipe, etc. Also, the script exits with the status of the given command.

    Limitations:

    • The syswin-su options are currently hardcoded to use the current user. Prepending env USERNAME=... to the script invocation overrides it. If other options were needed, the script would have to distinguish between syswin-su and command arguments, e.g. splitting at the first --.
    • If the UAC prompt is cancelled or declined, the script hangs.

    .

    #!/bin/bash
    set -e
    
    # join command $@ into a single string with quoting (required for syswin-su)
    cmd=$( ( set -x; set -- "$@"; ) 2>&1 | perl -nle 'print $1 if /\bset -- (.*)/' )
    
    tmpDir=$(mktemp -t -d -- "$(basename "$0")_$(date '+%Y%m%dT%H%M%S')_XXX")
    mkfifo -- "$tmpDir/out"
    mkfifo -- "$tmpDir/err"
    
    cat >> "$tmpDir/script" <<-SCRIPT
        #!/bin/env bash
        $cmd > '$tmpDir/out' 2> '$tmpDir/err'
        echo \$? > '$tmpDir/status'
    SCRIPT
    
    chmod 700 -- "$tmpDir/script"
    
    manufacture-syswin-su -s bash -u "$USERNAME" -m -c "cygstart --showminimized bash -c '$tmpDir/script'" > /dev/null &
    cat -- "$tmpDir/err" >&2 &
    cat -- "$tmpDir/out"
    wait $!
    exit $(<"$tmpDir/status")
    

提交回复
热议问题