123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- #!/bin/bash
-
- ################################################################################
- #
- # This file is part of the package personal-webhooks. It is subject to
- # the license terms in the LICENSE file found in the top-level directory
- # of this distribution and at:
- #
- # git://git.devalot.com/personal-webhooks.git
- #
- # No part of this package, including this file, may be copied, modified,
- # propagated, or distributed except according to the terms contained in
- # the LICENSE file.
- #
- ################################################################################
- #
- # Execute a command for each line read from a FIFO.
- #
- # For example, to use in conjunction with the download-video.sh script:
- #
- # watchfifo.sh -f /tmp/download.fifo -- download-video.sh -d ~/Downloads
- #
- # Lines read from the FIFO are piped into the given command's stdin.
- #
- ################################################################################
- set -e
- option_fifo_file=""
- option_force=0
- option_group=""
-
- ################################################################################
- usage () {
- cat <<EOF
- Usage: watchfifo.sh [options] -- command [arg1, arg2, ...]
-
- -f FILE The FIFO file to create and manage
- -F Force. If FIFO file exists, remove it
- -g GRP Set the FIFO file's group to GRP
- -h This message
-
- EOF
- }
-
- ################################################################################
- while getopts "f:Fg:h" o; do
- case "${o}" in
- f) option_fifo_file=$OPTARG
- ;;
-
- F) option_force=1
- ;;
-
- g) option_group=$OPTARG
- ;;
-
- h) usage
- exit
- ;;
-
- *) exit 1
- ;;
- esac
- done
-
- shift $((OPTIND-1))
-
- ################################################################################
- die() {
- >&2 echo "ERROR: " "$@"
- exit 1
- }
-
- ################################################################################
- prepare() {
- mkfifo -m 0620 "$option_fifo_file"
-
- if [ -n "$option_group" ]; then
- chgrp "$option_group" "$option_fifo_file"
- fi
- }
-
- ################################################################################
- cleanup() {
- rm -f "$option_fifo_file"
- }
-
- ################################################################################
- subcommand() {
- json=$1
- shift
-
- if ! echo "$json" | "$@"; then
- >&2 echo "ERROR: child process failed"
- fi
- }
-
- ################################################################################
- # Safety checks.
- if [ -z "$option_fifo_file" ]; then
- die "you must use -f to specify a file path"
- fi
-
- if [ -r "$option_fifo_file" ]; then
- if [ "$option_force" -eq 1 ]; then
- rm "$option_fifo_file"
- else
- die "FIFO file exists, remove it first: $option_fifo_file"
- fi
- fi
-
- if [ $# -le 0 ]; then
- die "please provide a command to run after -- "
- fi
-
- ################################################################################
- export IFS=$'\n'
- trap cleanup EXIT
- prepare
-
- while :; do
- read -r json < "$option_fifo_file"
- subcommand "$json" "$@" &
- done
|