Hi,
I am trying to write a small shell script that will take user input on SVN branch and validate that
the requested branch exists in SVN. And then continue with other operations if the branch is valid.
I tried the following but seems its not working right:

VALID_BRANCHES="4.2.1 4.1.0 4.3.1 4.2.2"

for brnache in $VALID_BRANCHES
do 
    echo "Checking if $brnache is a valid branch in SVN"
    if [ "$1" != "$brnache" ];
    then
        echo "it isn't"
        continue
    else
        echo "$1 is a valid branch"
        break
    fi
done

Dani AI

Generated

The loop in the original post will only stop early on a match; if no match is found it simply falls through and the rest of the script keeps running. A reliable pattern is either (A) ask SVN directly whether the branch exists, or (B) validate against a whitelist and explicitly exit on failure. As suggested, breaking a loop is not the same as exiting the script — use exit (or set a flag and test it after the loop).

A: check SVN directly (preferred when you want the truth from the repository)

#!/bin/sh
REPO_BASE="https://svn.example.com/project/branches"
branch="$1"

if [ -z "$branch" ]; then
  printf 'Usage: %s <branch-name>\n' "$0" >&2
  exit 2
fi

if svn info "${REPO_BASE}/${branch}" >/dev/null 2>&1; then
  printf '%s is a valid branch\n' "$branch"
else
  printf '%s not found in SVN\n' "$branch" >&2
  exit 1
fi

# continue with further operations...

B: whitelist inside the script (useful if you must restrict to known versions)

branch="$1"
case "$branch" in
  4.2.1|4.1.0|4.3.1|4.2.2) ;;
  *) printf '%s is not an allowed branch\n' "$branch" >&2; exit 1;;
esac

Troubleshooting notes: quote variables to handle odd names, check for an empty $1, and remember svn may prompt for credentials — use --non-interactive or supply credentials in automation. If you prefer a list variable, use a case or an array (bash) instead of repeatedly printing "it isn't" inside a loop.

Recommended Answers

All 3 Replies

hi,

«is not working right» is not very descriptive.

Oh,,sorry. Let me try again to explain my problem.
I want to check if the branch exists. So it compares the user input will all existing branches. Until it finds a match it keeps comparig. Soon it finds a match it continues, if no match it exits al together.

My script continues with or without a match.

you need at least a variable that's set or not depending on user's input branch exists or not, which would be tested to exit the script or not.

var=""
for  i in blue yellow red
do
    test "purple" = "$i" && { var=1; break;} || { var=0; continue;}
done
test $var -eq 0 && echo "no match: exit script" || echo "match: continue script"

continue and break are only useful for for, while, or until command, they will not exit, or continue the script.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.