Permalink
Browse files

More examples of hard-coded file descriptors

  • Loading branch information...
Andy Chu
Andy Chu committed Aug 14, 2017
1 parent 118089e commit 0af9b23ee9aafdf0a7dbe187049726f0a6648af1
Showing with 42 additions and 1 deletion.
  1. +42 −1 spec/hard-coded-descriptors.sh
@@ -9,6 +9,14 @@ set -o nounset
set -o pipefail
set -o errexit
# https://lobste.rs/s/bqftd6/avoid_directly_manipulating_file
#
# - "One thing that I have found non-{0,1,2} FDs useful for however is when
# tools (e.g. gpg) take a --passphrase-fd argument, useful for when you are
# also redirecting some other thing into stdin."
# - "Some protocols (like djb’s checkpassword or doit) rely on specific file
# descriptors"
# https://www.reddit.com/r/bash/comments/6td8j2/avoid_directly_manipulating_file_descriptors_in/
interactive-stdin() {
@@ -42,7 +50,6 @@ tee-like() {
cat $log
}
# Hm this one isn't working yet
pipe-stderr() {
local log=_tmp/pipe-stderr.log
set +o errexit
@@ -76,4 +83,38 @@ zip-demo() {
_zip _tmp/{left,right}.txt
}
_work() {
local id=$1
echo "Job $id"
for i in 1 2 3; do
echo "... $i ..."
sleep 0.2
done
}
# man flock
_flock() {
(
flock -n 9 || {
echo "Another job is already running; aborting"
exit 1
}
"$@"
) 9>_tmp/mylockfile
}
flock-demo() {
_work A
_work B
# Instead of running
$0 _flock _work C &
$0 _flock _work D & # One is already running
wait
wait
}
"$@"

0 comments on commit 0af9b23

Please sign in to comment.