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

Diego Sevilla
  • 28,636
  • 4
  • 59
  • 87
FireEnigmaX
  • 567
  • 2
  • 7
  • 17

5 Answers5

4

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)

Rahul
  • 76,197
  • 13
  • 71
  • 125
  • 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 ;) ) – FireEnigmaX Jun 29 '11 at 22:37
  • 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 Jun 29 '11 at 23:12
  • @FireEnigmaX, not sure whether you have already solved it; in case you haven't yet then see my edit – Rahul Jun 30 '11 at 19:57
  • SOrry, yes I got it solved,I used the following: changeListNum=`p4 change -o | sed 's//Commiting changes to '${ExportSubDir}' '${ArtifactName}'./' | p4 change -i|cut -d ' ' -f 2` – FireEnigmaX Jul 01 '11 at 18:20
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.

Loduwijk
  • 1,950
  • 1
  • 16
  • 28
2

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

use space as a delimiter with cut command

Community
  • 1
  • 1
mkro
  • 1,902
  • 14
  • 15
1

I would use something like this

echo "Change 44152 created." | tr -d 'a-zA-Z .'
Lynch
  • 9,174
  • 2
  • 23
  • 34
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.

Chance
  • 2,653
  • 2
  • 26
  • 33