什么是须藤在MSYS等价? [英] What's the equivalent of sudo in msys?

查看:193
本文介绍了什么是须藤在MSYS等价?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写的应该在Unix上,Cygwin的,和MSYS工作一个跨平台的shell脚本。在我的shell脚本我需要执行与提升权限的操作。在Unix上,你将通过须藤做到这一点,并在的 Cygwin的通过类似 cygstart --action =运行方式 。什么是对MSYS等价?

I'm writing a cross-platform shell script that's supposed to work on Unix, Cygwin, and msys. In my shell script I need to perform actions with elevated privileges. On Unix you would do this via sudo, and on Cygwin via something like cygstart --action=runas. What's the equivalent for msys?

我所有的谷歌搜索至今只止跌回升,这是不实际的距离一个shell脚本,因为你必须与GUI交互。

All my Googling so far has only turned up this, which isn't practical from a shell script since you have to interact with the GUI.

推荐答案

我想我可能已经找到使用PowerShell的一个解决方案:

I think I may have found a solution using PowerShell:

escape()
{
    RESULT="$1"
    RESULT="${RESULT/\'/\\\'\'}" # replace ' with \''
    RESULT="${RESULT/\"/\\\\\\\"}" # replace " with \\\"
    echo "''$RESULT''" # PowerShell uses '' to escape '
}

sudo()
{
    ESCAPED=()
    for ARG in "$@"
    do
        ESCAPED+=($(escape "$ARG"))
    done

    SHELL_PATH=$(cygpath -w $SHELL)
    PS_COMMAND="[Console]::In.ReadToEnd() | Start-Process '$SHELL_PATH' '-c -- \"${ESCAPED[*]}\"' -Verb RunAs"
    cat /dev/stdin | powershell -NoProfile -ExecutionPolicy Bypass "$PS_COMMAND"
}

绝对是位非常的hackish,但总比没有好。 (或批处理文件,为这一问题。)

Definitely a bit Extremely hackish, but it's better than nothing. (Or Batch files, for that matter.)

这篇关于什么是须藤在MSYS等价?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆