3

I'm writing the second version of my post-receive git hook.

I have a GL_REPO variable which conforms to:

/project.name/vhost-type/versioncodename

It may or may not have a trailing and/or preceding slash.

My current code misunderstood the function of the following code, and as a result it clearly duplicates $versioncodename into each variable:

# regex out project codename
PROJECT_NAME=${GL_REPO##*/}
echo "project codename is: $PROJECT_NAME"

# extract server target vhost-type -fix required
VHOST_TYPE=${GL_REPO##*/}
echo "server target is: $VHOST_TYPE"

# get server project - fix required
PROJECT_CODENAME=${GL_REPO##*/}
echo "server project is: $PROJECT_CODENAME"

What is the correct method for taking these elements one at a time from the back of the string, or guaranteeing that a three part string allocates these variables?

I guess it might be better to split into an array?

3 Answers 3

9
#!/bin/bash

GL_REPO=/project.name/vhost-type/versioncodename
GL_REPO=${GL_REPO#/} # remove preceding slash, if any

IFS=/ read -a arr <<< "$GL_REPO"

PROJECT_NAME="${arr[0]}"
VHOST_TYPE="${arr[1]}"
PROJECT_CODENAME="${arr[2]}"

UPDATE: an alternative solution by anishsane:

IFS=/ read PROJECT_NAME VHOST_TYPE PROJECT_CODENAME <<< "$GL_REPO"
Sign up to request clarification or add additional context in comments.

5 Comments

I was going to answer exactly this. +1. Clean & simple.
Alternately IFS=/ read PROJECT_NAME VHOST_TYPE PROJECT_CODENAME <<< "$GL_REPO"
Hi I used your solution in the end
@anishsane: looks great. I have updated my answer with your solution. user3706702: thanks
@anishine my thanks was removed from the solution due to editing, so just to be sure I am grateful to everyone who helped including Euginiu Rosca, and several other good solutions to this problem
2

You can use cut with a field separator to pull out items by order:

NAME=$(echo $GL_REPO | cut -d / -f 1)

You can repeat the same for other fields. The trailing/leading slash you can ignore (you'll get a NAME field being empty, for example) or you can strip off a leading slash with ${GL_REPO##/} (similarly, you can strip off a trailing slash with ${GL_REPO%%/}).

1 Comment

Hi thanks. I used part of your solution in resolving the problem to make sure both ends had the slashes removed
1

This is another way:

GL_REPO="/project.name/vhost-type/versioncodename"
GL_REPO="${GL_REPO/#\//}" 
#^replace preceding slash (if any) with empty string.
IFS="/" arr=($GL_REPO)
echo "PN: ${arr[0]} VHT: ${arr[1]} VC: ${arr[2]}"

Using Bash Pattern Matching:

GL_REPO="/project.name/vhost-type/versioncodename"
patt="([^/]+)/([^/]+)/([^/]+)"
[[ $GL_REPO =~ $patt ]]
echo "PN: ${BASH_REMATCH[1]} VHT: ${BASH_REMATCH[2]} VC: ${BASH_REMATCH[3]}"

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.