Skip to content

Instantly share code, notes, and snippets.

@x4x
Last active June 21, 2018 09:18
Show Gist options
  • Select an option

  • Save x4x/143d69370bd8084971e1c341e0673474 to your computer and use it in GitHub Desktop.

Select an option

Save x4x/143d69370bd8084971e1c341e0673474 to your computer and use it in GitHub Desktop.
a job queue in bash
#!/bin/bash
# Author: x4x
# Create a job queue for bash.
set -o errexit
set -o nounset
FIFO=/tmp/shfifo0
n=0
MARK='\033[0;35m'
NC='\033[0m' # No Color
trap "rm -f $FIFO" EXIT
if [[ ! -p $FIFO ]]; then
mkfifo $FIFO
fi
# read whatever from the named pipe.
while read job < $FIFO
do
n=$((n+1))
if [[ $job = 'quit' ]]; then
echo -e "${MARK}Done processing $n jobs${NC}"
exit
fi
echo -e "${MARK}running job $n: ${NC} $job"
{
(eval '$job') | sed -e 's/^/'"$n> /" &
# catch errors
} || {
echo -e "${MARK}An Error happend${NC}"
}
done
# use:
# run in other shell a piped comand to the FIFO
# echo "ytd " > /tmp/shfifo0
# #alias ytd='youtube-dl -x --audio-format mp3 '
# function ytmq { echo "youtube-dl -x --audio-format mp3 $1" > /tmp/shfifo0; }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment