tasklist.txt文件里面就只有三行
1
2
3
最新修改:
请问下面两段代码有什么不一样的:
代码一:
#以行读取
oldIFS=$IFS;
IFS=$'\n';
for line in $(cat tasklist.txt )
do
echo $line
nohup mpirun -np 1 ./bin/ASDMPI cff &
sleep 1
done
kill -9 $(pgrep ASDMPI)
以上代码输出
1
nohup: appending output to 'nohup.out'
2
nohup: appending output to 'nohup.out'
3
nohup: appending output to 'nohup.out'
代码二:
cat tasklist.txt | while read line
do
echo $line
nohup mpirun -np 1 ./bin/ASDMPI cff &
sleep 1
done
kill -9 $(pgrep ASDMPI)
以上代码输出
1
nohup: appending output to 'nohup.out'
测试环境为:Ubuntu18.04
@冰封,结果是一样的啊,不一样肯定是你的tasklist.txt发生了变化,要不然就是你的shell解释器不标准,试试执行的时候前面加bash。


https://stackoverflow.com/questions/56406771/how-to-sleep-in-shell
我在其他两个平台提问的,听他们感觉是我的ASDMPI吃掉了管道的内容,但是为何在;另一台centos中不会发发生这种情况?在我的Ubuntu则会出现
@冰封,由于在我这里没有复现该问题,所以我无法猜测。不过,我发现用 /bin/sh 运行你的第一个脚本结果不正确,可能是它不支持IFS。建议显式通过bash执行脚本来保证解析器一致。