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/webr开发者_如何学编程oot/
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.
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
How about:
cd $WEBROOT; cat js.txt | xargs cat > js_all.js
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
....
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
精彩评论