• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 273
  • Last Modified:

Comma seprated array list

Hi,
I want to execute perforce command using comma separated multiple changelist in a single variable,
P4_SHELV_CLIST=16754,74589,25679,89023

Once I declare perforce cl to above variable P4_SHELV_CLIST it should one by one pick the changelist and execute perforce unshelve command. I tried something like this but it does not take comma separated list

for SHELV in "${ P4_SHELV_CLIST [@]}"
   do
    ( echo "User specified a shelved changelist, unshelving changelist ($P4_SHELV_CLIST)";

     ${P4_DIR}/p4 unshelve -f -s ${ P4_SHELV_CLIST } //${P4CLIENT}/...;
    )
     echo "Read Shelved CL = $SHELV";
   done

Open in new window


Also I need to verify if any perforce changelist is wrong than I should exit with proper message, if a wrong changelist is entert than perforce throws following message:

//p4_shelved_CL--847463304/... - no such file(s).

Here “p4_shelved_CL--847463304” value is from ${P4CLIENT}

Thanks in advance for inputs.
0
saharey
Asked:
saharey
  • 9
  • 9
1 Solution
 
MikeOM_DBACommented:
Try something similar to this:
IFS=','
P4_SHELV_CLIST="16754,74589,25679,89023"
for SHELV in $P4_SHELV_CLIST
do
    ( echo "User specified a shelved changelist, unshelving changelist ($P4_SHELV_CLIST)";

     echo "${P4_DIR}/p4 unshelve -f -s $SHELV } //${P4CLIENT}/...;"
    )
     echo "Read Shelved CL = $SHELV";
done

Open in new window

0
 
sahareyAuthor Commented:
Hi Mike,
Thanks for quick solution, this has worked, I have used it like this:
IFS=','
ORG_SHELV_ENTRY=16754,74589,25679,89023
P4_SHELV_CLIST="$ORG_SHELV_ENTRY"
for SHELV in $P4_SHELV_CLIST
do
    ( echo "User specified a shelved changelist, unshelving changelist ($SHELV )";

     ${P4_DIR}/p4 unshelve -f -s $SHELV } //${P4CLIENT}/...;
    )
     echo "Read Shelved CL = $SHELV";
done
echo "Now revert unshelve"
#Now revert unshelve but KEEP the files locally
${P4_DIR}/p4 revert -k //${P4CLIENT}/...

Open in new window

but how to exit the script when user has entered wrong changelist, say cl 25679 is wrong, perforce throws message “//p4_shelved_CL--847463304/... - no such file(s).”, this message will come when following perforce command will be executed

${P4_DIR}/p4 unshelve -f -s ${SHELV } //${P4CLIENT}/...;

Any help in this regard will be highly appreciated
0
 
MikeOM_DBACommented:
You check the "$?" return variable.
${P4_DIR}/p4 unshelve -f -s $SHELV } //${P4CLIENT}/...;
if [ $? -ne 0 ]; then
  echo "!Error: something occur..."
  exit 3
fi

Open in new window

Or like this:
result=`${P4_DIR}/p4 unshelve -f -s $SHELV } //${P4CLIENT}/...;`
if [[ `echo $result|grep 'Error'|wc -l` -gt 0 ]]; then
  echo "!Error: something occur..."
  exit 3
fi

Open in new window

0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
sahareyAuthor Commented:
$? return variable will not work as the message generated by Perforce command will always return true (0) because it is not Error it is informing user about changelist, grep command is also not working I used it like this:

IFS=','
ORG_SHELV_ENTRY=16754
P4_SHELV_CLIST="$ORG_SHELV_ENTRY"
# Unshelve if true
if [ P4_SHELV_CLIST != "" ] ; then
for SHELV in $P4_SHELV_CLIST
do
    ( echo "User specified a shelved changelist, unshelving changelist ($SHELV)";

     result=`${P4_DIR}/p4 unshelve -f -s ${SHELV} //${P4CLIENT}/...;`
     if [[ `echo $result|grep 'no such file'|wc -l` -gt 0 ]]; then
          echo "!Error: something occur..."
          exit 3
       fi 

    )
     echo "Read Shelved CL = $SHELV";
done
else (
      echo "Not running with a shelved changelist"; 
      exit 1;
     )
fi
echo "Now revert unshelve"
#Now revert unshelve but KEEP the files locally
${P4_DIR}/p4 revert -k //${P4CLIENT}/...

Open in new window


Output came as

user specified a shelved changelist, unshelving changelist (16754)
++ /builder/tools/perforce/linux/p4 unshelve -f -s 16754 //p4_shelved_CL--847463304/...

//p4_shelved_CL--847463304/... - no such file(s).
+ result=
++ echo
++ grep 'no such file'
++ wc -l
+ [[ 0 -gt 0 ]]
+ echo 'Read Shelved CL = 16754'
Read Shelved CL = 16754
+ echo 'Now revert unshelve'
Now revert unshelve

looks like somewhere I am doing minor mistake in code, any idea?
0
 
MikeOM_DBACommented:
Perhaps the program is sending the message to the "error" output (2)?
Try:
result=`${P4_DIR}/p4 unshelve -f -s ${SHELV} //${P4CLIENT}/...; 2>&1`

Open in new window

0
 
sahareyAuthor Commented:
No change in result, same output.
0
 
MikeOM_DBACommented:
Copy and paste the exact output from this command:
${P4_DIR}/p4 unshelve -f -s ${SHELV} //${P4CLIENT}/...;

Open in new window

0
 
sahareyAuthor Commented:
Here it is

When correct changelist is provided this is the output:

 P4_SHELV_CLIST=548607
+ echo 'User specified a shelved changelist, unshelving changelist (548607)'
User specified a shelved changelist, unshelving changelist (548607)
+ /builder/tools/perforce/linux/p4 unshelve -f -s 548607 //p4_shelved_CL--847463304/...
/dev/config/1.0.02/src/main/java/com/bread/cloud/config/BreadPropertyListener.java#1 - unshelved, opened for edit
+ echo 'Read Shelved CL = 548607'
Read Shelved CL = 548607


and when I enter changelist which is already committed or it is still under pending changelist (not shelved) it give following message:


+ echo 'User specified a shelved changelist, unshelving changelist (134220)'
User specified a shelved changelist, unshelving changelist (134220)
++ /builder/tools/perforce/linux/p4 unshelve -f -s 134220 //p4_shelved_CL--847463304/...
//p4_shelved_CL--847463304/... - no such file(s).
0
 
MikeOM_DBACommented:
If the "result=..." is not capturing "no such file(s).", then the program is not sending the message either to standard or error output.

PS: Make sure there are no spaces before and after the '='.
0
 
sahareyAuthor Commented:
I already ensured there are no spaces between ‘result=’ but still result output is empty, no idea what more can be done here, output is looks like this:


User specified a shelved changelist, unshelving changelist (134220)
++ /builder/tools/perforce/linux/p4 unshelve -f -s 134220 //p4_shelved_CL--847463304/...
//p4_shelved_CL--847463304/... - no such file(s).
+ result=
++ echo
++ grep 'no such file(s).'
++ wc -l
+ [[ 0 -gt 0 ]]
+ echo 'Read Shelved CL = 134220'

Is there any issue with the code I am working on?

IFS=','
ORG_SHELV_ENTRY=16754
P4_SHELV_CLIST="$ORG_SHELV_ENTRY"
# Unshelve if true

for SHELV in $P4_SHELV_CLIST
do
    ( echo "User specified a shelved changelist, unshelving changelist ($SHELV)";

     result=`${P4_DIR}/p4 unshelve -f -s ${SHELV} //${P4CLIENT}/...;`
     if [[ `echo $result|grep 'no such file'|wc -l` -gt 0 ]]; then
          echo "!Error: something occur..."
          exit 3
       fi 

    )
     echo "Read Shelved CL = $SHELV";
done

echo "Now revert unshelve"
#Now revert unshelve but KEEP the files locally
${P4_DIR}/p4 revert -k //${P4CLIENT}/...

Open in new window

0
 
MikeOM_DBACommented:
You using bash?
If so, try using ksh, find out where it is and add this as first line:
#!/bin/ksh ##-- or
#!/usr/bin/ksh ##-- or
#!/usr/sbin/ksh ##-- or wherever ksh is...
# 
IFS=','
ORG_SHELV_ENTRY=16754
P4_SHELV_CLIST="$ORG_SHELV_ENTRY"
# Unshelve if true

for SHELV in $P4_SHELV_CLIST
do
    #( <== Remove these parenthesis, they create new shell
    echo "User specified a shelved changelist, unshelving changelist ($SHELV)";

     result=`${P4_DIR}/p4 unshelve -f -s ${SHELV} //${P4CLIENT}/...;  2>&1`
     if [[ `echo $result|grep 'no such file'|wc -l` -gt 0 ]]; then
          echo "!Error: something occur..."
          exit 3
       fi 

    #) <== removed
     echo "Read Shelved CL = $SHELV";
done

echo "Now revert unshelve"
#Now revert unshelve but KEEP the files locally
${P4_DIR}/p4 revert -k //${P4CLIENT}/...

Open in new window

Notice some changes I made.
Good luck!
0
 
sahareyAuthor Commented:
I gave a try by removing parenthesis and also tried with kshell build no improvement in both situation getting same blank result.

Looks like their is no solution to this issue.
0
 
MikeOM_DBACommented:
That means that somehow the program is not sending the message to standard or error output.
0
 
sahareyAuthor Commented:
Yes looks like, but should have some or other  solution to this.
0
 
MikeOM_DBACommented:
As a test, try sending the output to a file.
Is the code a script or a program?
0
 
sahareyAuthor Commented:
this is shell script executed under Jenkins command line window which generates logs same as we see output on command line.
0
 
MikeOM_DBACommented:
Jenkins command line window which generates logs same as we
Seems the output is captured by that script and written to those logs.
You may have to either disable those logs or "grep" the message from those logs.
  . . .
    ${P4_DIR}/p4 unshelve -f -s ${SHELV} //${P4CLIENT}/...;  2>&1`
     if [[ `grep 'no such file'  ${P4_DIR}/p4.log |wc -l` -gt 0 ]]; then
          echo "!Error: something occur..."
          exit 3
       fi 
  . . .

Open in new window

0
 
sahareyAuthor Commented:
I already tried with this option but did not worked.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

  • 9
  • 9
Tackle projects and never again get stuck behind a technical roadblock.
Join Now