Skip to content

Instantly share code, notes, and snippets.

@laser
Created July 14, 2018 16:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save laser/8239df140178bdc7891754f2629e59d6 to your computer and use it in GitHub Desktop.
Save laser/8239df140178bdc7891754f2629e59d6 to your computer and use it in GitHub Desktop.
fork/join shell
echo ""
echo "$(date "+%T") 'fork' a process..."
# opens the file as input fd 666 before the background job (curl) is started
exec 666< <(curl -s 'http://www.fakeresponse.com/api/?sleep=4')
echo ""
echo "$(date "+%T") 'fork' second process..."
# opens the file as input fd 666 before the background job (curl) is started
exec 777< <(curl -s 'https://cdn.bringatrailer.com/wp-content/uploads/2017/09/59c4506ce4ca8_P6155050-e1506103879707.jpg')
echo ""
echo "$(date "+%T") do something else in the meantime (like generate a 32 char random string)..."
RANDOM_STR_A=$(date +%s | md5 | base64 | head -c 32 ; echo)
echo ""
echo "$(date "+%T") blocks until image is downloaded..."
gzip <&777 > /tmp/datsun.jpg.gz
echo ""
echo "$(date "+%T") block until the slow web service responds..."
# 3.6 Redirections
# 3.6.8 Duplicating File Descriptors
# - the < redirection refers to standard input
# - cat does not return until fd 666 is closed
# - fd 666 is closed when curl completes
WEB_SVC_RESP=$(cat <&666)
echo ""
echo "$(date "+%T") yadda yadda..."
echo "RANDOM_STR_A=${RANDOM_STR_A}"
echo "WEB_SVC_RESP=${WEB_SVC_RESP}"
echo ""
echo "$(date "+%T") done!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment