title: How to increment version number in a shell script?
body: <p>The following simple version control script is meant to find the last version number of a given file, increment it, run a given command with the newly created file (e.g., editor), and after that save it to stable. Since it's simple, it doesn't check anything since the script would be modified as needed. For instance, if the result won't be stable, the user can omit the last argument.</p>
  
  <p>However, one major concern of the current functionality is how to implement the following: if the last section after dot has two digits, inc untill 99; if only 1, then inc until 9, then move to the previous section. The versions may have any positive integer number of sections.</p>
  
  <pre><code>1.2.3.44 -&gt; 1.2.3.45
  1.2.3.9 -&gt; 1.2.4.0
  1.2.3 -&gt; 1.2.4
  9 -&gt; 10
  </code></pre>
  
  <p>The remaining issue is that it doesn't wait for a tabbed wine editor to close the file; the goal is to detect when the tab is closed. Also, could you explain how best to make sure that my variable names don't overwrite existing ones?</p>
  
  <p>You can also offer other improvements.</p>
  
  <pre><code>#!/bin/bash
  #Tested on bash 4.1.5
  #All arguments in order: "folder with file" "file pattern" cmd [stable name]
  folder="$1"
  file_pattern="$2"
  cmd="$3"
  stable="$4"
  
  cd "$folder"
  last_version=$(ls --format=single-column --almost-all | \
      grep "$file_pattern" | \
      sed -nr 's/^[^0-9]*(([0-9]+\.)*[0-9]+).*/\1/p' | \
      sort -Vu | \
      tail -n 1)
  last_version_file=$(ls --format=single-column --almost-all | \
      grep "$file_pattern" | \
      grep $last_version | \
      tail -n 1) #tail -n 1 is only needed to get 1 line if there are backup files with the same version number
  new_version=$(echo $last_version | \
      gawk -F"." '{$NF+=1}{print $0RT}' OFS="." ORS="") #increments last section indefinitely
  new_version_file=$(echo "$last_version_file" | \
      sed -r "s/$last_version/$new_version/")
  cp "$last_version_file" "$new_version_file"
  "$cmd" "$new_version_file" &amp; \
      wait #works with gedit but not with wine tabbed editor
  [[ "$stable" ]] &amp;&amp; \
      cp "$new_version_file" "$stable" #True if the length of string is non-zero.
  </code></pre>
  
  <p>Update:
  The following works on my pc, I will update it if improvements or solutions to unsolved problems are found:</p>
  
  <pre><code>#!/bin/bash
  inc()
  {
  shopt -s extglob
      num=${last_version//./}
      let num++
  
      re=${last_version//./)(}
      re=${re//[0-9]/.}')'
      re=${re#*)}
  
      count=${last_version//[0-9]/}
      count=$(wc -c&lt;&lt;&lt;$count)
      out=''
      for ((i=count-1;i&gt;0;i--)) ; do
          out='.\\'$i$out
      done
  
      sed -r s/$re$/$out/ &lt;&lt;&lt;$num
  }
  
  folder="$1"
  file_pattern="$2"
  cmd="$3"
  stable="$4"
  
  cd "$folder"
  last_version=$(ls --format=single-column --almost-all | \
      grep "$file_pattern" | \
      sed -nr 's/^[^0-9]*(([0-9]+\.)*[0-9]+).*/\1/p' | \
      sort -Vu | \
      tail -n 1) #--almost-all do not list implied . and ..
  last_version_file=$(ls --format=single-column --almost-all | \
      grep "$file_pattern" | \
      grep $last_version | \
      tail -n 1) #tail -n 1 is only needed to get 1 line if there are backup files with the same version number
  new_version=$(inc)
  new_version_file=$(echo "$last_version_file" | \
      sed -r "s/$last_version/$new_version/")
  cp "$last_version_file" "$new_version_file"
  "$cmd" "$new_version_file" &amp;&amp; \
      wait #works with gedit but not tabbed wine editor
  [[ "$stable" ]] &amp;&amp; \
      cp "$new_version_file" "$stable" #True if the length of string is non-zero.
  </code></pre>
  
  <p>I appreciate the variety of solutions that have been offered, for they help with gaining a perspective and drawing a comparison.</p>