Search code examples
functionshelldirectoryfish

check if argv is a directory


I'm using FISH (Friendly Interactive SHell)

and I created 2 functions :

function send
    command cat $argv | nc -l 55555
end

--> send a file via nc

function senddir
    command tar cjf copy.tar $argv; cat copy.tar | nc -l 55555; rm copy.tar
end

--> send a dir by compressing it via nc

Now, I wan't to refactor and create a send function that does both, for this I need to check if argv is a dir. How can I do it with fish?


Solution

  • Same as in other shells, although in fish you are actually using an external program, not a shell built-in.

    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
    

    Actually, you don't need the temp file and can pipe the output of tar directly to nc -l, which allows you to simplify the function to

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