Hi ,
I actually have a script to bundle up two files but i don't know how it works .Can someone please explain.

Here's the script:-

for i
do
echo "echo $i 1>&2"
echo "cat >$i <<'End of $i'"
cat $i
echo "End of $i"
done

now suppose i pass two regular files as parameters :

$>chmod 777 bundle.sh
$>./bundle.sh file1 file2>new.sh
$>chmod 777 new.sh
$>./new.sh
file1
file2

The above is teh output as shown here we redirect the output to new.sh file and on executing that file we end up bundling the two files

Can somebody please explain what's going on in here?

The first thing: Use (CODE) button (which inserts tags) so we can refer to line numbers and see the code more pretty.

for i
do
  echo "echo $i 1>&2"
  echo "cat >$i <<'End of $i'"
  cat $i
  echo "End of $i"
done

So:

Line 1 and 2 (finished in line 7) loop through the command line args
line 3 echo what you are doing (incorrectly done if you want to put onto stderr)
line 4 another possible error, much line line 3
line 5 Do the work
line 6 put an ending line onto stdout
line 7 end of loop

Of course if you just want to put many files together (without the start and end lines), you can do it as one line

cat file1 file2 file3 > outputfile

> cat file1 file2 file3 > outputfile

I think you are missing the point. The script does not just cat files together. It creates another script, which serves as a primitive self-extracting archive. Let's see what happens when you invoke it as ./bundle.sh file1 file2>new.sh As you said, line 3 just logs a progress message on a stderr, and new.sh gets written with

cat >file1 << End of file1
... body of file1 ...
End of file1
cat >file2 << End of file2
... body of file2 ...
End of file2

The << construct is a so called "here document". When new.sh is running, the data between matching strings (in this case, "End of file1") gets sent to stdin of the command (in this case, cat). That is, new.sh will write the body of file1 into file1, and the body of file2 into file2.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.