Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 使用sudo别名启动午夜指挥官'mc',并保留路径_Bash_Ubuntu 18.04_Mc - Fatal编程技术网

Bash 使用sudo别名启动午夜指挥官'mc',并保留路径

Bash 使用sudo别名启动午夜指挥官'mc',并保留路径,bash,ubuntu-18.04,mc,Bash,Ubuntu 18.04,Mc,在退出sudo-mc(要求编号4)时,是否可以用sudo启动mc包装程序并仍然使用控制台上最后选择的目录?我的默认别名如下所示 alias mc='EDITOR="${EDITOR-mcedit}" . /usr/lib/mc/mc-wrapper.sh' 一些错误(针对谷歌人) 我的要求 Ubuntu 18.04.1 别名应可用于sudo调用,也可不用于sudo调用 如果可能,为所有用户提供/etc/bash.bashrc中的mc的单个别名 关闭Midnight Commander后,应“

在退出
sudo-mc
(要求编号4)时,是否可以用sudo启动mc包装程序并仍然使用控制台上最后选择的目录?我的默认别名如下所示

alias mc='EDITOR="${EDITOR-mcedit}" . /usr/lib/mc/mc-wrapper.sh'
一些错误(针对谷歌人) 我的要求
  • Ubuntu 18.04.1
  • 别名应可用于sudo调用,也可不用于sudo调用
  • 如果可能,为所有用户提供
    /etc/bash.bashrc
    中的
    mc
    的单个别名
  • 关闭Midnight Commander后,应“保留”您使用sudo mc更改的目录。这意味着您将不会与启动的
    sudo mc
    位于同一目录中(前提是它不是同一目录)
可选要求
  • 查看别名是否以超级权限启动
  • 查看别名是否以sudo开头
  • 如果别名
    mc
    是在没有超级电源或sudo的情况下启动的,请询问程序
    mc
    是否仍应使用sudo权限启动
  • 如果问题回答为否,请使用我的默认别名
  • 在所有其他情况下,应满足前四项要求
  • mc
    中的编辑器(例如
    mcedit
    vi
    )应可通过另一个别名选择,如
    mcvi
    (用于
    vi
    ),而无需代码重复
  • 参数应该传递给程序,比如
    sudomc/opt//mnt/
    • 这里有一个黑客解决方案(经过测试,但最后两个可选要求仍然缺失)

      /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)
      ),而且我认为这也没那么容易

      没有留下评论的否决票就像没有否决票一样。我认为这个问题对我有帮助(如果我将来遇到同样的问题;),因此很可能对其他人也有帮助。
      sudo
      需要一个真正的可执行文件,而不是别名或shell函数。不,请参阅我的答案。您不能保留由
      mc
      所做的目录更改,没有在退出
      mc
      之前将当前目录的路径保存在文件中,然后让脚本在退出
      mc
      之后重新读取该文件以更改目录。否,请参阅我的答案。)
      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:
      '