[问题]从文件中一行一行读取数据作为参数, 来执行命令

sh/bash/dash/ksh/zsh等Shell脚本
回复
caike
帖子: 30
注册时间: 2005-12-09 16:22

[问题]从文件中一行一行读取数据作为参数, 来执行命令

#1

帖子 caike » 2008-02-23 14:50

搞明白了, 总结一下:

(dat.txt是数据文件, 形式如下:)

代码: 全选

1
8
3
5
9
1
1. 一行一行读取文件:

代码: 全选

while read line
do
echo $line
done < dat.txt
2. 把命令一行行写在dat.txt里, 然后逐行执行:

代码: 全选

echo 1 
mkdir a
diff a/1.txt a/2.txt

代码: 全选

FILE=cat dat.txt

echo "$FILE" | \     # notice we PIPE the output data to the next line (denoted by \)
while read CMD; do   # CMD is the line, this could be any variable name
   $CMD             # just execute it
done
整理了一下, 方便后来人 :D
上次由 caike 在 2008-02-23 17:09,总共编辑 1 次。
java程序员 linux菜鸟 C++初学者
http://ke-cai.net
头像
windwiny
帖子: 2254
注册时间: 2007-03-13 17:26

#2

帖子 windwiny » 2008-02-23 16:21

....你想说什么。。


在循环里 写命令啊,加 $ 变量
头像
eexpress
帖子: 58428
注册时间: 2005-08-14 21:55
来自: 长沙

#3

帖子 eexpress » 2008-02-23 16:34

cat xxx | while read i; do xxxx; done

估计楼主还没明白自己需要怎么的操作。先说说,自己要什么操作。
● 鸣学
头像
BigSnake.NET
帖子: 12522
注册时间: 2006-07-02 11:16
来自: 廣州
联系:

#4

帖子 BigSnake.NET » 2008-02-23 16:52

FILE=`cat filename`

echo "$FILE" | \ # notice we PIPE the output data to the next line (denoted by \)
while read i; do
./a.sh $i
done
^_^ ~~~
要理解递归,首先要理解递归。

地球人都知道,理论上,理论跟实际是没有差别的,但实际上,理论跟实际的差别是相当大滴。
caike
帖子: 30
注册时间: 2005-12-09 16:22

#5

帖子 caike » 2008-02-23 16:55

多谢朋友们的帮忙, 解决方法整理在上面了
是这样的, 这个dat文件是一个程序生成的, 只是一些前期处理的结果, 我想在另一脚本里读取这些结果, 再进行下一步处理.

我找到的这种方法只能顺序执行dat文件里的命令, 我还要再用sed把每行再加上命令名.
我也是希望在while循环里只读出数据, 而不是执行里面的一条条命令.

代码: 全选

echo "$FILE" | \     # notice we PIPE the output data to the next line (denoted by \)
while read CMD; do   # CMD is the line, this could be any variable name
   echo $CMD             # just execute it
done
上面的写法是不行的, 如果dat文件里只是1,3,4的数字, 会报找不到命令的错误.

希望能说明白了:)
java程序员 linux菜鸟 C++初学者
http://ke-cai.net
回复