0

I am trying to get the max version number from a directory where i have several versions of one program

for example if output of ls is

something01_1.sh
something02_0.1.2.sh
something02_0.1.sh
something02_1.1.sh
something02_1.2.sh
something02_2.0.sh
something02_2.1.sh
something02_2.3.sh
something02_3.1.2.sh
something.sh

I am getting the max version number with the following -

ls somedir | grep some_prefix | cut -d '_' -f2 | sort -t '.' -k1 -r | head -n 1

Now if at the same time i want to check it with the version number which i already have in the system, whats the best way to do it...

in bash i got this working (if 2.5 is the current version)

(ls somedir | grep some_prefix | cut -d '_' -f2; echo 2.5) | sort -t '.' -k1 -r | head -n 1

is there any other correct way to do it?

EDIT: In the above example some_prefix is something02.

EDIT: Actual Problem here is

(ls smthing; echo more) | sort 

is it the best way to merge output of two commands/program for piping into third.

2
  • 1
    Did you consider using a real version control system like git ???? Mar 15, 2013 at 21:09
  • That decision was not mine to make.
    – Mohit
    Mar 15, 2013 at 22:04

3 Answers 3

9

I have found the solution. The best way it seems is using process substitution.

cat <(ls smthing) <(echo more) | sort

for my version example

cat <(ls somedir | grep some_prefix | cut -d '_' -f2) <(echo 2.5) | sort -t '.' -k1 -r | head -n 1

for the benefit of future readers, I recommend - please drop the lure of one-liner and use glob as chepner suggested.

Almost similar question is asked on superuser.

more info about process substitution.

2

Is the following code more suitable to what you're looking for:

#/bin/bash

highest_version=$(ls something* | sort -V | tail -1 | sed "s/something02_\|\.sh//g")
current_version=$(echo $0 | sed "s/something02_\|\.sh//g")

if [ $current_version > $highest_version ]; then
    echo "Uh oh! Looks like we need to update!";
fi
9
  • Does that accomplish what you need? Mar 15, 2013 at 20:52
  • are you sure? In every shell I know $0 is the name of the running program, not it's version number. Good luck to all!
    – shellter
    Mar 15, 2013 at 20:57
  • @Paul - Sorry. But i think you misunderstood my question. What i meant is i want to get the max version number among the files present in the directory and the one i am running. I am not doing this check in the program in question. as mentioned in the example. I am running version 2.5 of something02, which may or may not be present in the update directory. And i need to figure out if the current running is the latest or not and if i need replacement. replacement is expensive for me. i cant just start the latest without a check first.
    – Mohit
    Mar 15, 2013 at 20:57
  • Ahhh.. I see what you mean Mar 15, 2013 at 21:02
  • 1
    mywiki.wooledge.org/ParsingLs explains the problems with, and alternatives to, parsing the output of ls.
    – chepner
    Mar 15, 2013 at 22:27
1

You can try something like this :

#! /bin/bash

lastversion() { # prefix
  local prefix="$1" a=0 b=0 c=0 r f vmax=0

  for f in "$prefix"* ; do
    test -f "$f" || continue
    read a b c r <<< $(echo "${f#$prefix} 0 0 0" | tr -C '[0-9]' ' ')
    v=$(((a*100+b)*100+c))
    if ((v>vmax)); then vmax=$v; fi
  done
  echo $vmax  
}

lastversion "something02"

It will print: 30102

1
  • thanks for the answer. I ended up doing something same in perl. But the question still remains open. How to do ### (ls smthing; echo more) | sort ### with only pipes.
    – Mohit
    Mar 17, 2013 at 17:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.