Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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/fourth/fifth/pom.xml -> I want to run this one only

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

share|improve this question

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 from a command line, e.g.:

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

The script, follows:

#!/bin/sh -x

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

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
if [ -f pom.xml ]
  then mvn clean package
  else echo "pom.xml does not exist in $ROOTDIR"
       exit 3
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.

share|improve this answer
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: $ ./ ./ line 3: /cygdrive/h/third/fourth/fifth/sixth: is a directory ./ line 4: RELDIR: command not found ./ line 7: ![: command not found pom.xml does not exist in – Justice Bringer Dec 12 '11 at 12:15
Fixed my issues today, if [! -d ....] is required instead of having the ! outside the square bracket. – Justice Bringer Dec 12 '11 at 20:35

You must log in to answer this question.

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