如何在 zsh/bash 脚本中将参数传递给我的函数?

How do I pass arguments to my functions in a zsh/bash script?

我正在尝试将这两个函数组合到一个脚本中。这个想法是传递标志 -E 进行加密,-D 进行解密。到目前为止,旗帜正在发挥作用。我得到了加密、解密和帮助的不同用法。 问题:函数没有获取参数,我每次都收到用法消息。 如何将参数传递给函数? 例如:

./cript.zsh -E filetoencript out.des3
#!/usr/bin/env zsh

# TODO, make it a script. Flags -E to encrypt -D to decrypt.
# Usage:  = input  = output
function encrypt() {
  if [ -z "" ]; then
    echo Usage: encrypt '<infile>' '[outfile]'
    return
  fi
  if [ -z "" ]; then
    out="".des3
  else
   
...

}

# Usage:  = input  = output
function decrypt() {
  if [ -z "" ]; then
    echo Usage: decrypt '<infile>' '[outfile]'
    return
  fi
  if [ -z "" ]; then
    
...

}

function main() {
# -E = encrypt
# -D = decrypt
# FIXME
while getopts ":E:D:" opt; do
  case $opt in
    E)
        encrypt
        ;;
    D)
        decrypt
        ;;
    *)
        help
        exit 1
        ;;
  esac
done
}

main "$@"

解决方案:

function main() {
# -E = encrypt
# -D = decrypt
while getopts ":E:D:" opt; do
  case $opt in
    E)
        shift
        encrypt "$@"
        ;;
    D)
        shift
        decrypt "$@"
        ;;
    *)
        help
        exit 1
        ;;
  esac
done
}