开发者

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

开发者 https://www.devze.com 2023-03-16 13:48 出处:网络
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 scri

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 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)


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.


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

use space as a delimiter with cut command


I would use something like this

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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消