#!/bin/bash
function make_exit() {
if [ "$1" = "exit" ]; then
echo 'exit 1'
exit 1 # I want the whole script to exit
fi
}
echo -e "line1\nline2" | while read -r line; do
echo "start while,${line}"
make_exit "exit"
echo "end while,${line}"
done
# Why is it implemented here
echo 'end script'
The result of executing the above script,But I want to exit the whole script at exit 1 of make_exit.
./test.sh
start while,line1
exit 1
end script
But the following writing works correctly.
#!/bin/bash
function make_exit() {
if [ "$1" = "exit" ]; then
echo 'exit 1'
exit 1 # I want the whole script to exit
fi
}
echo -e "line1\nline2" > input
while read -r line; do
echo "start while,${line}"
make_exit "exit"
echo "end while,${line}"
done <input
# Why is it implemented here
echo 'end script'
./test.sh
start while,line1
exit 1
I probably guessed that the problem was caused by the pipeline, but the specific reason is not clear