问题描述
我正在写的应该在Unix上,Cygwin的,和MSYS工作一个跨平台的shell脚本。在我的shell脚本我需要执行与提升权限的操作。在Unix上,你将通过须藤
做到这一点,并在的。什么是对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,但总比没有好。 (或批处理文件,为这一问题。)
Extremely hackish, but it's better than nothing. (Or Batch files, for that matter.)
这篇关于什么是须藤在MSYS等价?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!