I am trying to perform an action depending on the output of perforce commands. But it seems that pipping and greping/acking the command doesn’t appear to pickup the output
e.g.
p4 sync -n $HOME/ | grep -c up
/homedirectory/ - file(s) up-to-date.
0
p4 sync -n $HOME/ | grep -c nope
/homedirectory/ - file(s) up-to-date.
0
Further example of what i’m trying to do:
if ( `p4 sync -n $HOME/... | grep -c "no such file"` == 0 ) then
if command
else
do else command
endif
Is there anyway to read the output of a perforce command without having to write to a file then read the output? Ideally the command would be a single line.
Advertisement
Answer
grep
isn’t working because the “empty” messages like no such file
and up-to-date
go to stderr. As @heemayl suggested one way to fix that is to do a redirect.
You can also fix this in a shell-independent way by using the -s
or -e
flags to p4
:
C:Perforcetest>p4 -s sync
error: File(s) up-to-date.
exit: 0
C:Perforcetest>p4 -e sync
error: File(s) up-to-date.
code0 554768772 (sub 388 sys 6 gen 17 args 1 sev 2 uniq 6532)
code0 554768772
fmt0 [%argc% - file(s)|File(s)] up-to-date.
argc
exit: 0
Both of these flags redirect all output to stdout and also prepend every message with debugging information about the message itself. If you’re trying to grep for a particular message, for example, you can use the -e
flag and grep for its unique code rather than the string.
Using the -F
flag lets you reformat the output to include particular elements from the message dict that you see with -e
, so if you want just the code:
C:Perforcetest>p4 -F %code0% sync
554768772
If you’re trying to capture elements of the actual output, like file names, -F
is even more useful:
C:Perforcetest>p4 -F %localPath% sync -n #1
c:Perforcetest.f1
c:Perforcetest1.15
c:Perforcetest1.18
c:Perforcetest2.f1
c:Perforcetest2.f2