Score:-2

My pipe is not working in my if statement

cn flag

I have this script to check git status for all of my repositories:


find / -type d -name .git 2>&- | 
while read gitFolder; do
    if [[ $gitFolder == *"/Temp/"* ]]; then
        continue;
    fi
    if [[ $gitFolder == *"/Trash/"* ]]; then
        continue;
    fi
    if [[ $gitFolder == *"/opt/"* ]]; then
        continue;
    fi
    parent=$(dirname $gitFolder);
    if [[ `git -C $parent status --porcelain` ]]; then
        echo "";
        echo $parent;
        git -C $parent status --porcelain
    else if [[ $(git -C $parent status | grep ahead) ]]; then
        echo "";
        echo "$parent is not pushed yet";
    fi
done 

But it's not working. If I remove the second else-block then it works.

Basically I want to know if a git repository has any changes (first if) or if it's ahead of master (second if).

How should I change that second if condition?

hr flag
Please try to be more descriptive than "not working" - what is supposed to happen, and what happens instead?
Lorenz Keel avatar
gr flag
Replace `else if` with `elif`. I've not checked the code for possible further errors
Score:2
gr flag

The Bash if...else if...else statement takes the following form:

if CONDITION1; then
  STATEMENTS1
elif CONDITION1; then
  STATEMENTS2
else
  STATEMENTS3
fi

The specific error in your script is the usage of the incorrect keyword else if instead of the correct one elif.

If the CONDITION1 evaluates to True, the STATEMENTS1 will be executed. If the CONDITION2 evaluates to True, the STATEMENTS2 will be executed. If none of the test commands evaluate to True, the CONDITION3 is executed.

The conditions are evaluated sequentially. Once a condition returns True, the remaining conditions are not performed, and program control moves to the end of the if statements.

You can have one or more elif clauses in the statement.

Suggestion: you can install shellcheck package to check your bash code. See https://github.com/koalaman/shellcheck for reference.

Score:-1
in flag

Try replacing

    else if [[ $(git -C $parent status | grep ahead) ]]; then

with

    else if git -C $parent status | grep ahead; then

You might also want to add -q to grep.

This works by replacing the [[ test with a naked command. The last command in the pipeline (grep) exits with a true or false value, depending if it found anything or not. The -q option tells it to only return with that value and not actually print what it found.

Note: there are other errors in your script that also need fixing.

Saeed Neamati avatar
cn flag
I don't understand it. Where is the **checking** in the second line? It seems like JavaScript.
Saeed Neamati avatar
cn flag
Did not work. Gives me syntax error. Near `done`.
terdon avatar
cn flag
@SaeedNeamati please don't tell us "did not work" or "syntax error". Edit your question, and show us what _exact_ errors you get. We can't just guess what you need.
Saeed Neamati avatar
cn flag
@terdon, I wrote the error. `Syntax error, near done`. I managed to make it work by changing the approach though.
user10489 avatar
in flag
You will need to combine the multiple answers here to get a working solution as you have multiple errors. Error near done is one or more missing `fi` to close your `if`s. Or replace `else if` with `elif`
lu flag
`elif` is the way to go.
user10489 avatar
in flag
I was thinking elif was a bash feature, but it looks like POSIX added it when I wasn't looking or something. With that out of the way, there's probably not an issue with backwards compatibility, and `elif` does look like the way to go.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.