0

In bash without using Ant, Grunt or similar, I want to concatenate some files.

I don't think is should be harder than a few lines of bash.

I don't want to use a build tool as they do in this SO Post.

I want to use bash similar to this SO Post.

It would be something like:

cat *.js > all.txt

However, I want to only cat .js files that have this form

object.SomeName.js

As a side question, do most people break out their .js into objects?

What about:

cat object.*.js >> all.txt

Also there are dependencies between the objects, so ordering matters.

What ordering does cat work in?

6
  • sounds like you just need to readup on wildcard file matching in bash... Commented Oct 4, 2015 at 21:27
  • I posted what I think is the answer, but b.c. there are depeddencies, I need to know the order that cat works in or perhaps there is a batter bash tool? Commented Oct 4, 2015 at 21:28
  • This answer: serverfault.com/a/122743 says bash wildcard expansion is alphabetical. Commented Oct 4, 2015 at 21:30
  • you can add a prefix to depends to ensure that alpha order is the correct order. Commented Oct 4, 2015 at 21:31
  • well I have dependencies, so I don't think I should do this. I would have to come up with a naming convention to assure the order. For example if obj2 needs obj0 and obj1, I would need to make sure they were loaded first. Commented Oct 4, 2015 at 21:32

1 Answer 1

2

First, create a file that describes your dependencies.

obj0 obj2
obj1 obj2

Now tsort(1) can give you an ordering.

$ tsort dep.txt
obj0
obj1
obj2

Now you can read each line in turn and output the files in the correct order.

{
  while read obj
  do
    cat "object.$obj.js"
  done < <(tsort dep.txt)
} > all.js
Sign up to request clarification or add additional context in comments.

2 Comments

Here is a nice explanation of tort - viget.com/extend/dependency-sorting-in-ruby-with-tsort - It uses a dependency hash. Can i write dep. txt in JSON ? Or is it just space separated like in your example.
@cadegalt: As the man page explains, the input file is whitespace-delimited.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.