如何在 zsh 脚本中获取文件名(不是调用命令)?

How do I get the filename (not the invoking command) in a zsh script?

我正在编写一个 zsh 脚本,其中有几个指向它的符号链接。正常操作是通过其中一个链接调用脚本。

在某个时候,我想处理直接调用脚本而不是通过其中一个链接调用脚本的情况。为此,我需要获取用于调用脚本的命令并将其与脚本文件本身的名称进行比较。

似乎有几种方法可以获得调用命令($0 和 ${(%):-%N} 是两个例子)。但是,我不知道如何确定包含实际脚本源的文件的名称。每一次寻找的尝试似乎都让我知道如何获得调用命令。

下面是一些示例代码,可能有助于说明我的意思:

invoking_command=$(basename [=10=])

# If we're called via one symbolic link, do one thing.
if [[ $invoking_command = "link-one" ]]; then                                                                                                         
    condition="link-one"                                                                                                                               
fi                                                                                                                                                

# If we're called via the other link, do something else.                                                                                                                                                  
if [[ $invoking_command = "sapti" ]]; then                                                                                                        
    condition="link-two"                                                                                                                              
fi   

# If we're called directly, do something like, for example, 
# recommend to the user what the expected usage is.
if [[ $invoking_command = (??? WHAT GOES HERE ???) ]]
    condition="script file was run directly"
    usage && exit
fi

在我在这里使用的具体示例中,如果第一个条件都不成立,我想我可以只打印用法。这将处理这种情况,但我仍然面临着如何在需要时找到文件名的问题。

这当然是可能的。想法?

来自 zshexpn:

   a      Turn a file name into an absolute path:   prepends  the  current
          directory, if necessary, and resolves any use of `..' and `.' in
          the path.  Note that the transformation takes place even if  the
          file or any intervening directories do not exist.

   A      As  `a',  but also resolve use of symbolic links where possible.
          Note that resolution of `..' occurs before  resolution  of  sym-
          bolic  links.   This  call is equivalent to a unless your system
          has the realpath system call (modern systems do).
#!/usr/local/bin/zsh
mypath=[=11=]:A
invoker=[=11=]
echo $mypath
echo $invoker