简体   繁体   English

是否有命令检查 Git Bash 是否以管理员模式打开?

[英]Is there a command to check if Git Bash is opened in Administrator mode?

Let's say you opened a Git Bash console and used it for a time but you forgot if you opened it as Administrator or not.假设您打开了一个 Git Bash 控制台并使用了一段时间,但您忘记了是否以管理员身份打开它。

Is there a way to check this in the current console, without closing and opening it again?有没有办法在当前控制台中检查它,而无需关闭并再次打开它?

Adapted from the thread on how to check for admin rights , run the following:改编自关于如何检查管理员权限的线程,运行以下命令:

if [[ $(sfc 2>&1 | tr -d '\0') =~ SCANNOW ]]; then echo Administrator; else echo $USERNAME; fi

If you are running git-bash as Administrator, it will print Administrator , otherwise it will print your username.如果您以管理员身份运行git-bash ,它将打印Administrator ,否则将打印您的用户名。

My PS1 environmental variable looks like this:我的 PS1 环境变量如下所示:

\[\033]0;$TITLEPREFIX:$PWD\007\]\n\[\033[32m\]\u@\h \[\033[35m\]$MSYSTEM \[\033[33m\]\w\[\033[36m\]`__git_ps1`\[\033[0m\]\n$

So off the back of the answer from @daveloyall.所以在@daveloyall 的回答后面。 I've put this into my .bash_profile我已将其放入我的 .bash_profile

if [[ $(sfc 2>&1 | tr -d '\0') =~ SCANNOW ]]; then
    export TITLEPREFIX='ADMIN' 
fi

This then prefixes the window title with ADMIN if you've opened git-bash as administrator.如果您以管理员身份打开了 git-bash,那么这会在窗口标题前加上ADMIN

A reliable way to check if git-bash on Windows is running as admin privilege:检查 Windows 上的 git-bash 是否以管理员权限运行的可靠方法:

#!/bin/bash

net session 1>/dev/null 2>&1 || printf >&2 "error: please run this script as administrator.\n"

If you want to take into consideration that there might be a Windows which has no net command (I doubt that), you can check for it:如果您想考虑到可能有一个没有net命令的 Windows(我对此表示怀疑),您可以检查它:

#!/bin/bash

if type -fq net >/dev/null
then
    net session 1>/dev/null 2>&1 || printf >&2 "error: please run this script as administrator.\n"
fi

You can try a linux command, like that:您可以尝试使用 linux 命令,如下所示:

whoami

This will return your system username这将返回您的系统用户名

or windows command:或 windows 命令:

ECHO %USERNAME%

Yes, You can check the current username by using this command:是的,您可以使用以下命令检查当前用户名:

git config user.name git config 用户名

This will return the username and you can know if it is the Administrator.这将返回用户名,您可以知道它是否是管理员。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM