Shell script to sort and execute files sequentially

Hi,

I want to sort my files under a directory and execute them sequentially.
For ex:

my diir contains files: a_5.sql, ab_2.sql,abc_3.sql, acbd_1 ,ab_4.sql, etc. I want to execute the files based on the last number after underscore sequentially(i.e.. _1,_2,etc) .

Can anybody help me?

Hi!
I think this should do the trick:

ls | sort -nr | xargs exec

substitute exec with the proper command you need to actually execute those *.sql files

Something like this:

$ ls -1 *.sql | perl -e 'print sort { $a=~/_(\d+)/; $c=$1; $b=~/_(\d+)/; $d=$1; $c <=> $d; } <>;'
ab_2.sql
abc_3.sql
ab_4.sql
a_5.sql

Update: What happens with Aleksejs' variant:

$ ls -1 *.sql| sort -nr
abc_3.sql
ab_4.sql
ab_2.sql
a_5.sql
$ ls -1 *.sql| sort -n
a_5.sql
ab_2.sql
ab_4.sql
abc_3.sql

Hi,

Try something like this:

for f in $(ls *.sql | sort -t_ -k2,2);do ./$f;done

Hmm... if I run

touch abc_3.sql ab_4.sql ab_2.sql a_5.sql acbd_1
ls | sort -nr

then I get:

acbd_1
abc_3.sql
ab_4.sql
ab_2.sql
a_5.sql

Pludi' variant seems more adaptable, of course.

The above script works perfectly as expected but the difference is it is in perl. anyways thanks also for the quick reply.