samba
samba

Reputation: 3101

How to dynamically define a path using a file prefix within a directory?

I wrote a bash script that first takes a projectRoot from a processor.properties file. Then creates a metricsRoot, removes a directory that contains matrics data (metricsDir) and rewrites the data in the metricsDir.

Now I want to define the /data dir name dynamically depending on a prefix of files within the directory. For example there may be files like common.mertics1.csv in the metricsDir. So I need to take the prefix before the dot (common in this case). And the metricsDir should look like metricsDir=$metricsRoot/$metricsPrefix.

How can I achieve this within the bash script?

#!/bin/bash

file="/configs/processor.properties"
projectRoot=$(grep -Po '(?<=projectRoot=).+$' "$file")

abspath=`dirname "$(cd "${0%/*}" 2>/dev/null; echo "$PWD"/"${0##*/}")"`
metricsRoot=$projectRoot/metrics
metricsDir=$metricsRoot/data

whoami=`whoami`

if [ "x${whoami}" != "xoozie" ] ; then
    echo "ERROR: You need to be an oozie user"
    exit 127
fi

hadoop fs -mkdir $metricsRoot
hadoop fs -rm -r $metricsDir
hadoop fs -put $abspath $metricsDir

Upvotes: 1

Views: 147

Answers (1)

Sam Tolton
Sam Tolton

Reputation: 357

If you just want to get the prefix of the first file in the $abspath you could use something like this:-

filenames=`ls "$abspath"/*.csv`

for filename in $filenames
do
    filename="${filename%%.*}"
    metricsPrefix="${filename##*/}"
    break
done

echo $metricsPrefix

You should test to see if $metricsPrefix actually contains anything before you use it though.

Upvotes: 2

Related Questions