使用sudo别名启动Midnight Commander`mc`并保留路径

时间:2018-11-29 14:55:28

标签: bash ubuntu-18.04 mc

是否可以用sudo启动mc-wrapper并在退出sudo mc时仍使用控制台上最后选择的目录(需求编号4)?我的默认别名看起来像这样。

alias mc='EDITOR="${EDITOR-mcedit}" . /usr/lib/mc/mc-wrapper.sh'

某些错误(针对Google员工)

sudo: mc: command not found
sudo: .: command not found  # "." == "source"

我的要求

  • Ubuntu 18.04.1。
  • 别名应在有和没有sudo调用的情况下都可以使用。
  • 如有可能,请为所有用户在mc中为/etc/bash.bashrc提供一个别名。
  • 在关闭Midnight Commander之后,应该用“ sudo mc”更改为目录。这意味着您将不会与开始sudo mc的目录位于同一目录中(前提是它不是同一目录)。

可选要求

  • 查看别名是否以超级能力开头。
  • 查看别名是否以sudo开头。
  • 如果别名mc是在没有超级权限或sudo的情况下启动的,请询问程序mc是否仍应以sudo权限启动。
  • 如果回答为“否”,请使用我的默认别名。
  • 在所有其他情况下,都应满足前四个要求。
  • mcedit中的编辑器(例如vimc)应该可以通过其他别名(例如mcvi(对于vi)进行选择,而无需代码重复。 / li>
  • 参数应传递给程序,例如sudo mc /opt/ /mnt/

1 个答案:

答案 0 :(得分:0)

这是一个hacky解决方案(经过测试,但最后两个可选要求仍然缺失)。

/etc/bash.bashrc

alias sudo='sudo '  # fixes "sudo: mc: command not found" (breaks with an argument: sudo -H ll)

# sudo apt update && sudo apt install dialog mc pwgen
#
# Start the alias with a "real" command (instead of a shell keyword like "if") so that sudo is not confused.
# This first command (env) will eat sudo and all arguments! Even the following file redirection including the angle bracket is already executed without sudo.
alias mc='env > "/tmp/mc.env.$(whoami).$$"
MC_USER="$(whoami)"
MC_ENV_FILE="/tmp/mc.env.${MC_USER}.$$"
# cat "${MC_ENV_FILE}"
if [ "$(cat "${MC_ENV_FILE}" | grep "$(id -nu 0)" | wc -l)" -gt "3" ]; then
    # echo "This alias was called with super powers."
    MC_ROOT="root"
fi
if [ "$(cat "${MC_ENV_FILE}" | grep "^SUDO_" | wc -l)" -gt "3" ]; then
    # echo "This alias was called with sudo (possibly sudo -i or sudo -s was entered before)."
    MC_SUDO="sudo"
fi
if [ "${MC_ROOT}" == "root" ] || [ "${MC_SUDO}" == "sudo" ]; then
    MC_DIALOG="0"
else
    # echo "This alias was called as normal user."
    dialog --cr-wrap --defaultno --title "sudo mc" --yesno "Do you want super powers (sudo/root)?\n\n(Alternatively you can use \"sudo mc\" directly next time.)\n\nAgain: Do you want super powers (sudo/root)?" 9 64
    MC_DIALOG="$?"
    tput reset
fi
if [ "${MC_DIALOG}" != "0" ]; then
    # echo "No, do not use sudo and stay normal user."
    # echo "Standard wrapper (without arguments)..."
    EDITOR="${EDITOR-mcedit}" . /usr/lib/mc/mc-wrapper.sh  # does not work with sudo because "." is not a program like "ls" or "grep"!
else
    # echo "Yes, exec those decisive commands with super powers."
    # echo "Custom wrapper (also without arguments)..."
    MC_PWD_FILE_DIRNAME="${TMPDIR-/tmp}/mc-${MC_USER}/"
    MC_PWD_FILE="${MC_PWD_FILE_DIRNAME}mc.pwd.$$.$(pwgen 13 1)"
    sudo mkdir -p "$MC_PWD_FILE_DIRNAME"
    sudo chown "$(sudo whoami)":"$(sudo whoami)" "$MC_PWD_FILE_DIRNAME"
    sudo chmod 0700 "$MC_PWD_FILE_DIRNAME"
    sudo EDITOR="${EDITOR-mcedit}" /usr/bin/mc -P "$MC_PWD_FILE"
    sudo chown -R "$MC_USER":"$MC_USER" "$MC_PWD_FILE_DIRNAME"
    if test -r "$MC_PWD_FILE"; then
        MC_PWD=$(cat "$MC_PWD_FILE")
        if test -n "$MC_PWD" && test -d "$MC_PWD"; then
            cd "$MC_PWD"
        fi
        unset MC_PWD
    fi
    rm -f "$MC_PWD_FILE"
    unset MC_PWD_FILE
    unset MC_PWD_FILE_DIRNAME
fi
unset MC_DIALOG
unset MC_SUDO
unset MC_ROOT
rm -f "${MC_ENV_FILE}"
unset MC_ENV_FILE
unset MC_USER
# This terminating line break is required:
'

我没有设法使用函数mcwrapper(和$(declare -f mcwrapper)),我也不认为这很容易!?