阻止'cp'从bash脚本输出文件名

时间:2021-06-20 20:30:53

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

我有一个shell脚本,它将一些文件复制到当前目录,压缩它们,并将压缩文件流式传输到stdout。

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

在客户端,我使用plink来执行脚本并将stdin流式传输到文件。

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".

似乎cp命令输出从脚本内部执行时复制的文件名。如果我执行'cp / path / to / file1。'它在外壳中安静地做到了;如果我在脚本中执行它输出“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.

我该如何防止这种情况?我已经尝试将cp命令的输出传递给/ dev / null和一个虚拟文本文件,但没有运气。

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 个解决方案

#1


19  

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

它不是cp,它是焦油。你传递它-v,这使它打印文件的名称。

#2


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发出的文件名转到stderr,但情况并非总是如此:只有当你将tar文件写入stdout时,-v写的文件才会转到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. :-)

这没有产生文件名列表,因为它们被发送到/ dev / null。我猜你每天都学到新东西。 :-)

#3


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的-v(详细)选项正在将文件名踢到STDERR。您可以通过让tar将压缩文件流写入STDOUT来提高脚本效率:

tar zcf - file1 file2 file3

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

在此示例中,作为文件名传递的“ - ”选项使tar将输出写入STDOUT。

#1


19  

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

它不是cp,它是焦油。你传递它-v,这使它打印文件的名称。

#2


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发出的文件名转到stderr,但情况并非总是如此:只有当你将tar文件写入stdout时,-v写的文件才会转到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. :-)

这没有产生文件名列表,因为它们被发送到/ dev / null。我猜你每天都学到新东西。 :-)

#3


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的-v(详细)选项正在将文件名踢到STDERR。您可以通过让tar将压缩文件流写入STDOUT来提高脚本效率:

tar zcf - file1 file2 file3

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

在此示例中,作为文件名传递的“ - ”选项使tar将输出写入STDOUT。