3

I am trying to automate the copying of changed files into a perforce changelist, but need help getting the generated changelist number. I assume this is probably a straight-forward thing for bash scripting - but I'm just not getting it yet!!...

Basically I execute the command

p4 change -o | sed 's/<enter description here>/This is my description./' | p4 change -i

As a result of this, I get output onto screen something like the line below (obviously the number changes)

Change 44152 created.

What I want, is to be able to capture the generated number into a variable I can then use in the rest of my script (to add future files to the same changelist etc)...

Can anyone please advise?

Thanks

5 Answers 5

3

like this

change=`p4 change -o | sed 's/<enter description here>/This is my description./' | p4 change -i|cut -d f2`

echo $change

EDIT: Per @Enigma last comment

If you want to use shell variable in sed command use doublr quote "" instead single quote '' around sed command. Like below

sed "s/<enter description here>/ updating $change form/"

Results in "updating 44152 form" ($change holds value 44152)

Sign up to request clarification or add additional context in comments.

4 Comments

SO many ways to do this obviously, but I like this method, which is using @mkro sugestion, so thanks to you both! (you get the answer mark for the example ;) )
sorry, one further thing, I tried replacing the /This is my description./ with some dyanmic data eg /updating ${formName} form/ but i get the actual ${formName} string in the description. -- how can I escape this?? (nb, i tried without the {} also)
@FireEnigmaX, not sure whether you have already solved it; in case you haven't yet then see my edit
SOrry, yes I got it solved,I used the following: changeListNum=p4 change -o | sed 's/<enter description here>/Commiting changes to '${ExportSubDir}' '${ArtifactName}'./' | p4 change -i|cut -d ' ' -f 2
3

You can capture the output of a command with the ` character.

myVariable=`myCommand`

You can use awk to get the 2nd column of data, the number part.

myVariable=`originalCommand|awk '{print $2}'`

Now myVariable will be your number, 44152.

Comments

2

you could use cut. Here is another related stackoverflow entry:

use space as a delimiter with cut command

Comments

1

I would use something like this

echo "Change 44152 created." | tr -d 'a-zA-Z .'

Comments

0

If you are wanting to get the last generated changelist, you can also type

variable=`p4 counter change`

but this will only work if no one else made a changelist after you made yours.

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.