Last active
June 21, 2018 09:18
-
-
Save x4x/143d69370bd8084971e1c341e0673474 to your computer and use it in GitHub Desktop.
a job queue in bash
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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