mirror of https://github.com/tLDP/LDP
66 lines
1.2 KiB
Bash
66 lines
1.2 KiB
Bash
#!/bin/bash
|
|
# wr-ps.bash: while-read loop with process substitution.
|
|
|
|
# This example contributed by Tomas Pospisek.
|
|
# (Heavily edited by the ABS Guide author.)
|
|
|
|
echo
|
|
|
|
echo "random input" | while read i
|
|
do
|
|
global=3D": Not available outside the loop."
|
|
# ... because it runs in a subshell.
|
|
done
|
|
|
|
echo "\$global (from outside the subprocess) = $global"
|
|
# $global (from outside the subprocess) =
|
|
|
|
echo; echo "--"; echo
|
|
|
|
while read i
|
|
do
|
|
echo $i
|
|
global=3D": Available outside the loop."
|
|
# ... because it does NOT run in a subshell.
|
|
done < <( echo "random input" )
|
|
# ^ ^
|
|
|
|
echo "\$global (using process substitution) = $global"
|
|
# Random input
|
|
# $global (using process substitution) = 3D: Available outside the loop.
|
|
|
|
|
|
echo; echo "##########"; echo
|
|
|
|
|
|
|
|
# And likewise . . .
|
|
|
|
declare -a inloop
|
|
index=0
|
|
cat $0 | while read line
|
|
do
|
|
inloop[$index]="$line"
|
|
((index++))
|
|
# It runs in a subshell, so ...
|
|
done
|
|
echo "OUTPUT = "
|
|
echo ${inloop[*]} # ... nothing echoes.
|
|
|
|
|
|
echo; echo "--"; echo
|
|
|
|
|
|
declare -a outloop
|
|
index=0
|
|
while read line
|
|
do
|
|
outloop[$index]="$line"
|
|
((index++))
|
|
# It does NOT run in a subshell, so ...
|
|
done < <( cat $0 )
|
|
echo "OUTPUT = "
|
|
echo ${outloop[*]} # ... the entire script echoes.
|
|
|
|
exit $?
|