<p>I want to use a file to store the current version number for a piece of customer software which can be used by a start-up script to run the binary in the correct directory.</p>

<p>For Example, if the run directory looks like this:</p>

<pre><code>.
..
1.2.1
1.2.2
1.3.0
run.sh
current_version
</code></pre>

<p>And current_version contains:</p>

<pre><code>1.2.2
</code></pre>

<p>I want <code>run.sh</code> to descend into 1.2.2 and run the program <code>foo</code>.</p>

<p>The current solution is this:</p>

<pre><code>#!/bin/sh
version = `cat current_version`
cd $version
./foo
</code></pre>

<p>It works but is not very robust.  It does not check for file existence, cannot cope with multiple lines, leading spaces, commented lines, blank files, etc.</p>

<p>What is the most survivable way to do this with either a shell or perl script?</p>

[{'answer_id': 66055, 'author': 'DustinB', 'author_id': 7888, 'author_profile': 'https://Stackoverflow.com/users/7888', 'pm_score': 3, 'selected': False, 'text': '<p>That\'s a common approach.  You can check for dir/file existence using test operators such as "if [ -d "somedirectory" ]; then" or [ -t "somefile" ]</p>\n\n<p>I use symbolic links more often, though.  Then, you can just change your symbolic link to use the version you want.</p>\n\n<p>For example,</p>\n\n<pre>\n$ ln -s 1.2.2 current_version\n$ ls -al\ntotal 20\ndrwxr-xr-x 5 dbreese dbreese 4096 2008-09-15 13:34 .\ndrwxr-xr-x 3 dbreese dbreese 4096 2008-09-15 13:34 ..\ndrwxr-xr-x 2 dbreese dbreese 4096 2008-09-15 13:34 1.2.1\ndrwxr-xr-x 2 dbreese dbreese 4096 2008-09-15 13:34 1.2.2\ndrwxr-xr-x 2 dbreese dbreese 4096 2008-09-15 13:34 1.3.0\nlrwxrwxrwx 1 dbreese dbreese    6 2008-09-15 13:34 current_version -> 1.2.2/\n</pre>\n\n<p>Then your script can just use "cd current_version".</p>\n'}, {'answer_id': 66072, 'author': 'Nicholas Mancuso', 'author_id': 8945, 'author_profile': 'https://Stackoverflow.com/users/8945', 'pm_score': 0, 'selected': False, 'text': '<p>I would change the script to accept an argument. The argument should be a filename. Open the file using whatever scripting language you prefer [perl, python] and traverse the file until  you find a match for your version.</p>\n\n<p>I would use a regex... something along the lines of /\\d\\.\\d\\.\\d/ . then have it execute the application through your script.</p>\n'}, {'answer_id': 66086, 'author': 'Adam Rosenfield', 'author_id': 9530, 'author_profile': 'https://Stackoverflow.com/users/9530', 'pm_score': 0, 'selected': False, 'text': '<p>You can check for existence using the &amp;&amp; and || operators:</p>\n\n<pre>\n!$/bin/sh\nversion = `cat current_version`\ncd $version && ./foo || echo "Bad version"\n</pre>\n\n<p>The &amp;&amp; operator causes the second statement to only execute if the first one succeeds (exit status 0), and the || operator causes the second statement to execute only if the first one fails (exit status non-zero).</p>\n\n<p>I\'m not sure what you mean by coping with multiple lines, leading spaces, or commented lines.</p>\n'}, {'answer_id': 66087, 'author': 'slipset', 'author_id': 9422, 'author_profile': 'https://Stackoverflow.com/users/9422', 'pm_score': -1, 'selected': False, 'text': '<pre><code>!#/bin/sh\n\nif [ -e \'current_version\' ]; then\n    version=`cat current_version`;\n    version=`echo $version | tr -ds [[:blank:]]`\n    if [ -n "$version" ]; then\n      if [ -d "$version" ]; then\n         cd "$version"\n      else\n         echo $version is not a directory\n      fi\n    else\n      echo version_file contained only blanks\n    fi\nelse \n   No file named current_version exists\nfi\n</code></pre>\n'}, {'answer_id': 66124, 'author': 'Peter Boughton', 'author_id': 9360, 'author_profile': 'https://Stackoverflow.com/users/9360', 'pm_score': 0, 'selected': False, 'text': '<p>If the versioning will always be in #.#.# format, you could do this:</p>\n\n<pre><code>ls | grep ^[0-9]\\.[0-9]\\.[0-9]$ | sort -nr | head -n 1\n</code></pre>\n\n<p>That will list the versions in descending numerical order, then selects the first of those</p>\n'}, {'answer_id': 72602, 'author': 'Peter Stuifzand', 'author_id': 1633, 'author_profile': 'https://Stackoverflow.com/users/1633', 'pm_score': 0, 'selected': False, 'text': '<p>What about:</p>\n\n<pre><code>#!/usr/bin/perl -w\nuse strict;\nuse warnings;\n\nmy $version_file = \'current_version\';\nopen my $fh, \'&lt;\', $version_file or die "Can\'t open $version_file: $!";\n\n# Read version from file\nmy $version = &lt;$fh&gt;;\nchomp $version;\n\n# Remove whitespace (and match version)\ndie "Version format not recognized" if $version !~ m/(\\d+\\.\\d+\\.\\d+)/;\n\nmy $dir = $1;\ndie "Directory not found: $dir" unless -d $dir;\n\n# Execute program in versioned directory.\nchdir $dir;\nsystem("./foo");\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/65994', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/7476/']