如何将bash函数转换为fish函数?

问题描述

我使用this post和文档,试图为gokechan/lf软件包上的PR创建fish函数,以使lfcd在鱼中工作。

这是我要转换的bash函数:

2020-09-15 17:05:36,992 [INFO] No Names entry in Catalog
Traceback (most recent call last):
  File "facturx-pdfextractxml.py",line 95,in <module>
    main(options,arguments)
  File "facturx-pdfextractxml.py",line 80,in main
    logger.warn('File %s has not been created',out_xml_filename)
  File "C:\Users\Selim SEDIKKI\AppData\Local\Programs\Python\Python38\lib\logging\__init__.py",line 1449,in warn
    warnings.warn("The 'warn' method is deprecated,"
  File "C:\Users\Selim SEDIKKI\AppData\Local\Programs\Python\Python38\lib\warnings.py",line 109,in _showwarnmsg
    sw(msg.message,msg.category,msg.filename,msg.lineno,File "C:\Users\Selim SEDIKKI\AppData\Local\Programs\Python\Python38\lib\site-packages\PyPDF4\pdf.py",line 1133,in _showwarning
    file.write(formatWarning(message,category,filename,lineno,line))
  File "C:\Users\Selim SEDIKKI\AppData\Local\Programs\Python\Python38\lib\site-packages\PyPDF4\utils.py",line 69,in formatWarning
    file = filename.replace("/","\\").rsplit("\\",1)[1] # find the file name
IndexError: list index out of range

这是我从上面的函数改编而成的fish函数:

lfcd () {
tmp="$(mktemp)"
lf -last-dir-path="$tmp" "$@"
if [ -f "$tmp" ]; then
    dir="$(cat "$tmp")"
    rm -f "$tmp"
    if [ -d "$dir" ]; then
        if [ "$dir" != "$(pwd)" ]; then
            cd "$dir"
        fi
    fi
fi
}

到目前为止,该函数运行lf,但在退出时未打开最后选择的目录。

我在创建鱼功能或将bash转换为鱼方面有0年经验。 这是我的入门鱼项目,因此,非常感谢您的帮助或指导!

解决方法

大多数时候,并不是必须要在鱼中引用变量:分词不是鱼的功能。

您可以写:

function lfcd
    set tmp (mktemp)
    lf -last-dir-path=$tmp $argv
    if [ -f $tmp ]
        set dir (cat $tmp)
        rm -f $tmp
        [ -d $dir ]; and [ $dir != (pwd) ]; and cd $dir
    end
end

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...