shell - sh: How do I avoid clobbering numbered file descriptors? -


when have

  exec 3>>file               # file descriptor 3 points file   [ $dryrun ] && exec 3>&1   # or possibly stdout   echo "running">&3   exec 3>&-                  # , closed 

i'm worried file descriptor 3 may have pointed outside of function in question. how can handle this?

  • is there builtin next_available_fd?
  • is there way duplicate fd3 variable, dup once function done?
    • and should worry threading , concurrent writes fd3 in case?
  • i'm in sh, maybe bash/ksh/zsh has answer this?

instead of using exec redirect file descriptor within function, can (with bash, haven't tried other shells) do:

 foo() {   test $dryrun && exec 3>&1   echo running >&3 } 3>>file  foo more_commands 

in setup, "running" go either file or original stdout depending on $dryrun, , more_commands have fd 3 before foo called.


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -