shell 通过zsh中的别名在索引处检索输入参数

iqjalb3h  于 2023-05-29  发布在  Shell
关注(0)|答案(2)|浏览(134)

我试图在ZSH中创建函数和别名,使参数处理更容易阅读。
因此,我想创建像下面的例子一样的东西:

#!/bin/zsh
myCustomFunction(){
  local numberOfParameters=$(count-parameters)
  local lastParameter=$(get-parameter $numberOfParameters)
  echo "Number of parameters: $numberOfParameters"
  echo "Last parameter is: $lastParameter"
}

其中count-parameters定义如下:

#!/bin/zsh
alias count-parameters='echo "$#"

如何实现get-parameter?我一直在尝试使用"$@",就像here中建议的那样,但似乎不起作用。
我想要的结果是,给定一个输入,例如:

myCustomFunction.sh "First param" "Second param" "Third param"

输出将是:

Number of parameters: 3
Last parameter: Third param
1zmg4dgp

1zmg4dgp1#

您可以通过将别名作为执行实际工作的函数的瘦 Package 器来实现这一点。
下面有一个zsh的shebang,但也是为了与bash一起使用而编写的。

#!/bin/zsh

# define functions to be called by the aliases below
_count-parameters() { echo "$#"; }
_get-parameter() {
  local last_idx tgt
  # make args array 1-indexed whether running in bash or zsh
  local -a args=( ${BASH_VERSION:+ "_" } "$@" )
  tgt=$(( "${args[$#]}" ))
  echo "${args[$tgt]}"
}

# this is supposed to be run with zsh, so the below line _should_ be moot
# for bash, aliases are off by default in scripts; turn them on explicitly
[[ $BASH_VERSION ]] && shopt -s expand_aliases

alias count-parameters='_count-parameters "$@"'
alias get-parameter='_get-parameter "$@"'

myCustomFunction() {
  local numberOfParameters lastParameter
  numberOfParameters=$(count-parameters)
  lastParameter=$(get-parameter "$numberOfParameters")
  echo "Number of parameters: $numberOfParameters"
  echo "Last parameter is: $lastParameter"
}

myCustomFunction "First param" "Second param" "Third param"
epggiuax

epggiuax2#

经过反复试验,我终于达到了预期的结果.
我是这么做的。
1.计数参数别名

#!/bin/zsh
alias count-parameters='echo "$#"'

对于get-parameter,我必须执行多个操作(黑客的方式,但它的工作)

  • 将每个参数保存在文件中(每个参数由NUL字符分隔)
  • 将文件中的文本存储在一个变量中,并对每个字符进行循环。使用NUL字符确定一个参数结束的位置和下一个参数开始的位置。然后,删除临时创建的文件
#!/bin/zsh
export tempParameterFile="$HOME/.params.txt"
alias save-parameters='save_parameters "$@"'
#!/bin/zsh
save_parameters(){
  numberOfParams=${#}
  if [ "$numberOfParams" -gt 0 ]; then
    rm -f "$tempParameterFile"
    while (( $# ))
    do
      echo -n "$1" >> "$tempParameterFile"
      echo -ne $'\0' >> "$tempParameterFile" # Adding NUL character after each parameter
      shift
    done
  fi
}
#!/bin/zsh

get_parameter(){
  local indexToUse=$1
  #echo "Called get_parameter for index $1"
  local currentIndex=0
  local nullCharacter=$'\0'
  local currentParam=""
  local allParameters=$(cat "$tempParameterFile")
  local currentCharacter=""
  for (( index=0; index<${#allParameters}; index++ ));
  do
    currentCharacter="${allParameters:$index:1}"
    if [ "$currentCharacter" = "$nullCharacter" ]; then # When the null character is found
      if [ "$currentIndex" = "$indexToUse" ];then # If the current index matches the desired index
        echo "$currentParam" # We found the word we were looking for
        return 0
      else # If the current index is lower than the desired index
        currentIndex=$(( $currentIndex + 1 )) # We increase the current index
        currentParam="" # And reset the current parameter
      fi
    else # If the character read is not  the null character
      currentParam="$currentParam$currentCharacter" # We append the character to the current param
    fi
  done
}
#!/bin/zsh
alias get-parameter='save_parameters "$@" && get_parameter'

运行命令时:

╰─$ myCustomFunction.sh "One param" "Second param" "Third param" "Fourth param"
Number of parameters: 4
Last parameter: Fourth param

达到了预期的结果。
编辑:更新了代码,以解决输入参数中包含新行的可能性。现在使用NUL字符来区分参数,正如注解中所建议的那样。

相关问题