[英]echo to file from nested script bash
我是bash的新手。 我有兩個腳本-遍歷目錄的script1,並且針對其中的每個文件在文件上執行script2。
腳本1:
#!/bin/bash
for file in $(find ../myFiles/ -type f); do
$( cat $file | ./script2) >> res.txt
done
腳本2:
while read line;
do ...
....
echo "$line"
done
但是,script2中的echo "$line"
不能按我想要的方式工作(到res.txt文件),但是它作為命令輸出,導致錯誤(“找不到命令”)
有人知道該怎么做嗎? 謝謝。
$( 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
如果使用函數來完成任務,則該任務會更容易:
#!/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
如果以后需要額外的功能,它更易於閱讀和修改。 一個腳本就足夠了。
(很酷的一點是,腳本將自行打印。)
$(...)
用於命令替換。 考慮以下腳本:
腳本1:
#!/bin/bash
while read file; do
./script2 "$file" >> res.txt
done < <(find . -name "file" -type f)
腳本2:
#!/bin/bash
while read line; do
echo "$line"
done < "$1"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.