Skip to content

Instantly share code, notes, and snippets.

@drewchapin
Last active January 3, 2024 11:13
Show Gist options
  • Save drewchapin/f43ab1a10367b1bfa53c55fe612d4245 to your computer and use it in GitHub Desktop.
Save drewchapin/f43ab1a10367b1bfa53c55fe612d4245 to your computer and use it in GitHub Desktop.
Example of how to use the COPROC feature of Bash by showing a progress bar with zenity and updating it.
#!/bin/bash
on_exit() {
echo "#The script has quit unexpectidly on line $1" >&$z
echo "The script has quit unexpectidly on line $1" >&2
exit
}
on_error() {
echo "Error on line $1"
exit
}
coproc zenity --display=:0 --progress --title 'COPROC Example' --text 'Running...'
z=${COPROC[1]}
# Trap when an error occurs, such as '$z: Bad file descriptor' when the user closes the progress dialog
trap 'on_exit $LINENO' EXIT
trap 'on_error $LINENO' ERR
for i in $(seq 1 100); do
echo $i >&$z
echo "#Running... $i%" >&$z # The text will not change if you do not include the hash sign.
sleep 0.1
if [ $i == 50 ]; then
exit 42
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment