0

I'm new to scripting so please bear with me (if you can).

I want to do something similar to this question but the 'root' directory will be different for different users on different OS (there can't be any windows paths 'hardcoded' else the script will fail on another machine).

I'm running cygwin on Windows 7 and the script I want will also be accessed by users running Linux.

I can simply do this on my machine:

cd "D:/first/second/third/fourth/fifth" (contains pom.xml I want to execute)
mvn clean package

The location "D:/first/second/third/" is the basedir on my machine, so can I find a relative path for this that will work on windows and linux?

The pom.xml I want to execute lives under /fourth/fifth/.

I've used find -name pom.xml -type f but this returns many different pom.xml files in all subdirectories under my current one:

D:/first/second/third/pom.xml
D:/first/second/third/fourth/pom.xml
D:/first/second/third/fourth/fifth/pom.xml -> I want to run this one only
D:/first/different-secondsecond/pom.xml

Can anyone give me some tips how I can make a OS independent script that will find and run the pom that I want?

Thanks in advance

1 Answer 1

0

Note: the -x in the script's first line echoes script lines as they execute and helps debugging it - simple edit it out if the script does what you want after testing it.

Try invoking the following Unix Bourne shell script or with Linux using 1st line modified as: #!/bin/bash -x

To invoke the script (herein named mvnclnpkg.sh) from a command line, e.g.:

$ ./mvnclnpkg.sh "D:/first/second/third" "/fourth/fifth"

The script, mvnclnpkg.sh follows:

#!/bin/sh -x

if [ $1 == "" -or $2 == "" ]
  then echo "$1 or $2 is null, please provide both parameters to script"
       exit 1
fi

BASEDIR=$1 # where BASEDIR = D:/first/second/third aka $1 parameter to script
RELDIR=$2 # where RELDIR = /fourth/fifth aka $2 parameter to script
MVNCLNPKGDIR=$BASEDIR$RELDIR # $1 is required root directory parameter to script

if ![ -d $MVNCLNPKGDIR ]
  then echo "$MVNCLNPKGDIR does not exist"
       exit 2
fi
cd $MVNCLNPKGDIR
if [ -f pom.xml ]
  then mvn clean package
  else echo "pom.xml does not exist in $ROOTDIR"
       exit 3
fi
exit 0

Note: Because you did not want to operate on the pom.xml file, but instead wanted it to exist in a particular path name and then execute the maven command, it did not make sense to code the script with the find command.

2
  • Thanks for the assist, however I want the script to be self contained so that no one need pass any parameters when they execute it. If I substitue real values for the BASEDIR and RELDIR it 'should' work just fine, but it doesn't. I'm using BASEDIR= "$(dirname $(readlink -f $0))" and RELDIR=/sixth/seventh/ and assigning that to the MVNCLNPKG variable. This is fails: $ ./mvnclnpkg.sh ./mvnclnpkg.sh: line 3: /cygdrive/h/third/fourth/fifth/sixth: is a directory ./mvnclnpkg.sh: line 4: RELDIR: command not found ./mvnclnpkg.sh: line 7: ![: command not found pom.xml does not exist in Dec 12, 2011 at 12:15
  • Fixed my issues today, if [! -d ....] is required instead of having the ! outside the square bracket. Dec 12, 2011 at 20:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .