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

问题描述:

我正在解除.tar.gz文件并将输出重定向到变量。但是这样做会给我一个错误。将tar命令的输出重定向到shell脚本中的变量

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

错误:

old_file: command not found 

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

无法弄清楚,如果tar文件重定向工作或没有?

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

你可以试试这个;

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

例如;

tar -tvzf $ node |头-2 |尾巴-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