Solved

Comma seprated array list

Posted on 2014-03-26
18
236 Views
Last Modified: 2014-07-14
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
Comment
Question by:saharey
  • 9
  • 9
18 Comments
 
LVL 29

Accepted Solution

by:
MikeOM_DBA earned 200 total points
ID: 39957108
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
 

Author Comment

by:saharey
ID: 39959575
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
 
LVL 29

Expert Comment

by:MikeOM_DBA
ID: 39959768
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
On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

 

Author Comment

by:saharey
ID: 39961385
$? 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
 
LVL 29

Expert Comment

by:MikeOM_DBA
ID: 39961404
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
 

Author Comment

by:saharey
ID: 39961710
No change in result, same output.
0
 
LVL 29

Expert Comment

by:MikeOM_DBA
ID: 39966726
Copy and paste the exact output from this command:
${P4_DIR}/p4 unshelve -f -s ${SHELV} //${P4CLIENT}/...;

Open in new window

0
 

Author Comment

by:saharey
ID: 39969059
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
 
LVL 29

Expert Comment

by:MikeOM_DBA
ID: 39969285
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
 

Author Comment

by:saharey
ID: 39974326
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
 
LVL 29

Expert Comment

by:MikeOM_DBA
ID: 39976100
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
 

Author Comment

by:saharey
ID: 39977560
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
 
LVL 29

Expert Comment

by:MikeOM_DBA
ID: 39978002
That means that somehow the program is not sending the message to standard or error output.
0
 

Author Comment

by:saharey
ID: 39981224
Yes looks like, but should have some or other  solution to this.
0
 
LVL 29

Expert Comment

by:MikeOM_DBA
ID: 39983863
As a test, try sending the output to a file.
Is the code a script or a program?
0
 

Author Comment

by:saharey
ID: 39986540
this is shell script executed under Jenkins command line window which generates logs same as we see output on command line.
0
 
LVL 29

Expert Comment

by:MikeOM_DBA
ID: 39986892
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
 

Author Comment

by:saharey
ID: 39988187
I already tried with this option but did not worked.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How do I monitor a folder and email changes once a day? 12 102
Strange html entity encoding by tidy 14 100
incorrect file reading in bash 7 141
Using an inventory file in a script 7 41
This Windows batch file is useful for organizing image files from a digital camera or other source, but can have many other uses.  It simply renames the file(s) to match their create date.  For example, if you took a picture today at 1:40pm and the …
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question