Search code examples
fish

Syntax error in script


My script try execute mvn clean install in all projects but before it tried switch to dev branch and pull it.

successString="[INFO] BUILD SUCCESS";
file="mvnoutput";
red=$'\e[1;31m';
grn=$'\e[1;32m';
end=$'\e[0m';

function checkResult
    if grep -Fxq "$successString" $file 
    then
        echo -en "${grn}[${1}]Build ok${end}";
    else    
        echo "${red}[${1}]Error in mvn clean install${end}";
        exit 1;
    fi;
end

function pullAndSwitchDevBranch
    git checkout dev;
    git pull origin dev;
end 

cd api-pay-commons/;
pullAndSwitchDevBranch;
touch mvnoutput;
mvn clean install -U > mvnoutput;
checkResult PAY;

Why I received this error?

line 17: end: command not found ./script.sh: line 20: syntax

error near unexpected token git' ./script.sh: line 20: git checkout dev;'


Solution

  • You have used the "fish" tag, so I'm assuming you are running this with the fish shell.

    If so: This is not a valid fish script. Fish is explicitly not POSIX-compatible, so you might want to read up on the syntax. (If not, then please correct your tag)

    red=$'\e[1;31m';

    Fish does not use var=value to set a variable. It uses set var value.

    Fish also does not have the $'' style of quoting. Instead, backslash-escapes are interpreted outside of quotes.

    So this would be set red \e"[1;31m". Alternatively, fish offers the set_color builtin, which prints the escape sequence for a given color. So this could be set red (set_color red) (or you could call set_color later).

    then
    

    Fish does not use if condition; then dosomething; fi. It's if condition; dosomething; end.

    echo -en "${grn}[${1}]Build ok${end}";
    

    Fish does not use "${var}". It also does not call the function arguments $1 et al.

    This would be something like echo -ens "$grn" "[$argv[1]]" "Build ok" "$end".

    exit 1
    

    Fish currently does not allow exiting from functions. You'll have to return 1 and check the result from the outside.

    Additionally, you're using semicolons all over the place, which is not typical style in any shell I'm aware of.


    In case you are not using fish, but some POSIX-compatible shell (like bash), this is also not valid. The main (and possibly only) issue is that you are using function something; dosomething; end. In bash, that would be

     function something () {
          #dostuff
     }
    

    And in pure POSIX, that would be

     something () {
         #dostuff
     }