Root user/sudo equivalent in Cygwin?

后端 未结 17 2276
旧时难觅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:02

    It seems that cygstart/runas does not properly handle "$@" and thus commands that have arguments containing spaces (and perhaps other shell meta-characters -- I didn't check) will not work correctly.

    I decided to just write a small sudo script that works by writing a temporary script that does the parameters correctly.

    #! /bin/bash
    
    # If already admin, just run the command in-line.
    # This works on my Win10 machine; dunno about others.
    if id -G | grep -q ' 544 '; then
       "$@"
       exit $?
    fi
    
    # cygstart/runas doesn't handle arguments with spaces correctly so create
    # a script that will do so properly.
    tmpfile=$(mktemp /tmp/sudo.XXXXXX)
    echo "#! /bin/bash" >>$tmpfile
    echo "export PATH=\"$PATH\"" >>$tmpfile
    echo "$1 \\" >>$tmpfile
    shift
    for arg in "$@"; do
      qarg=`echo "$arg" | sed -e "s/'/'\\\\\''/g"`
      echo "  '$qarg' \\" >>$tmpfile
    done
    echo >>$tmpfile
    
    # cygstart opens a new window which vanishes as soon as the command is complete.
    # Give the user a chance to see the output.
    echo "echo -ne '\n$0: press  to close window... '" >>$tmpfile
    echo "read enter" >>$tmpfile
    
    # Clean up after ourselves.
    echo "rm -f $tmpfile" >>$tmpfile
    
    # Do it as Administrator.
    cygstart --action=runas /bin/bash $tmpfile
    

提交回复
热议问题