-1

I'm working on a deploy script which wants to update a symlink atomically. I intend to use a command like

$ ln -s new current_tmp && mv -Tf current_tmp current

(Credit to here for details on why this works).

However, not all versions of mv support the -T option. How can I reliably determine if a system supports it before using it?

Is the a better way than creating a different file and attempting to mv that file instead using the -T option (and checking if that was successful) so I can know if the current system supports that option? Then I can run the correct command for that system.

Thanks for your help.

Luke Cousins
  • 377
  • 1
  • 3
  • 18

1 Answers1

1

In general you can

  • write portable code by not relying on specific extentions beyond what every UNIX will support (which is a PITA as only the -f and -i option are mandated for mv and similar limitations exist for other utilities...)
  • Most people only have a limited selection of operating systems and versions and don't need to write truly universal scripts, just determine which major version in you're dealing with in your deployment tool and act accordingly. Similar to what a tool such as facter already does for you in Puppet.
  • assume that a feature is present and catch the error in case your command fails (which you should be doing regardless) and then fall back to a work around.
  • check the version string of a command beforehand.
    If the -T or --no-target-directory feature is specific to the GNU coreutils version of mv you could check in git when it was introduced , find 2004 and then determine you need a version of coreutils more recent than 5.2.1 released twelve years ago. .
HBruijn
  • 72,524
  • 21
  • 127
  • 192