如何从Bash/ZSH别名中指定参数值?

huangapple go评论97阅读模式
英文:

How to specify argument value from Bash/ZSH alias?

问题

在我的 .zshrc 文件中,我有以下的别名:

  1. alias ls="colorls"
  2. alias l="colorls -1"
  3. alias la="colorls -a"
  4. alias ld="colorls -d"
  5. alias lf="colorls -f"
  6. alias lt="colorls --tree"
  7. alias ltd="colorls --tree=$1"

我使用 ltd 别名运行 colorls,并使用 tree 参数,该参数需要一个整数来表示树的深度。

我希望通过运行 ltd 5(或任何指定的数字),我可以将其别名设置为 colorls --tree=5,但似乎不兼容。

此外,colorls 要求整数直接跟在等号后面,不能有空格。这意味着 alias ltd="colorls --tree=" 也不起作用。

请问如何正确执行这种别名方法?谢谢。

英文:

In my .zshrc I have the following aliases

  1. alias ls="colorls"
  2. alias l="colorls -1"
  3. alias la="colorls -a"
  4. alias ld="colorls -d"
  5. alias lf="colorls -f"
  6. alias lt="colorls --tree"
  7. alias ltd="colorls --tree=$1"`

I have used the ltd alias to run colorls with the tree argument which requires an integer for tree depth.

I was hoping that by running ltd 5 (or any specified number) I would be aliasing colorls --tree=5, but this appears to be incompatible.

Additionally colorls requires the integer to be specified directly after the equals sign without a space. This means that alias ltd="colorls --tree=" doesn't work either.

How can I perform this method of aliasing properly? Thank you

答案1

得分: 1

你可以只使用一个shell函数,而不是一个整个shell脚本的怪兽:

  1. ltd() {
  2. if [[ $# -eq 0 ]]; then
  3. print -u 2 "ltd 需要一个深度参数"
  4. return 1
  5. fi
  6. local depth=$1
  7. shift
  8. lscolor --tree=$depth "$@"
  9. }

然后 ltd 5 blah blah blah 将使用第一个参数作为 --tree 选项的参数,并将其余部分不变地传递。

英文:

You can just use a shell function instead of that monstrosity of an entire shell script:

  1. ltd() {
  2. if [[ $# -eq 0 ]]; then
  3. print -u 2 "ltd needs a depth argument"
  4. return 1
  5. fi
  6. local depth=$1
  7. shift
  8. lscolor --tree=$depth "$@"
  9. }

then ltd 5 blah blah blah will use the first argument as the argument to that --tree option, and pass the rest through unchanged.

答案2

得分: 0

我在整理这个问题的时候找到了答案。答案来自这里

代码部分如下:

  1. #!/bin/sh
  2. # POSIX
  3. die() {
  4. printf '%s\n' "$1" >&2
  5. exit 1
  6. }
  7. # 初始化所有选项变量。
  8. # 这确保我们不会受到环境变量的污染。
  9. file=
  10. verbose=0
  11. while :; do
  12. case $1 in
  13. -h|-\?|--help)
  14. show_help # 显示使用概要。
  15. exit
  16. ;;
  17. -f|--file) # 接受一个选项参数;确保已指定。
  18. if [ "$2" ]; then
  19. file=$2
  20. shift
  21. else
  22. die '错误:"--file" 需要一个非空选项参数。'
  23. fi
  24. ;;
  25. --file=?*)
  26. file=${1#*=} # 删除一切直到 "=" 并分配剩余部分。
  27. ;;
  28. --file=) # 处理空的 --file=
  29. die '错误:"--file" 需要一个非空选项参数。'
  30. ;;
  31. -v|--verbose)
  32. verbose=$((verbose + 1)) # 每个 -v 增加 1 个详细信息。
  33. ;;
  34. --) # 所有选项结束。
  35. shift
  36. break
  37. ;;
  38. -?*)
  39. printf '警告:未知选项(已忽略):%s\n' "$1" >&2
  40. ;;
  41. *) # 默认情况:没有更多选项,因此退出循环。
  42. break
  43. esac
  44. shift
  45. done
  46. # 如果提供了 --file,打开它以进行写入,否则复制 stdout
  47. if [ "$file" ]; then
  48. exec 3> "$file"
  49. else
  50. exec 3>&1
  51. fi
  52. colorls --tree="$1"

以及在 .zshrc 中的别名:

  1. alias ltd="zsh ~/ltd.sh"
英文:

I found the answer during my time putting this question together. The answer comes from here

with the code

  1. #!/bin/sh
  2. # POSIX
  3. die() {
  4. printf '%s\n' "$1" >&2
  5. exit 1
  6. }
  7. # Initialize all the option variables.
  8. # This ensures we are not contaminated by variables from the environment.
  9. file=
  10. verbose=0
  11. while :; do
  12. case $1 in
  13. -h|-\?|--help)
  14. show_help # Display a usage synopsis.
  15. exit
  16. ;;
  17. -f|--file) # Takes an option argument; ensure it has been specified.
  18. if [ "$2" ]; then
  19. file=$2
  20. shift
  21. else
  22. die 'ERROR: "--file" requires a non-empty option argument.'
  23. fi
  24. ;;
  25. --file=?*)
  26. file=${1#*=} # Delete everything up to "=" and assign the remainder.
  27. ;;
  28. --file=) # Handle the case of an empty --file=
  29. die 'ERROR: "--file" requires a non-empty option argument.'
  30. ;;
  31. -v|--verbose)
  32. verbose=$((verbose + 1)) # Each -v adds 1 to verbosity.
  33. ;;
  34. --) # End of all options.
  35. shift
  36. break
  37. ;;
  38. -?*)
  39. printf 'WARN: Unknown option (ignored): %s\n' "$1" >&2
  40. ;;
  41. *) # Default case: No more options, so break out of the loop.
  42. break
  43. esac
  44. shift
  45. done
  46. # if --file was provided, open it for writing, else duplicate stdout
  47. if [ "$file" ]; then
  48. exec 3> "$file"
  49. else
  50. exec 3>&1
  51. fi
  52. colorls --tree=$1

and the alias in .zshrc

  1. alias ltd="zsh ~/ltd.sh"

huangapple
  • 本文由 发表于 2023年7月3日 09:36:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76601408.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定