如何打印bash的真实执行路径,例如类型-f类似于-a?

时间:2022-07-11 21:44:57

I imagine to invoke type with the option -f (follow) akin to -a (all).

我想用类似于-a(全部)的选项-f(跟随)来调用类型。

Here are my questions:

这是我的问题:

  1. Is there a Bash built-in to print how bash would execute a command?
  2. 是否内置了Bash来打印bash如何执行命令?

  3. Is there a Linux utility to print how any shell would execute a command?
  4. 是否有Linux实用程序来打印任何shell如何执行命令?

  5. Can the below shell function I'm using be simplified?
  6. 我正在使用的下面的shell函数可以简化吗?

Given the following definitions and /usr/local/bin/ls being a link to /usr/bin/ls:

给定以下定义,/ usr / local / bin / ls是/ usr / bin / ls的链接:

alias ls="\ls -h --color=auto"
alias lsa="ls -A"
alias lsh="lsa -I'*'"

rcommand lsh prints:

rcommand lsh打印:

alias lsh='lsa -I'\''*'\'''
alias lsa='ls -A'
alias ls='\ls -h --color=auto'
link /usr/local/bin/ls
file /usr/bin/ls

Here is my shell function I defined in my .bashrc file:

这是我在.bashrc文件中定义的shell函数:

function rcommand {
  declare -r a="${1#\\}"
  declare -r b="$(type -t "$a")"
  if [[ "$b" == alias && "$a" == "$1" ]]; then
    declare -r c="$(command -v "$a")"
    echo "$c"
    declare -r d="$(echo "$c" | sed "s/^.*='\\\\\\?\(\w\+\).*$/\1/")"
    if [[ "$d" == "$a" ]]; then
      rcommand  "\\$d"
    else
      rcommand  "$d"
    fi
  elif [[ "$b" == builtin || "$b" == function || "$b" == keyword ]]; then
    echo "$b $a"
  else
    declare -r c="$(declare -F "$a")"
    if [[ "$c" == "$a" ]]; then
      echo "function $a"
    else
      declare -r d="$(type -P "$a")"
      if [[ -h "$d" ]]; then
        echo "link $d"
        rcommand "$(readlink "$d")"
      elif [[ -e "$d" ]]; then
        echo "file $d"
      fi
    fi
  fi
}

1 个解决方案

#1


1  

Did you mean set -x for tracing command in bash and/or sh ?

你是说在bash和/或sh中使用set -x跟踪命令吗?

Anyway, your script seem pretty and nice. There is my version with some alternatives...

无论如何,你的脚本看起来很漂亮。我的版本有一些替代品......

function rcommand() {
  local b="$(type -t "$1")"
  case $b in
    alias )
          local c="$(command -v "$1")"
          echo $c
          local d=$(sed "s/^.*='\?\([^ ]\+\) .*$/\1/" <<<$c)
          if [[ "$d" == "$1" ]]; then
              rcommand  "\\$d"
          else
              rcommand  "$d"
          fi
          ;;
      builtin | function | keyword )
          echo "$b $1"
          ;;
      * )
          local a="${1#\\}"
          local c="$(declare -F "$a")"
          if [[ "$c" == "$a" ]]; then
              echo "function $a"
          else
              local d="$(type -P "$a")"
              if [ -h "$d" ]; then
                  echo "link $d"
                  rcommand "$(readlink "$d")"
              elif [ -e "$d" ]; then
                  echo "file $d"
              fi
          fi
  esac
}

#1


1  

Did you mean set -x for tracing command in bash and/or sh ?

你是说在bash和/或sh中使用set -x跟踪命令吗?

Anyway, your script seem pretty and nice. There is my version with some alternatives...

无论如何,你的脚本看起来很漂亮。我的版本有一些替代品......

function rcommand() {
  local b="$(type -t "$1")"
  case $b in
    alias )
          local c="$(command -v "$1")"
          echo $c
          local d=$(sed "s/^.*='\?\([^ ]\+\) .*$/\1/" <<<$c)
          if [[ "$d" == "$1" ]]; then
              rcommand  "\\$d"
          else
              rcommand  "$d"
          fi
          ;;
      builtin | function | keyword )
          echo "$b $1"
          ;;
      * )
          local a="${1#\\}"
          local c="$(declare -F "$a")"
          if [[ "$c" == "$a" ]]; then
              echo "function $a"
          else
              local d="$(type -P "$a")"
              if [ -h "$d" ]; then
                  echo "link $d"
                  rcommand "$(readlink "$d")"
              elif [ -e "$d" ]; then
                  echo "file $d"
              fi
          fi
  esac
}