Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
错误地将命令重定向到shell变量?_Shell - Fatal编程技术网

错误地将命令重定向到shell变量?

错误地将命令重定向到shell变量?,shell,Shell,我想从XML stdin中获取目录名,然后将内容复制到targetdirectory: count=$(echo $xmlstring | xmlstarlet sel -t -v "count(//directory)" -) && for ((i=1; i<=count; i++)); do {currentpath=$(echo $xmlstring| xmlstarlet sel -t -v "//directory[$i]/text()" -) &&am

我想从XML stdin中获取目录名,然后将内容复制到targetdirectory:

 count=$(echo $xmlstring | xmlstarlet sel -t -v "count(//directory)" -) && for ((i=1; i<=count; i++)); do {currentpath=$(echo $xmlstring| xmlstarlet sel -t -v "//directory[$i]/text()" -) && cp currentpath /opt/targetdir} ; done    
基本上,我正在尝试将xml Stdin中的目录名作为CP命令的源目录……这样,最终,xml中指定的目录中的所有文件都会复制到targetdir

我得到一个错误:

-bash: {currentpath=/opt/in1: No such file or directory
{currentpath=: command not found        
从xml stdin中提取目录并写入txt文件是有效的,

但是当我想从currentdirectory复制到某个targetdir时,它不起作用。

有很多错误,请参阅此更正版本:

count=$(echo "$xmlstring" | xmlstarlet sel -t -v "count(//directory)" -)
for ((i=1; i<=count; i++)); do
    currentpath="$(
        echo "$xmlstring" |
            xmlstarlet sel -t -v "//directory[$i]/text()" - |
            tr -d '$\n'
    )"
    cp "$currentpath" "/opt/targetdir/"
done    
count=$(echo“$xmlstring”| xmlstarlet sel-t-v“count(//目录)”-)

对于((i=1;ii trued$,它也不起作用….我需要使用oneliner,因为我正在使用的系统我猜您有剪切和粘贴错误,因为
$currentpath=
不是在
var=$(…)的rhs上分配变量和省略
$
的好方法
也很糟糕。或者这些是你面临的根本问题吗?对不起,这里有打字错误,,,,我编辑了原始帖子。你的错误之一是因为缺少空格。bash很挑剔,你不能在
{
后面没有空格来写
{cmd
{
是一个模棱两可的语法功能。您需要在它们周围加上空格和分号才能创建列表。
{echo a;echo b;echo c;}
vs.
(echo a;echo b;echo c)
。谢谢,xpath查询本身有问题,,,,非常感谢!!这里可以将SCP用于远程服务器吗?
count=$(echo "$xmlstring" | xmlstarlet sel -t -v "count(//directory)" -)
for ((i=1; i<=count; i++)); do
    currentpath="$(
        echo "$xmlstring" |
            xmlstarlet sel -t -v "//directory[$i]/text()" - |
            tr -d '$\n'
    )"
    cp "$currentpath" "/opt/targetdir/"
done