patternbashMinor
SSH chatting tool
Viewed 0 times
chattingsshtool
Problem
I have taught myself some bash and already knew some AppleScript, so I have come up with this SSH chatting tool for OS X users. It allows you to chat from a terminal to another OS X computer.
One problem is that this code has to be located in an executable shell file on the computer of the person receiving the messages in order for it to work.
For example if the
```
VER="1.6 BETA"
echo "#############################"
echo " SSH CHAT TOOL BY PI.SASHA "
echo "#############################"
echo -ne "V. $VER\n\n"
printf '> '
read -n4 COMMND
while true;
do
case $COMMND in
msgs) #Standard message
echo -ne "\n*\nMESSAGE:\n"
read MSG
osascript -e 'tell application "System Events" to display dialog "'"$MSG"'" buttons "OK" default button 1'
echo -ne "*\n\n"
printf '> '
read -n4 COMMND
;;
msga) #Advanced message
echo -ne "\n*\nMESSAGE:\n"
printf 'TITLE > '
read TITLE
printf 'BUTTON > '
read BUTTON
echo "TEXT:"
read MSG
osascript -e 'tell application "System Events" to display dialog "'"$MSG"'" buttons "'"$BUTTON"'" default button 1 with title "'"$TITLE"'"'
echo -ne "*\n\n"
printf '> '
read -n4 COMMND
;;
chts) #Standart chat
echo -ne "\n*\nMESSAGE:\n"
read MSG
RSPNS=$(osascript -e 'Tell application "System Events" to display dialog "'"$MSG"'" default answer "Message" buttons "Respond" default button 1' -e 'text returned of result')
if [ -z "$RSPNS" ]; then
echo "USER CANCELLED"
else
echo -ne "---------------\nREACTION:\n$RSPNS\n*\n\n"
fi
printf '> '
read -n4 COMMND
;;
chta) #Advanced chat
echo -ne "\n*\nMESSAGE:\n"
printf 'TITLE > '
read TITLE
printf 'BUTTON > '
read BUTTON
printf 'DEF.ANSW > '
read DEFANSW
echo "TEXT:"
read MSG
RSPNS=$(osascript -e 'tell appl
One problem is that this code has to be located in an executable shell file on the computer of the person receiving the messages in order for it to work.
For example if the
sshChat.sh is located in ~, you have to go ssh username@adress, then cd ~ and then ./sshChat.sh and I would like it to work without the file being on the computer of the person receiving the messages.```
VER="1.6 BETA"
echo "#############################"
echo " SSH CHAT TOOL BY PI.SASHA "
echo "#############################"
echo -ne "V. $VER\n\n"
printf '> '
read -n4 COMMND
while true;
do
case $COMMND in
msgs) #Standard message
echo -ne "\n*\nMESSAGE:\n"
read MSG
osascript -e 'tell application "System Events" to display dialog "'"$MSG"'" buttons "OK" default button 1'
echo -ne "*\n\n"
printf '> '
read -n4 COMMND
;;
msga) #Advanced message
echo -ne "\n*\nMESSAGE:\n"
printf 'TITLE > '
read TITLE
printf 'BUTTON > '
read BUTTON
echo "TEXT:"
read MSG
osascript -e 'tell application "System Events" to display dialog "'"$MSG"'" buttons "'"$BUTTON"'" default button 1 with title "'"$TITLE"'"'
echo -ne "*\n\n"
printf '> '
read -n4 COMMND
;;
chts) #Standart chat
echo -ne "\n*\nMESSAGE:\n"
read MSG
RSPNS=$(osascript -e 'Tell application "System Events" to display dialog "'"$MSG"'" default answer "Message" buttons "Respond" default button 1' -e 'text returned of result')
if [ -z "$RSPNS" ]; then
echo "USER CANCELLED"
else
echo -ne "---------------\nREACTION:\n$RSPNS\n*\n\n"
fi
printf '> '
read -n4 COMMND
;;
chta) #Advanced chat
echo -ne "\n*\nMESSAGE:\n"
printf 'TITLE > '
read TITLE
printf 'BUTTON > '
read BUTTON
printf 'DEF.ANSW > '
read DEFANSW
echo "TEXT:"
read MSG
RSPNS=$(osascript -e 'tell appl
Solution
Avoid
Although you intend to use this script specifically on Mac OS X,
as a general rule of thumb,
it's good to avoid the various flags of
because they are not portable.
In some of your use cases the flags are completely unnecessary,
for example instead of this:
You could write like this to get the same effect:
In other cases, it's better to use
Just replace with
Use indentation
It's customary to indent code blocks that belong to a specific logical unit,
for example the body of a loop, if conditions, longer case statements,
like this:
It makes the code more readable when you see its structure.
Decompose to functions
The
The script will become easier to read if you extract those statements to functions.
Naming
I don't enjoy nitpicking on names,
but
I don't think it's worth saving one character.
echo -neAlthough you intend to use this script specifically on Mac OS X,
as a general rule of thumb,
it's good to avoid the various flags of
echo,because they are not portable.
In some of your use cases the flags are completely unnecessary,
for example instead of this:
echo -ne "V. $VER\n\n"You could write like this to get the same effect:
echo "V. $VER"
echoIn other cases, it's better to use
printf, for example here:echo -ne "\n***************\nMESSAGE:\n"Just replace with
printf, the end result will be the same.Use indentation
It's customary to indent code blocks that belong to a specific logical unit,
for example the body of a loop, if conditions, longer case statements,
like this:
while true; do
case $COMMAND in
msgs)
read MSG
osascript -e 'tell application "System Events" to display dialog "'"$MSG"'" buttons "OK" default button 1'
printf "***************\n\n"
printf '> '
read -n4 COMMAND
;;It makes the code more readable when you see its structure.
Decompose to functions
The
case statements are fairly long.The script will become easier to read if you extract those statements to functions.
Naming
I don't enjoy nitpicking on names,
but
COMMND looks like a typo and it's annoying.I don't think it's worth saving one character.
Code Snippets
echo -ne "V. $VER\n\n"echo "V. $VER"
echoecho -ne "\n***************\nMESSAGE:\n"while true; do
case $COMMAND in
msgs)
read MSG
osascript -e 'tell application "System Events" to display dialog "'"$MSG"'" buttons "OK" default button 1'
printf "***************\n\n"
printf '> '
read -n4 COMMAND
;;Context
StackExchange Code Review Q#88367, answer score: 5
Revisions (0)
No revisions yet.