<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>

That's a common approach.  You can check for dir/file existence using test operators such as "if [ -d "somedirectory" ]; then" or [ -t "somefile" ]
I use symbolic links more often, though.  Then, you can just change your symbolic link to use the version you want.
For example,
```
$ ln -s 1.2.2 current_version
$ ls -al
total 20
drwxr-xr-x 5 dbreese dbreese 4096 2008-09-15 13:34 .
drwxr-xr-x 3 dbreese dbreese 4096 2008-09-15 13:34 ..
drwxr-xr-x 2 dbreese dbreese 4096 2008-09-15 13:34 1.2.1
drwxr-xr-x 2 dbreese dbreese 4096 2008-09-15 13:34 1.2.2
drwxr-xr-x 2 dbreese dbreese 4096 2008-09-15 13:34 1.3.0
lrwxrwxrwx 1 dbreese dbreese    6 2008-09-15 13:34 current_version -> 1.2.2/
```
Then your script can just use "cd current_version".