2003-11-03 16:26:45 +00:00
|
|
|
#!/bin/sh
|
|
|
|
# readpipe.sh
|
|
|
|
# This example contributed by Bjon Eriksson.
|
|
|
|
|
2011-05-03 15:38:48 +00:00
|
|
|
### shopt -s lastpipe
|
|
|
|
|
2003-11-03 16:26:45 +00:00
|
|
|
last="(null)"
|
|
|
|
cat $0 |
|
|
|
|
while read line
|
|
|
|
do
|
|
|
|
echo "{$line}"
|
|
|
|
last=$line
|
|
|
|
done
|
2009-09-29 15:08:03 +00:00
|
|
|
|
|
|
|
echo
|
|
|
|
echo "++++++++++++++++++++++"
|
2011-05-03 15:38:48 +00:00
|
|
|
printf "\nAll done, last: $last\n" # The output of this line
|
|
|
|
#+ changes if you uncomment line 5.
|
|
|
|
# (Bash, version -ge 4.2 required.)
|
2003-11-03 16:26:45 +00:00
|
|
|
|
|
|
|
exit 0 # End of code.
|
|
|
|
# (Partial) output of script follows.
|
|
|
|
# The 'echo' supplies extra brackets.
|
|
|
|
|
|
|
|
#############################################
|
|
|
|
|
|
|
|
./readpipe.sh
|
|
|
|
|
|
|
|
{#!/bin/sh}
|
|
|
|
{last="(null)"}
|
|
|
|
{cat $0 |}
|
|
|
|
{while read line}
|
|
|
|
{do}
|
|
|
|
{echo "{$line}"}
|
|
|
|
{last=$line}
|
|
|
|
{done}
|
2009-09-29 15:08:03 +00:00
|
|
|
{printf "nAll done, last: $lastn"}
|
2003-11-03 16:26:45 +00:00
|
|
|
|
|
|
|
|
2009-09-29 15:08:03 +00:00
|
|
|
All done, last: (null)
|
2003-11-03 16:26:45 +00:00
|
|
|
|
2009-09-29 15:08:03 +00:00
|
|
|
The variable (last) is set within the loop/subshell
|
|
|
|
but its value does not persist outside the loop.
|