1

我想用类似于(all)type的选项(follow) 来调用。-f-a

以下是我的问题:

  1. 是否有内置的 Bash 来打印如何bash执行命令?
  2. 是否有 Linux 实用程序可以打印任何 shell 如何执行命令?
  3. 我正在使用的以下 shell 函数可以简化吗?

给定以下定义并/usr/local/bin/ls链接到/usr/bin/ls

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

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

这是我在 .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
}
4

1 回答 1

1

您的意思是在和/或set -x跟踪命令吗?bashsh

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

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
}
于 2012-11-11T14:05:44.830 回答