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

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

How to specify argument value from Bash/ZSH alias?

问题

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

    alias ls="colorls"
    alias l="colorls -1"
    alias la="colorls -a"
    alias ld="colorls -d"
    alias lf="colorls -f"
    alias lt="colorls --tree"
    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

alias ls="colorls"
alias l="colorls -1"
alias la="colorls -a"
alias ld="colorls -d"
alias lf="colorls -f"
alias lt="colorls --tree"
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脚本的怪兽:

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

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

英文:

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

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

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

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

代码部分如下:

#!/bin/sh
# POSIX

die() {
    printf '%s\n' "$1" >&2
    exit 1
}

# 初始化所有选项变量。
# 这确保我们不会受到环境变量的污染。
file=
verbose=0

while :; do
    case $1 in
        -h|-\?|--help)
            show_help    # 显示使用概要。
            exit
            ;;
        -f|--file)       # 接受一个选项参数;确保已指定。
            if [ "$2" ]; then
                file=$2
                shift
            else
                die '错误:"--file" 需要一个非空选项参数。'
            fi
            ;;
        --file=?*)
            file=${1#*=} # 删除一切直到 "=" 并分配剩余部分。
            ;;
        --file=)         # 处理空的 --file=
            die '错误:"--file" 需要一个非空选项参数。'
            ;;
        -v|--verbose)
            verbose=$((verbose + 1))  # 每个 -v 增加 1 个详细信息。
            ;;
        --)              # 所有选项结束。
            shift
            break
            ;;
        -?*)
            printf '警告:未知选项(已忽略):%s\n' "$1" >&2
            ;;
        *)               # 默认情况:没有更多选项,因此退出循环。
            break
    esac

    shift
done

# 如果提供了 --file,打开它以进行写入,否则复制 stdout
if [ "$file" ]; then
    exec 3> "$file"
else
    exec 3>&1
fi

colorls --tree="$1"

以及在 .zshrc 中的别名:

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

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

with the code

#!/bin/sh
# POSIX

die() {
    printf '%s\n' "$1" >&2
    exit 1
}

# Initialize all the option variables.
# This ensures we are not contaminated by variables from the environment.
file=
verbose=0

while :; do
    case $1 in
        -h|-\?|--help)
            show_help    # Display a usage synopsis.
            exit
            ;;
        -f|--file)       # Takes an option argument; ensure it has been specified.
            if [ "$2" ]; then
                file=$2
                shift
            else
                die 'ERROR: "--file" requires a non-empty option argument.'
            fi
            ;;
        --file=?*)
            file=${1#*=} # Delete everything up to "=" and assign the remainder.
            ;;
        --file=)         # Handle the case of an empty --file=
            die 'ERROR: "--file" requires a non-empty option argument.'
            ;;
        -v|--verbose)
            verbose=$((verbose + 1))  # Each -v adds 1 to verbosity.
            ;;
        --)              # End of all options.
            shift
            break
            ;;
        -?*)
            printf 'WARN: Unknown option (ignored): %s\n' "$1" >&2
            ;;
        *)               # Default case: No more options, so break out of the loop.
            break
    esac

    shift
done

# if --file was provided, open it for writing, else duplicate stdout
if [ "$file" ]; then
    exec 3> "$file"
else
    exec 3>&1
fi

colorls --tree=$1

and the alias in .zshrc

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:

确定