I just got through writing a bash script so my friend and I can share snippets of code to a central file via rsync and ssh. What it does is this;
- Sets up ssh public key auth if not already working.
- Makes a local "snippet file" using rsync to copy an exiting file from remote server.
- Allows the user to edit the snippet file.
- If user chooses, sync snippet file to remote host.
I put the project on github at https://github.com/shakabra/snippetShare
It's a pretty straight-forward script, but I wonder what could be done better.
I am really new at programming and was hoping you guys might spot some obvious mistakes that I just over-looked or don't understand.
#!/bin/bash SERVER="dakinewebs.com" UN="" PORT="22" TIME=$(date +"%c") localSnippetFile=$HOME/snippetFile.html remoteSnippetFile=/home/shakabra/dakinewebs.com/snippetShare/snippetFile.html checkSsh () { ssh -o BatchMode=yes "$UN"@"$SERVER" 'exit' if [[ $? = 255 ]]; then echo "Local key doesn't exits" echo "creating local key" ssh-keygen -t rsa ssh-add uploadKey fi } sshConfig () { if [[ -z $SERVER ]]; then echo -e "SERVER NAME:\n" read SERVER fi if [[ -z $UN ]]; then echo -e "USERNAME:\n" read UN fi if [[ -z $PORT ]]; then echo -e "PORT:\n" read PORT fi } makeLocalSnippet () { echo "syncing with remote snippet" rsync -avz --progress -e ssh "$UN"@"$SERVER":"$remoteSnippetFile" "$localSnippetFile" } editSnippet () { echo -e "What's the title of the snippet?\n" read snippetTitle echo -e "<time>"$TIME"</time>\n" >> "$localSnippetFile" echo -e "<header>"$snippetTitle"</header>" >> "$localSnippetFile" echo "<pre></pre>" >> "$localSnippetFile" nano "$localSnippetFile" syncSnippet while [[ $? = 1 ]]; do syncSnippet done } syncSnippet () { read -p "Sync snippet file now [y/n]: " syncNow case $syncNow in y|Y|yes|YES|Yes) rsync -av --delete -e ssh "$localSnippetFile" "$UN"@"$SERVER":"$remoteSnippetFile" ;; n|N|no|No|NO) echo "Guess we'll sync next time. Hope it wasn't too important!" ;; *) echo "not an answer" return 1 ;; esac } uploadKey () { ssh-copy-id "$UN"@"$SERVER" ssh "$UN"@"$SERVER" 'exit' echo "Key uploaded" } sshConfig checkSsh makeLocalSnippet editSnippet