Help with if condition

o/p of my command is given below

My requirement is

if Pnumber is 0 then

	stabilization.Build.2013

else

stabilization.PBuild.2013.3

What have you tried so far?
Where is that you got stuck?

Dear nikhil jain,
I have a few to questions pose in response first:-

  • What have you tried so far?
  • What output/errors do you get?
  • What OS and version are you using?
  • What are your preferred tools? (C, shell, perl, awk, etc.)
  • What logical process have you considered? (to help steer us to follow what you are trying to achieve)

Most importantly, What have you tried so far?

There are probably many ways to achieve most tasks, so giving us an idea of your style and thoughts will help us guide you to an answer most suitable to you so you can adjust it to suit your needs in future.

We're all here to learn and getting the relevant information will help us all, and you have over 100 posts, so I'm sure you know how to ask a good question.

Regards,
Robin

sorry for the whole lot of confusion,

Release : 2014Q1
Branch : stabilization
BuildNumber : 2013
PNumber : 3

Whenever Pnumber is other than 0 then i want it to display as stabilization.PBuild.2013.$PNumber i.e. the value whatever PNumber is having after delimiter ":"
in this case it is 3.
If PNumber : 0 then it should display as stabilization.Build.2013

Sorry Again.

So, what have you tried so far and what errors do you get?

If the output you are showing us is the right detail but the wrong format, it would be useful to know how you are getting the output you have shared.

You know when you your car breaks down, do you just ring them and say "It doesn't work." can they diagnose and fix it without know what is really wrong or even seeing the car?

Please tell us:-

  • What have you tried so far?
  • What output/errors do you get?
  • What OS and version are you using?
  • What are your preferred tools? (C, shell, perl, awk, etc.)
  • What logical process have you considered? (to help steer us to follow what you are trying to achieve)

Most importantly, What have you tried so far?

There are probably many ways to achieve most tasks, so giving us an idea of your style and thoughts will help us guide you to an answer most suitable to you so you can adjust it to suit your needs in future.

We're all here to learn and getting the relevant information will help us all.

Regards,
Robin

set -x
#find /opt/c/Com/depots -name "version.info" > a.txt
 
while read line
do
cat $line > detail.txt
var=`cat detail.txt|awk -F ":" '/PatchNumber/ {print $2}'`
if [ $var == "0" ]
then
echo "stabilization.Build.2013"
else
echo "stabilization.PatchBuild.2013.$var"
fi
done < a.txt

a.txt contains

/a/b/c/v.info
/d/e/f/v.info
...
...

v.info contains

Release : 2014Q1
Branch : stabilization
BuildNumber : 2013
PNumber : 3

but PNumber keeps changing based on v.info file

I'm afraid your code snippet won't print either of your strings, as var will be empty (not "0"!) due to the wrong pattern: "PatchNumber" will not be found in v.info.
Should var contain any integer number, and with not too old a shell, try (no if [ ... ] needed)

VX=${var##0}; echo stabilization.${VX:+Patch}Build.2013${VX:+.}$VX
stabilization.Build.2013
stabilization.PatchBuild.2013.3

Right, we've something to work from now.

Do you just want the one line output, or the whole Release/Branch/Build & PNumber (adjusted)

I'm not too sure why you use cat $line > detail.txt at all. cp $line detail.txt would do, but you could eliminate this and just use $line in the next statement (and remove the cat there too):-

var=`awk -F ":" '/PNumber/ {print $2}' $line`

.... or even:-

grep "PNumber" $line | read zzz var

Although perhaps a little inefficient (and we've all done stuff like this) nothing seems to be 'wrong' apart from seaching for the PatchNumber strings rather than PNumber, but it just depends what you actually want to see.

If you have three files in your loop, what output do you want to get? You are probably very close already.

Robin

stabilization.PatchBuild.2013. 6
stabilization.PatchBuild.2013. 6
stabilization.PatchBuild.2013. 7
stabilization.PatchBuild.2013. 7

I get the o/p as above but i don't want space between 2103. &$var

i wan it as shown below

stabilization.PatchBuild.2013.6
stabilization.PatchBuild.2013.6
stabilization.PatchBuild.2013.7
stabilization.PatchBuild.2013.7
while read line
do
cp $line detail.txt
var=`cat detail.txt|awk -F ":" '/PNumber/ {print $2}'`
if [ $var == 00 ]
then
echo "stabilization.Build.2013"
else
echo "stabilization.PatchBuild.2013.$var"
fi
done < a.txt

The variable var appears to be a string with a leading blank. Perhaps one of the other suggestions to get the value set will do the trick, but at worst we could use shell functions to remove the leading space like this:-

:
:
else
   echo "stabilization.PatchBuild.2013.${var# }"
fi
:
:

This will trim a single leading space, if there is one. If you want to trim off everything up to the last space (in case you have more) you can use ${var## } instead, but beware if your variable really does contain spaces that you want.

Robin

robin,

thanks for the solution but this gives me o/p as

stabilization.Build.2013
stabilization.Build.2013
./verison.sh: line 13: stabilization.PatchBuild.2013.${var #}: bad substitution

---------- Post updated at 06:02 PM ---------- Previous update was at 05:52 PM ----------

got the solution

thanks for all ur help

var=`cat detail.txt|awk -F ":" '/PatchNumber/ {print $2}'|tr -d ' '`

Thanks nikhil_jain.