Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 597
FireEnigmaX Avatar asked Dec 03 '25 21:12

FireEnigmaX


2 Answers

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)

like image 181
Rahul Avatar answered Dec 06 '25 11:12

Rahul


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.

like image 38
Loduwijk Avatar answered Dec 06 '25 12:12

Loduwijk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!