Looping though a file in bash and filtering for a directory name beginning

Yannick Forster :

I'm really new to bash scripting and I am trying to write a bash script to use with subversion hooks.

The goal is to get a list of the java projects that were comitted, then write that list to a file (so I can create a change log from it and display it inside an application) along some other information gathered using svnlook that I've already worked out.

Since subversion itself doesn't really care for projects that I commit in eclipse and instead works with directories, I have to use the "svnlook changed" command which spits out each and every file with its full path that was included in the commit.

Here's an example of what "svnlook changed" returns:

U   Project/branches/11.4.11.001/LIB1/com/some/other/directory/some_java_class.java 
U   Project/branches/11.4.11.001/LIB2/com/some/other/directory/another_thingy.java                                                                                                                                                                                                                                                                                                                                                                     
U   Project/branches/11.4.11.001/new/directories/LIB1/com/something/some_java_class.java                                                                                                                                                                                                                                                                                                                                                            
U   Project/branches/11.4.11.001/PRJ1/com/directory/some_java_class.java

Now I can't guarantee that this directory structure will always be the same, so what I want to do is to find the java project names by filtering for "PRJ" and "LIB", since they will always begin with those letters and I can be sure this will not change.

So what I am trying to do in the script:

Step 1: Put the output of "svnlook changed" into a file:

tempfile=/data/svn/scratch.txt
input=/data/svn/scratch2.txt
touch $tempfile
touch $input

$SVNLOOK changed -r "$REVISION" "$REPOS" >> $input 

This works.

Step 2: iterate over every line of the file, get the substring that represents the project by looking for "LIB" or "PRJ" in the path. Write these into a file. For now I am just assuming there are no more than 9 child directories and I am looping over the path 10 times to look at each:

for i in {1..10}
do
        cat $input | cut -d "/" -f $i | grep -i -s -e PRJ -e LIB | tr [:lower:]äöü [:upper:]ÄÖÜ >> $tempfile
done

This doesn't work in the script. The file at /data/svn/scratch.txt is always empty. When I run the command from the command line and replace $input and $i, it works and all of the following commands work, too.

Step 3: Set all the other variables. I iterate over the temporary file, filtering out the duplicates and putting commas to seperate them:

DATE=$(date '+%d-%m-%Y')
REVISION=$($SVNLOOK youngest "$REPOS") 
CHANGEDPRJ=$(cat $tempfile | sort -u |  xargs | sed -e 's/ /, /g')
COMMENT=$($SVNLOOK log -t "$TXN" "$REPOS")

Step 4:Output this variable along other stuff into the file in which I'd like to store it all:

echo "$DATE" , "$REVISION" , "$CHANGEDPRJ" , "$COMMENT" >> /data/svn/commit.log

So right now I am getting:

17-02-2020 , 571 , , Cleanup of outdated comments

Instead of:

17-02-2020 , 571 , LIB1,LIB2,PRJ1 , Cleanup of outdated comments

I'm pretty sure there is a very obvious and easy solution to get this working, but I'm bash scripting for the very first time today and I can't seem to google for the right thing to find out what I'm doing wrong.... If someone could point me into the right direction that'd be amazing!

KamilCuk :

The following:

cat <<EOF |
U   Project/branches/11.4.11.001/LIB1/com/some/other/directory/some_java_class.java 
U   Project/branches/11.4.11.001/LIB2/com/some/other/directory/another_thingy.java
U   Project/branches/11.4.11.001/new/directories/LIB1/com/something/some_java_class.java     
U   Project/branches/11.4.11.001/PRJ1/com/directory/some_java_class.java
EOF
# remove the U<space><space>
sed 's/^U  //' |
# replace `/` with a newline
tr '/' '\n' |
# grep only lines with PRJ and LIB
grep -e 'PRJ\|LIB' |
# sort unique
sort -u |
# join elements with a comma
paste -sd,

outputs:

LIB1,LIB2,PRJ1

So you want:

"$SVNLOOK" changed -r "$REVISION" "$REPOS" |
sed 's/^U  //' | tr '/' '\n' | grep -e 'PRJ\|LIB' | sort -u | paste -sd,

Note: remember to quote variable expansions. Don't touch $tempfile do touch "$tempfile".

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=12113&siteId=1