dobroho_vechora/dobroho_vechora.bash
2024-12-16 22:46:50 +05:00

60 lines
820 B
Bash
Executable File

#!/bin/bash
set -e
ip="${1}"
port="${2}"
print_usage() {
echo "${1}"
echo "${0} <IP> <PORT>"
exit
}
[ -z "${ip}" ] && print_usage 'No IP given.'
[ -z "${port}" ] && print_usage 'No port given.'
[ -z "${3}" ] || print_usage 'Too many arguments.'
send_message() {
exec 3<> "/dev/tcp/${ip}/${port}"
echo "${1}" >&3
exec 3>&-
}
get_message() {
exec 3<> "/dev/tcp/${ip}/${port}"
echo "${1}" >&3
timeout 1s cat <&3 || true
exec 3>&-
}
while :; do
read cmd
case "${cmd}" in
i)
b=""
while :; do
read c
[[ "${c}" != "." ]] || break
if [ -z ${b} ]; then
b="${c}"
else
b="${b}"$'\n'"${c}"
fi
done
send_message 0"${b}"
;;
I)
read a
send_message 0"${a}"
;;
p)
send_message 1
get_message 2
;;
q)
exit
;;
*)
echo ?
;;
esac
done