linkyndy
linkyndy

Reputation: 17920

Add a flag in bash only if the command's version supports it

I have a command such: ... | sort -u -V. It works on many of the latest versions of sort, but some of my machines I run the command on have a very old version of sort that does not support the -V flag.

How can I conditionally set the -V flag only if the sort command accepts it?

Upvotes: 0

Views: 118

Answers (2)

Vytenis Bivainis
Vytenis Bivainis

Reputation: 2376

I guess this funny command might work:

SUPPORTS=$(man sort | grep "^\s*\-V" &> /dev/null && echo Y || echo N)

Upvotes: 1

fedorqui
fedorqui

Reputation: 290025

You can maybe do:

if $(sort -u -V test_file &>/dev/null)
then
    ... | sort -u -V
fi

Because sort -u -V will return an error exit code if it is not available.

Upvotes: 2

Related Questions