Sunday, April 28, 2024
 Popular · Latest · Hot · Upcoming
3
rated 0 times [  3] [ 0]  / answers: 1 / hits: 854  / 2 Years ago, wed, august 24, 2022, 11:09:06

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?


More From » bash

 Answers
1

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.


[#1259] Thursday, August 25, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
doredtness

Total Points: 153
Total Questions: 113
Total Answers: 106

Location: South Georgia
Member since Fri, Nov 13, 2020
4 Years ago
doredtness questions
Tue, Aug 2, 22, 15:45, 2 Years ago
Sat, May 21, 22, 05:26, 2 Years ago
;