-1

I am trying to write a script to go into a folder, find all the pom files in there located in different directories and change their version. I think the issue is with my sed command cos currently I am not getting any errors but the version doesn't change. Could I get some assistance on what I am doing wrong please. Thanks.

#!/bin/bash

mainPath=/Users/name/bin/proj/simulators
oldPOMVersion=$1
newPomVersion=$2

find $mainPath -name "pom.xml" | while read filePath; do
  if grep -aq "<version>$1</version>" $filePath
  then
    echo "=================================================="
    echo "Switching version for $filePath ..."
    sed -i "" 's/$1/$2/g' $filePath 
  fi
done

Calling the script as follows which outputs no errors but doesn't change the POM file versions as expected. Note that there will be no conflicts with other dependencies in that file due to the 'guardian' text in the version below which is unique. The filePath is correct (grep works) for each file based on the echo in the script.

bash updt.sh 5.7-guardian-SNAPSHOT 5.8-guardian-SNAPSHOT
karvai
  • 2,417
  • 16
  • 31

1 Answers1

0

Don't use sed to edit XML, use an XML-aware tool. For example, in xsh you can write

register-namespace pom http://maven.apache.org/POM/4.0.0 ;

$pom         = $ARGV[1] ;
$old_version = $ARGV[2] ;
$new_version = $ARGV[3] ;

open $pom ;
for //pom:version[. = $old_version]
    set . $new_version ;
save :b ;
choroba
  • 231,213
  • 25
  • 204
  • 289
  • Looks good but seems like I need to download / install to be able to use this. Gonna be a challenge to use this considering the amount of red tape in bringing in external downloads. – karvai Oct 18 '19 at 12:12
  • @karvai: There are many similar tools, try to find the one that's easiest for you to get. – choroba Oct 18 '19 at 13:32