title: get file versions using bash script in linux
body: <p>I have this code on my <code>Windows</code> machine:</p>
  <pre><code>FileName = &quot;C:\Windows\notepad.exe&quot;
  Set objFs = WScript.CreateObject(&quot;Scripting.Filesystemobject&quot;)
  If objFs.FileExists(FileName) Then
      If objFs.GetFileVersion(FileName) &lt;&gt; &quot;&quot; Then
          WScript.echo objFs.GetFileVersion(FileName)
      Else
          WScript.echo &quot; File Version not fond&quot;
      End If
  End If
  </code></pre>
  <p>I'm trying to write a <code>bash</code> script for my <code>Linux</code> machine in similar lines. Of-course it doesn't have a <code>GetFileVersion</code> built-in function, so I'm trying to write a user defined function- something like this:</p>
  <pre><code>#!/bin/bash
  
   
  GetFileVersion () {
          echo $1 | awk -F&quot;/&quot; '{print $NF}' | grep -E &quot;.*\.so$|.*\.so\..*&quot; &gt; /dev/null 2&gt;&amp;1
          if [ $? -ne 0 ]; then
                  file $1 | grep -w &quot;ELF&quot; &gt; /dev/null 2&gt;&amp;1
                  if [ $? -eq 0 ]; then
                          ver=$($1 --version | head -1)
                  else
                          file $1 | grep &quot;ASCII text executable&quot; &gt; /dev/null 2&gt;&amp;1
                          if [ $? -eq 0 ]; then
                                  $1 --version &gt; /dev/null 2&gt;&amp;1
                                  if [ $? -eq 0 ]; then
                                          ver=$($1 --version | head -1)
                                  else
                                          ver=&quot;NA&quot;
                                  fi
                          else
                                  ver=&quot;NA&quot;
                          fi
                  fi
          else
                  ver=&quot;NA&quot;
          fi
          echo $ver
  
  }
  
   
  fileName=$1
  if [ -f &quot;${fileName}&quot; ]; then
          fileVersion=$(GetFileVersion &quot;${fileName}&quot;)
          echo &quot;$fileVersion|True&quot;
  elif [ -d &quot;${full_path}&quot; ]; then
          echo &quot;NA|True&quot;
  else
          echo &quot;NA|False&quot;
  fi
  </code></pre>
  <p>Works well for executables under /bin, /usr/bin, /sbin etc., which have the <code>--version</code> option working for them. I want to however extend the function to get versions for <code>.so</code> files, package files also.</p>
  <p>for <code>.so</code> files I'm trying to parse something out of <code>ldconfig -v</code> output, but no luck so far. Also the output is confusing at times, so not sure which one to be considered a version:</p>
  <p>Example:</p>
  <p><code>libldap-2.4.so.2 -&gt; libldap-2.4.so.2.10.7</code></p>
  <p>In this case, <code>2.4</code> or <code>2.10.7</code> should be considered the right version for the <code>.so</code> file ?</p>