brokenman wrote:Verdict: No good for getting version if your file name has a name-with-a-dash
the filename pattern proposed was name-version-suffix where name would be any chars, version only numbers with dots and suffix two dotted names. That is, there's no version suffix like -x86_64-1 after version and before extension.
If this condtition is true or at least separators used in that version suffix are different and not used in name section (and providing version suffix is part of version) you can solve the name-with-a-dash problem (or generally name-with-a-separator problem) with this code:
supposing sepator chars allowed in name are - and : while not allowed in version suffix (using for example _ and +). Note you must explicity avoid . in brackets because it's used in version number.
proof of concept:
Code: Select all
% F=openldap-client-2.4.42+x86_64+1.tar.xz
% V=${F%.*.*}
% V=${V##*[-:]}
% echo $V
2.4.42+x86_64+1
% F=openldap-client:bundle-2.4.42+x86_64+1.tar.xz
% V=${F%.*.*}
% V=${V##*[-:]}
% echo $V
2.4.42+x86_64+1
If the two preconditions are not satisfized then you need a more advanced program (awk, sed...) or advanced bash options (extglob) using this code:
Now you can also cover all extension possibilities (all like tar.xz , .tgz , .tar.gz.crypt , .why.not.a.bit.longer.1more ) with the code:
Code: Select all
shopt -s extglob
V=${F%%+(.+([[:alnum:]]))}
V=${V##+(+([[:alpha:]])[-:_.+])}
this way you have no conflict with version or suffix separator chars:
Code: Select all
% shopt -s extglob
% F=openldap-client-2.4.42+x86_64+1.tar.xz
% V=${F%.*.*}
% echo ${V##+(+([[:alpha:]])[-:_.+])}
2.4.42+x86_64+1
% F=openldap-client:bundle-2.4.42+x86_64+1.tar.xz
% V=${F%.*.*}
% echo ${V##+(+([[:alpha:]])[-:_.+])}
2.4.42+x86_64+1
% F=openldap-client:bundle.new-2.4.42-x86_64-1.tar.xz
% V=${F%.*.*}
% echo ${V##+(+([[:alpha:]])[-:_.+])}
2.4.42+x86_64+1
% F=openldap-client.2.4.42-x86_64-1.tar.xz1
% V=${V##+(+([[:alpha:]])[-:_.+])}
% echo ${V##+(+([[:alpha:]])[-:_.+])}
2.4.42-x86_64-1
% F=openldap.client.2.4.42-x86_64-1.tgz
% V=${F%%+(.+([[:alnum:]]))}
% echo ${V##+(+([[:alpha:]])[-:_.+])}
2.4.42-x86_64-1
% F=openldap.client.2.4.42-x86_64-1.why.not.a.bit.longer.1more
% V=${F%%+(.+([[:alnum:]]))}
% echo ${V##+(+([[:alpha:]])[-:_.+])}
2.4.42-x86_64-1
% F=openldap-client_bundle+kiosk-2.4.42-x86_64-1.tar.gz.crypt.2
% V=${F%%+(.+([[:alnum:]]))}
% V=${V##+(+([[:alpha:]])[-:_.+])}
% echo $V
2.4.42-x86_64-1