将 tar 命令的输出重定向到 shell 脚本中的变量

Redirect output of tar command to a variable in shell script

我正在取消taring .tar.gz 文件并将输出重定向到一个变量。但是在执行此操作时它给了我一个错误。

old_file =$(tar -tvzf $node | head -2 | tail -1 | cut -f4 -d "/")

错误:

 old_file: command not found

但是如果 运行 在终端中执行此命令,我会得到一些输出。

无法确定 tar 文件重定向是否有效?

应该是old_file=$(some cmd)。变量和'='之间没有space。

你能试试这个吗;

old_file =$(tar -tvzf $node | head -2 | tail -1 | cut -f3 -d "/")

例如;

tar -tvzf $node |头-2 | tail -1 输出是这样的;

-rw-rw-r-- user/user     2 2016-11-07 12:10 yourfile/php.png

如果分隔符为“/”,则剪切字段如下;

字段 1 是 -rw-rw-r-- user

字段 2 是 user 2 2016-11-07 12:10 yourfile

字段 3 是 php.png