Skip to content

Instantly share code, notes, and snippets.

@colinsheppard
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save colinsheppard/9301493 to your computer and use it in GitHub Desktop.
Save colinsheppard/9301493 to your computer and use it in GitHub Desktop.
#!/bin/bash
server()
{
# Wait for command file to show up and send it to running bash
fswatch $cmdDir "cat $cmdFile | /bin/bash -i >> $logFile 2>&1"
}
client()
{
local lastSize=$(stat -f %z $logFile)
local size=$lastSize
local wheel=("|" "/" "-" "\\\\") # Spinning wheel
# Force a prompt at start to confirm connection
local line="echo \"session started on $serverCode\""
echo "" > $cmdFile
while [[ -n $line ]] || read line
do
# Send command
echo "$line" > $cmdFile
# Wait for output
echo -n "Waiting...."
let i=0
while [[ $size -eq $lastSize ]]
do
# Should really use inotifywait here but it doesn't seem to work
# with the way Dropbox updates $logFile.
sleep 0.5
echo -n -e "${wheel[$i]}\b"
let i=(i+1)%4
size=$(stat -f %z $logFile)
done
# Display output
if [[ $size -lt $lastSize ]]
then
echo "Error; please try again"
else
echo -n -e "\r \r"
tail -c $((size - lastSize)) $logFile | grep -v "bash-3.2$ exit" | grep -v "bash: no job control in this shell"
fi
lastSize=$size
line=
done
}
if [[ $1 = "-s" ]]
then
isServer=true
serverCode=$2
else
isServer=false
serverCode=$1
fi
baseDir=~/Dropbox/remote/$serverCode
cmdDir=$baseDir/in
cmdFile=$baseDir/in/in
logFile=$baseDir/out/out
if [[ $1 = "-s" ]]
then
server
else
client
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment