2

I have file js.txt which contains paths to javascript files. I want to output all javascripts into one file.

js.txt content:

js/jquery/jquery-1.6.2.min.js

js/jquery/jquery-ui-1.8.6.custom.min.js

My bash script:

#!/bin/bash
WEBROOT=/home/rexxar/web/webroot/

FILE=$WEBROOT"js.txt"
cat "js.txt" | while read LINE; do
    cat $WEBROOT$LINE >> js_all.js
done

Output in terminal is error message: "Directory or file doesn't exist" followed by file path fragment for each line.

: Directory or file doesn't exist/jquery/jquery-1.6.2.min.js

: Directory or file doesn't exist/jquery/jquery-ui-1.8.6.custom.min.js

I am sure that all paths are right and files does exist.

3
  • The error message and your information don't match. You say you have js/jquery/*.js, the error message says it's not finding /jquery/*.js (notice the absence of js/). Please recheck and make sure you post both the exact script and error messages. Commented Oct 2, 2011 at 10:51
  • I mentioned it's error message followed by path fragment. Commented Oct 2, 2011 at 11:30
  • I can see that, and the error message shows paths that don't exist according to what you wrote above, so something is not consistent in your question. Commented Oct 2, 2011 at 11:34

4 Answers 4

2

Firstly, some advices.

1) Check absolute paths of files js/jquery/jquery-1.6.2.min.js and js/jquery/jquery-ui-1.8.6.custom.min.js. Use readlink -f and dirname.

2) Check absolute path of directory your script is running from.

3) Think about variable $FILE . Maybe it's a good idea to use cat ${FILE} instead of cat "js.txt"

4) Empty lines in js.txt is also make some kind of problems to you.

5) And why are you using CAPS_VARIABLE_NAMES?

Secondly, the solution.

I'm trying to understand your problem, so I've create all files you've got there:

$> cat js/jquery/jquery-1.6.2.min.js 
test1
$> cat js/jquery/jquery-ui-1.8.6.custom.min.js 
test2
$> cat js.txt 
js/jquery/jquery-1.6.2.min.js
js/jquery/jquery-ui-1.8.6.custom.min.js

So, like Arnout Engelen said (but I cannot understand why he use > instead of >>)

$> cat ./js.txt | xargs cat >> ./js_all
$> cat ./js_all
test1
test2
Sign up to request clarification or add additional context in comments.

1 Comment

I already know why your solutions don't work. This file was probably created on windows system, so I replaced cat "js.txt"....with cat "js.txt" | dos2unix....... and it magically works :)
1

How about:

cd $WEBROOT; cat js.txt | xargs cat > js_all.js

1 Comment

No, result is a little bit different : Directory or file doesn't existjs : Directory or file doesn't existustom.min.js
1

It looks to me like your js.txt file has DOS line endings (carriage return+linefeed) instead of unix (just linefeed), and the script is treating the CR as part of the filename. Either convert the file with something like dos2unix, or make the script convert it on the fly:

...
tr -d "\r" <"js.txt" | while read LINE; do
....

Comments

1

relative paths are saved in your js.txt. you have to make sure that the path is valid from the directory where you execute the script. unless in your script you first run 'cd' command to the right directory.

if the directory thing is fixed, awk oneliner can do what you need.

awk '{if($0) system("cat "$0" >> js_all.js")}' js.txt

Comments

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.