FireEnigmaX
FireEnigmaX

Reputation: 577

bash scripting - getting number output onto screen as result of previous command

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

Upvotes: 3

Views: 889

Answers (5)

Chance
Chance

Reputation: 2700

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.

Upvotes: 0

Rahul
Rahul

Reputation: 77876

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)

Upvotes: 3

Lynch
Lynch

Reputation: 9474

I would use something like this

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

Upvotes: 1

Loduwijk
Loduwijk

Reputation: 1977

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.

Upvotes: 3

mkro
mkro

Reputation: 1892

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

use space as a delimiter with cut command

Upvotes: 2

Related Questions