while read 中遇到的问题

2021年7月9日 0 条评论 596 次阅读 0 人点赞

exit的差异:

方法一:

#!/bin/bash
echo -e "1\n2\n3" > /tmp/testfile
function abc(){
    cat /tmp/testfile | while read line;do
        echo $line
        exit
    done
}
echo Starttttttttt
abc
echo Enddddddddddd

方法二:

#!/bin/bash
echo -e "1\n2\n3" > /tmp/testfile
function abc(){
    while read line;do
        echo $line
        exit
    done<<<"$(cat /tmp/testfile)"
}
echo Starttttttttt
abc
echo Enddddddddddd

方法一结果:

Starttttttttt
1
Enddddddddddd

方法二结果:

Starttttttttt
1
可以看出方法一中的exit无法退出脚本

Sevenfal

这个人太懒什么东西都没留下

文章评论(0)