1

I have a shell script which copies a few files to the current directory, compresses them, and streams the compressed file to stdout.

On the client side I use plink to execute the script and stream stdin to a file.

This almost works.

It seems that the cp command outputs the file name being copied when its executed from inside the script. If I execute 'cp /path/to/file1 .' in the shell it does it quietly; if I execute it in a script it outputs "file1".

How do I prevent this? I've tried piping the output of the cp command to /dev/null and to a dummy text file but with no luck.

thanks for any help.

the script

#!/bin/bash cp /path/to/file1 . cp /path/to/file2 . cp /path/to/file3 . tar -cvzf package.tgz file1 file2 file3 cat package.tgz 

the output

file1 file2 file3 <<binary data>> 
3
  • If you want the tar output, why not removing the -f package.tgz and letting tar print it directly to stdout? Commented Nov 7, 2008 at 1:15
  • What he said. (remove the -f and -v options) Commented Nov 7, 2008 at 1:20
  • @Ken G: and remove the cat package.tgz at the end, of course. Commented Nov 7, 2008 at 1:28

3 Answers 3

19

It's not cp, it's tar. You are passing it -v, which makes it print the names of the files.

Sign up to request clarification or add additional context in comments.

Comments

2

Aha! I'd always assumed that the file names emitted by tar go to stderr, but that isn't always the case: only if you write your tar file to stdout do the files written by -v go to stderr:

$ tar cvf - share > /dev/null share/ # this must be going share/.DS_Store # to stderr since we share/man/ # redirected stdout to share/man/.DS_Store # /dev/null above. share/man/man1/ share/man/man1/diffmerge.man1 

The counter-example:

$ tar cvf blah.tar share > /dev/null 

This produced no list of file names because they got sent to /dev/null. I guess you learn something new every day. :-)

Comments

0

As others pointed out, the -v (verbose) option to tar is kicking out the file names to STDERR. You can also make your script more efficient by having tar write the compressed file stream to STDOUT:

tar zcf - file1 file2 file3 

In this example, the "-" option passed as the filename makes tar write the output to STDOUT.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.