5

I have a .bat file that I need to convert into a Linux .sh one.

In this .bat script there si some piece of code I just do not understand and I cannot find proper keywords to search it's meaning.

Here is the code:

if not x%VERSION:SNAPSHOT=%==x%VERSION% (
        echo " ....  SNAPSHOT version detected "
        echo VERSION=%VERSION:SNAPSHOT=%%formatdate%_%formattime%
    )

My main problem lies with the usage of a ":". What does %VERSION:SNAPSHOT=% do?

I also do not know the meaning of the 'x' in x%VERSION% or x%VERSION:SNAPSHOT%.

2
  • See set /? - the colon is used to replace one substring with another after=
    – woxxom
    Commented Nov 22, 2015 at 18:07
  • The 'x' which is used on both sides of the == sign is a dirty hack: it prevents a syntax error in case either side of the == sign evaluates to nothing.
    – Mike Nakis
    Commented Nov 22, 2015 at 18:12

1 Answer 1

8

The : (as well as the = at the end of the variable) are used for string substitution.

In this case, %VERSION:SNAPSHOT=% says to take the contents of the variable %VERSION% and replace any instance of the string SNAPSHOT with nothing.

As Mike said in the comments, the x on both sides is used to prevent a syntax error in case %VERSION% contains nothing but the string SNAPSHOT, which would cause a syntax error. Traditionally, you'll see quotes being used instead of other strings, but this method is perfectly valid.

The entire if statement checks to see if VERSION contains the substring SNAPSHOT and runs its code if it does.

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.