my_question
my_question

Reputation: 3235

How to check if a perforce file is opened using bash script

So I run "p4 opened the_file", it prints the status, but I cannot capture the print:

$ a=`p4 opened file1`
file1 - file(s) not opened on this client.
$ echo $a

Variable a is empty.

What I want is, I can get hold of the string "file(s) not opened on this client" and search for "not opened".

Any way to do that?

Upvotes: 0

Views: 806

Answers (3)

Drew Marold
Drew Marold

Reputation: 185

if [[ -z "$(p4 -ztag opened $myfile)" ]]; then echo "Not opened"; fi

p4 -ztag opened won't return anything for a file that isn't open, so you can just test for empty output.

Upvotes: 0

Samwise
Samwise

Reputation: 71454

If you do:

p4 -s opened file1

all the server output gets sent to stdout and prefixed with a tag saying whether it's "error" or "info" (be careful, it's not always obvious where the distinction is). For your case that should get you something like:

error: file1 - file(s) not opened on this client.
exit: 0

Another fun global option (i.e. it goes before the command name, same as the "-s") is "-e", which gives you the raw error dict; you can do interesting things with this like look for a specific error code rather than grepping human-readable message strings.

Upvotes: 1

Bryan Pendleton
Bryan Pendleton

Reputation: 16349

Change the first line to:

a=`p4 opened file1 2>&1`. 

That redirects stderr into stdout, so your variable will capture both normal output and error output.

Upvotes: 1

Related Questions