Search code examples
functionfish

Fish: Compiler apparently not waiting the execution of previous command


Using Fish functions when I run "rmccw *" the *.out files are excluded, as expected, but the compiler doesn't run and I get this error:

input file 'a.out' is the same as output file
collect2: error: ld returned 1 exit status

When I run the same command a second time (there's no .out files) the compiler runs normally. A a.out file is created.

function rmouts
        set outs *.out
        if count $outs >/dev/null
                rm -f *.out
        end
end

function ccw
        cc $argv -Wall -Wextra -Werror
end

function rmccw
        rmouts
        ccw $argv
end

Instead of using rmccw if I run "rmouts && ccw *" I get no errors. I already tried to add a "wait $last_pid" after rmouts, but it didn't work either.

Instead of using rmccw if I run "rmouts && ccw *" I get no errors. I already tried to add a "wait $last_pid" after rmouts, but it didn't work either.


Solution

  • Calling rmccw *, fish expands * before invoking the function. You are passing .out files to ccw.

    Here's a command to filter out .out files from $argv:

    set i 1
    while test $i -le (count $argv)
        if string match -q '*.out' $argv[$i]
            set -e argv[$i]
        else
            set i (math $i + 1)
        end
    end