Function 检查argv是否为目录

Function 检查argv是否为目录,function,shell,directory,fish,Function,Shell,Directory,Fish,我正在使用FISH(友好的交互式SHell) 我创建了两个函数: function send command cat $argv | nc -l 55555 end -->通过nc发送文件 function senddir command tar cjf copy.tar $argv; cat copy.tar | nc -l 55555; rm copy.tar end -->通过nc压缩发送目录 现在,我不想重构并创建一个同时执行这两项任务的send函数,为此我需要检查a

我正在使用FISH(友好的交互式SHell)

我创建了两个函数:

function send
    command cat $argv | nc -l 55555
end
-->通过nc发送文件

function senddir
    command tar cjf copy.tar $argv; cat copy.tar | nc -l 55555; rm copy.tar
end
-->通过nc压缩发送目录


现在,我不想重构并创建一个同时执行这两项任务的send函数,为此我需要检查argv是否是dir。如何使用fish呢?

与其他shell中相同,尽管在
fish
中,您实际上使用的是外部程序,而不是shell内置程序

function send
    if test -d $argv
        command tar cjf copy.tar $argv; cat copy.tar | nc -l 55555; rm copy.tar
    else
        command cat $argv | nc -l 55555
    end
end
实际上,您不需要temp文件,可以通过管道将
tar
的输出直接传输到
nc-l
,这允许您将函数简化为

function send
    if test -d $argv
        command tar cj $argv
    else
        command cat $argv
    end | nc -l 55555
end

与其他shell相同,尽管在
fish
中实际使用的是外部程序,而不是shell内置程序

function send
    if test -d $argv
        command tar cjf copy.tar $argv; cat copy.tar | nc -l 55555; rm copy.tar
    else
        command cat $argv | nc -l 55555
    end
end
实际上,您不需要temp文件,可以通过管道将
tar
的输出直接传输到
nc-l
,这允许您将函数简化为

function send
    if test -d $argv
        command tar cj $argv
    else
        command cat $argv
    end | nc -l 55555
end

请注意,
$argv
是一个数组,因此如果您传递了多个参数,
test
将呕吐

$ test -d foo bar
test: unexpected argument at index 2: 'bar'
更具防御性的编码:

function send
    if test (count $argv) -ne 1
        echo "Usage: send file_or_dir"
        return

    else if test -d $argv[1]
        # ...

    else if test -f $argv[1]
        # ...

    else
        # ...
    end
end

请注意,
$argv
是一个数组,因此如果您传递了多个参数,
test
将呕吐

$ test -d foo bar
test: unexpected argument at index 2: 'bar'
更具防御性的编码:

function send
    if test (count $argv) -ne 1
        echo "Usage: send file_or_dir"
        return

    else if test -d $argv[1]
        # ...

    else if test -f $argv[1]
        # ...

    else
        # ...
    end
end
是的,“argv”表示参数向量:)是的,“argv”表示参数向量:)