1

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
2
  • 1
    Can you add example catalog tree? It's hard to deduce what do you want to achieve.
    – JUSHJUSH
    Aug 16, 2019 at 10:37
  • @JUSHJUSH the files are originally locaded in a classpath: /resources/metrics/data/common.mertics1.csv
    – samba
    Aug 16, 2019 at 10:47

1 Answer 1

2

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.

2
  • Actually I want to take the prefix of a first file in the directory if it exists
    – samba
    Aug 16, 2019 at 12:27
  • OK, I've edited the answer to get the prefix of the first file
    – Sam Tolton
    Aug 16, 2019 at 14:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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