2013-12-08 116 views
0

我是bash中的新手。我有两个脚本 - script1覆盖一个目录,其中的每个文件都在该文件上执行script2。从嵌套脚本bash回显文件

SCRIPT1:

#!/bin/bash 
for file in $(find ../myFiles/ -type f); do 
    $(cat $file | ./script2) >> res.txt 
done 

SCRIPT2:

while read line; 
do ... 
    .... 
    echo "$line" 
done 

然而在SCRIPT2的echo "$line"不工作,我想它(到res.txt文件),但它outputed作为命令,导致错误(“找不到命令”)

有人知道如何做到这一点吗? 谢谢。

+0

尝试 - '回声$ line'没有双引号。同样在script1中,'$(cat ..')行是否工作?您需要像'\'cat $ file | ./script2 \'>> res.txt' – Hussain

回答

1

$(foo)执行命令foo的结果,完全如您所述。做:

./script2 < "$file" >> res.txt 

有没有必要创建一个管道和运行的东西是什么bash做什么。 “echo”是外部命令。所以是“[”(如:if [ thing ]),但实际上bash在内部处理这些内容。尽管如此,您仍然可以将[作为独立程序运行。类型:which [来看看。

编辑:的情况下,这是不够清楚:

#!/bin/bash 
for file in $(find ../myFiles/ -type f); do 
    ./script2 < "$file" >> res.txt 
done 
0

任务是更容易,如果你用的功能来解决:

#!/bin/bash                  

search_path='.'                 

### declare functions ###               
function foo {                 
    for file in `find $search_path -type f`           
    do                    
    echo 'processing: '$file              
    process_file $file               
    done                   
}                     

function process_file {               
    while read line                 
    do                    
    echo $line                 
    done < $file                 
}                     

### call your main function ###             
foo 

它更易于阅读,并修改以防您稍后需要额外的功能。 一个脚本也足够了。

(A凉爽的是,因为它是,该脚本将打印本身。)

0

$(...)用于命令替换。考虑以下脚本:

SCRIPT1:

#!/bin/bash 
while read file; do 
    ./script2 "$file" >> res.txt 
done < <(find . -name "file" -type f) 

SCRIPT2:

#!/bin/bash 
while read line; do 
    echo "$line" 
done < "$1"