title: Define Version with script in RPM spec file
body: <p>I have an RPM Spec file, building on rhel7 with rpmbuild, where I would like to define the Version with a script. </p>
  
  <p>I read here <a href="http://www.techrepublic.com/article/rpmproc-spec-file/" rel="noreferrer">http://www.techrepublic.com/article/rpmproc-spec-file/</a> , That I can do this:</p>
  
  <pre><code>%define version 1.2
  
  Version: %{version}
  </code></pre>
  
  <p>And here <a href="https://stackoverflow.com/questions/10234053/rpm-spec-file-is-it-possible-to-dynamically-populate-a-spec-file-variable">RPM spec file - Is it possible to dynamically populate a spec file variable</a> , That I can define with a script: </p>
  
  <pre><code>%define whoami %(cmd)
  </code></pre>
  
  <p>So I tried to do this in my Spec File:</p>
  
  <pre><code>%define version %(echo "$(sed -n 's|^[ ]*appVersion = "\(.*\)"|\1|p' /fullfilepath/values.txt | sed 's/^\(.*\)-.*$/\1/')")
  
  Version: %{version}  **Line 23**
  </code></pre>
  
  <p>But I get a</p>
  
  <pre><code>error: line 23: Empty tag: Version:
  </code></pre>
  
  <p>Things I have tested so far:</p>
  
  <pre><code>%define version %(echo "12") --basic script works ok, version becomes 12
  
  //As a command straight in terminal
  $ echo "$(sed -n 's|^[ ]*appVersion = "\(.*\)"|\1|p' /fullfilepath/values.txt | sed 's/^\(.*\)-.*$/\1/')"
  //returns 1.2
  </code></pre>
  
  <p>These work well, So I have no idea what it could be thats causing it to fail.   Any ideas what could be casuing it to fail when I call the same thing in define tag in spec file?</p>
  
  <p><strong>Update</strong></p>
  
  <p>I tried replacing the file name with the actual value so it looks like this </p>
  
  <pre><code>echo "$(sed -n 's|^[ ]*appVersion = "\(.*\)"|\1|p' &lt;&lt;&lt; "appVersion = \"1.2-SNAPSHOT\"" | sed 's/^\(.*\)-.*$/\1/')"
  </code></pre>
  
  <p>That works when called in terminal but as a  </p>
  
  <pre><code>%(echo "$(sed -n 's|^[ ]*appVersion = "\(.*\)"|\1|p' &lt;&lt;&lt; "appVersion = \"1.2-SNAPSHOT\"" | sed 's/^\(.*\)-.*$/\1/')") 
  </code></pre>
  
  <p>but I still get the </p>
  
  <pre><code>Empty tag: Version: Error
  </code></pre>
  
  <p><strong>Update 2</strong></p>
  
  <p>I tested a different more complex command then <code>echo "12"</code>:</p>
  
  <pre><code>%define version %(echo "$(git log -1 | grep commit | awk -F"commit " '{print $2}' | cut -c1-8)")
  </code></pre>
  
  <p>This works ok too! Makes the version the 7 first digits of the commit hash. </p>
  
  <p><strong>Update 3</strong></p>
  
  <p>The mystery continues, I did a test to check if its the sed command thats the cause but the following command gives me 1.2 as the version</p>
  
  <pre><code>%define version %(echo "$( sed 's/.*= //' &lt;&lt;&lt; "appVersion = 1.2" )")
  </code></pre>
  
  <p>If this command works but not my first one, then its got to be with something in my first command that only works when called directly in terminal and not in %(cmd). Getting closer!</p>
  
  <p><strong>Update 4</strong></p>
  
  <p>Ok so I seem to have isolated what it must be, curious, looks like it could be the <code>-n</code> or the <code>s| | \1 |p</code> syntax that rpmbuild doesn't like. I made a more simpler version of my original. Check it out:</p>
  
  <pre><code>#Error, doesn`t set version to 1.2
  %define version %(echo "$( sed -n 's|^.*-\(-*\)|\1|p' &lt;&lt;&lt; "foo-1.2" )") 
  
  #Works ok! sets version to 1.2
  %define version %(echo "$( sed 's/.*= //' &lt;&lt;&lt; "appVersion = 1.2" )") 
  </code></pre>
  
  <p>Unfortunately though I don't think I can do anymore to isolate and figure out what the issue is. Theres nothing wrong with using a sed in the style of the second command but its still very interesting as to why the first command doesn't work. </p>
  
  <p><strong>Update 5</strong></p>
  
  <p>I have discovered that there is some deep issue here when working with any script inside %() with a spec file and rpmbuild. I tried using awk just to see what would happen and it too breaks! This goes way deeper then I initially thought, like discovering a conspiracy: </p>
  
  <pre><code>#In terminal it prints 1.2-SNAPSHOT, but in Spec it's an error 
  %define version %(echo "$(awk '/appVersion /{ print $3 }' &lt;&lt;&lt; "appVersion = \"1.2-SNAPSHOT\"" | tr -d \")")
  
  sh: -c: line 0: unexpected EOF while looking for matching `)'
  sh: -c: line 1: syntax error: unexpected end of file
  error: line 23: Empty tag: Version:
  </code></pre>
  
  <p><strong>Update 6</strong></p>
  
  <p>Good news and bad news for everyone, I found that rpm seems to be doing some of its own work in the backround and not showing what its doing, I finally found a command that gives different values when called through rpm: </p>
  
  <pre><code>%define version %(echo "$(awk '/midonetVersion /{ print $3 }' &lt;&lt;&lt; "midonetVersion = \"5.1-SNAPSHOT\"")")
  #In terminal it echos "5.1-SNAPSHOT" (literally wrapped in "" )
  #When in spec it set version to 5.1-SNAPSHOT , rpmbuild is removing the ""
  </code></pre>
  
  <p>So now I made an adjustment and called this: </p>
  
  <pre><code>#echos "5.1 in terminal and sets version to 5.1 in spec
  %define version %(echo "$(awk '/appVersion /{ print $3 }' &lt;&lt;&lt; "appVersion = \"1.2-SNAPSHOT\"")"| cut -d'-' -f1)
  </code></pre>
  
  <p>So from looking at this, I think there maybe is a similar kind of behind the scenes parsing of the result of my first sed command from rpm. We will have our way rpm!</p>
  
  <p><strong>Final Update</strong></p>
  
  <p>A truce was made with rpm, I am going to use this command instead:</p>
  
  <pre><code>%define version %(echo "$(awk '/ appVersion =/{ print $3 }' /filepath/values.txt" | sed 's/\"//g' | cut -d'-' -f1)
  </code></pre>
  
  <p>It does the same thing as my first command and works inside specfile setting the version number correctly. If anyone has any guess as to why the first command wouldn't run I would be thrilled to read it. Peace!</p>