Skip to content

Instantly share code, notes, and snippets.

@sayanarijit
Last active December 23, 2018 18:30
Show Gist options
  • Save sayanarijit/a0d37b9c5d64df7e219611e43737181d to your computer and use it in GitHub Desktop.
Save sayanarijit/a0d37b9c5d64df7e219611e43737181d to your computer and use it in GitHub Desktop.
Going Crazy With UNIX Pipes
#!/bin/bash
send() {
[ "$(cat /tmp/pipelock)" == "1" ] && return 0
echo 1 > /tmp/pipelock
read -p "me ['q' to quit] : " msg
[ $msg == q ] && echo 0 > /tmp/pipelock && echo q > ~/mypipe && return 1
echo $msg > ~/mypipe
echo 0 > /tmp/pipelock
return 0
}
receive() {
[ "$(cat /tmp/pipelock)" == "0" ] && return 0
msg=$(cat ~/mypipe)
[ "$msg" == "q" ] && return 1
echo "other person : $msg"
return 0
}
main() {
[ -f /tmp/pipelock ] || echo 0 > /tmp/pipelock
[ -p ~/mypipe ] || mknod ~/mypipe p
echo "* Starting chat..."
while /bin/true; do
receive || break
send || break
done
echo "* Chat ended...!"
}
main
#!/bin/bash
send() {
read -p "Me : " msg
[ "$msg" == "" ] && echo "You ended the chat...!" && echo > ~/mypipe && return 1
echo $msg > ~/mypipe
}
receive() {
msg=$(cat ~/mypipe)
[ "$msg" == "" ] && echo "Kurisu ended the chat...!" && return 1
echo "Kurisu: $msg"
}
main() {
while /bin/true; do
send || break
receive || break
done
}
main
#!/bin/bash
send() {
read -p "Me : " msg
[ "$msg" == "" ] && echo "You ended the chat...!" && echo > ~/mypipe && return 1
echo $msg > ~/mypipe
}
receive() {
msg=$(cat ~/mypipe)
[ "$msg" == "" ] && echo "Okabe ended the chat...!" && return 1
echo "Okabe: $msg"
}
main() {
while /bin/true; do
receive || break
send || break
done
}
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment