-1

是否可以使用 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 调用。
  • 如果可能,为所有用户提供一个mcin别名。/etc/bash.bashrc
  • 关闭 Midnight Commander 后,您更改的目录sudo mc应该被“保留”。这意味着您将不会在您开始时位于同一目录中sudo mc(前提是它不是同一目录)。

可选要求

  • 查看别名是否以超能力开始。
  • 查看别名是否以 sudo 开头。
  • 如果别名mc是在没有超级权限或 sudo 的情况下启动的,请询问程序mc是否仍应以 sudo 权限启动。
  • 如果问题的答案为否,请使用我的默认别名。
  • 在所有其他情况下,应满足前四个要求。
  • 内部的编辑器(例如mceditvimc应该可以通过另一个别名(例如mcvi(for vi))进行选择,而无需重复代码。
  • 参数应该传递给程序,比如sudo mc /opt/ /mnt/
4

1 回答 1

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 (and $(declare -f mcwrapper)),我也不认为这很容易!?

于 2018-11-29T14:55:27.853 回答