patternbashMinor
Bash script to share snippets of code using rsync
Viewed 0 times
scriptsnippetsusingsharecodebashrsync
Problem
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;
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"\n" >> "$localSnippetFile"
echo -e ""$snippetTitle"" >> "$localSnippetFile"
echo "" >> "$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 -
- 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"\n" >> "$localSnippetFile"
echo -e ""$snippetTitle"" >> "$localSnippetFile"
echo "" >> "$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 -
Solution
Your
Furthermore, since this script is designed for interactive use, you should use
This looks a little more complicated than it needs to be, you can write
The following part has a bug and is not as readable as it could be:
The bug is that
The readability improvement is to use a here document.
glenn jackman has already mentioned this: it's rude to impose your editor on the user. There is a standard for letting the user choose an editor: use the
… and instead change
read calls perform \ expansion: if the line ends with \, the shell continues reading the next line and returns the text without the backslash-newline sequence; any other backslash quotes the next character (i.e. the backslash is ignored, except that \\ yields \). This is probably not desirable; use read -r to avoid this behavior. Also, for future reference, note that whitespace at the beginning and end of the input is stripped (you need to set IFS to the empty string to avoid this, but here stripping whitespace is desirable); see Understanding IFS and the linked posts.Furthermore, since this script is designed for interactive use, you should use
read -e to enable editing of the input line (with readline). It would also be nicer to have the prompt together with the input line: use read -p 'Server name: ' and so on instead of calling echo (and don't SHOUT). For example:if [[ -z $SERVER ]]; then
read -rep 'Server name: ' SERVER
fi
rsync -avz --progress -e ssh "$UN"@"$SERVER":"$remoteSnippetFile" "$localSnippetFile"This looks a little more complicated than it needs to be, you can write
"$UN@$SERVER:$remoteSnippetFile".The following part has a bug and is not as readable as it could be:
echo -e ""$TIME"\n" >> "$localSnippetFile"
echo -e ""$snippetTitle"" >> "$localSnippetFile"
echo "" >> "$localSnippetFile"The bug is that
$TIME and $snippetTitle undergo word splitting and filename generation (i.e. globbing). For example, if $snippetTitle is The A* algorithm and the current directory contains the files Astar.tex, Astar.pdf and test.c, then you're writing The Astar.pdf Astar.tex algorithm to the file. Always use double quotes around variable and command substitutions, unless you know why you must leave them off and why it's safe to do so. (Exception: you may leave off the double quotes in contexts where they are explicitly not needed, such as inside [[ … ]].)The readability improvement is to use a here document.
cat >"$localSnippetFile"
$TIME
$snippetTitle
EOFnano "$localSnippetFile"glenn jackman has already mentioned this: it's rude to impose your editor on the user. There is a standard for letting the user choose an editor: use the
VISUAL environment variable, falling back to EDITOR. Use a sensible fallback if neither variable is set.if [[ -z ${VISUAL=$EDITOR} ]]; then
for VISUAL in sensible-editor nano vi; do
if type "$VISUAL" >/dev/null 2>/dev/null; then break; fi
done
fi
Drop this …while [[ $? = 1 ]]; do
syncSnippet
done… and instead change
syncSnippet to first call read until the user enters an accepted input, and then perform the actions based on this input.while
read -p "Sync snippet file now [y/n]: " -rN1 yn
[[ $yn != [YNyn] ]]
do :; done
if [[ $yn = [Yy] ]]; then
rsync …
fiCode Snippets
if [[ -z $SERVER ]]; then
read -rep 'Server name: ' SERVER
fi
rsync -avz --progress -e ssh "$UN"@"$SERVER":"$remoteSnippetFile" "$localSnippetFile"echo -e "<time>"$TIME"</time>\n" >> "$localSnippetFile"
echo -e "<header>"$snippetTitle"</header>" >> "$localSnippetFile"
echo "<pre></pre>" >> "$localSnippetFile"cat <<EOF >>"$localSnippetFile"
<time>$TIME</time>
<header>$snippetTitle</header>
<pre></pre>
EOFnano "$localSnippetFile"if [[ -z ${VISUAL=$EDITOR} ]]; then
for VISUAL in sensible-editor nano vi; do
if type "$VISUAL" >/dev/null 2>/dev/null; then break; fi
done
fi
Drop this …Context
StackExchange Code Review Q#12017, answer score: 4
Revisions (0)
No revisions yet.