fork download
  1. exec 2>&1
  2. function cdate() {
  3. ret="${1:-0}"
  4. date
  5. ((ret==0)) || echo "** failed **" >&2
  6. return $ret
  7. }
  8. function usepipe() {
  9. cdate "$@" | tr A-Z a-z
  10. }
  11. function usetfile1() {
  12. local tfile=$(mktemp)
  13. echo "** temporary file $tfile created **" >&2
  14. cdate "$@" >"$tfile" && tr A-Z a-z <"$tfile"
  15. rm "$tfile"
  16. }
  17. function usetfile2() {
  18. local tfile=$(mktemp) tfd_i tfd_o
  19. echo "** temporary file $tfile created **" >&2
  20. exec {tfd_o}>"$tfile" {tfd_i}<"$tfile"
  21. rm "$tfile"
  22. cdate "$@" >&$tfd_o && tr A-Z a-z <&$tfd_i
  23. exec {tfd_o}>&- {tfd_i}<&-
  24. }
  25. function docase() {
  26. echo "* begin: \"$@\" *" >&2
  27. "$@"
  28. echo "* end *" >&2
  29. echo >&2
  30. }
  31. #set -x
  32. docase usepipe 0
  33. docase usepipe 1
  34. docase usetfile1 0
  35. docase usetfile1 1
  36. docase usetfile2 0
  37. docase usetfile2 1
  38.  
  39. ls -l . $TMPDIR
Success #stdin #stdout 0.03s 5296KB
stdin
Standard input is empty
stdout
* begin: "usepipe 0" *
thu 06 jun 2024 11:24:59 am utc
* end *

* begin: "usepipe 1" *
** failed **
thu 06 jun 2024 11:24:59 am utc
* end *

* begin: "usetfile1 0" *
** temporary file /tmp/wWj7GL/tmp.bNetj1OZXi created **
thu 06 jun 2024 11:24:59 am utc
* end *

* begin: "usetfile1 1" *
** temporary file /tmp/wWj7GL/tmp.9ZNniJg0id created **
** failed **
* end *

* begin: "usetfile2 0" *
** temporary file /tmp/wWj7GL/tmp.Tb2Fxm7AvK created **
thu 06 jun 2024 11:24:59 am utc
* end *

* begin: "usetfile2 1" *
** temporary file /tmp/wWj7GL/tmp.6wOAjyCkCg created **
** failed **
* end *

.:
total 4
-rw-rw-r-- 1 root root 762 Jun  6 11:24 prog.sh

/tmp/wWj7GL:
total 0