patternbashMinor
Text snippet creator/manager in Bash
Viewed 0 times
snippettextmanagercreatorbash
Problem
Below is a script to create and manage text snippets. It is also posted on GitHub. Most of the code is error checking and displaying help because I wanted to make a solid command line interface. The
I am looking for a review of the script: Is it easy to use? Is the code confusing? Are there any horrible bugs that I left unnoticed?
It is tested in OS X, so I am not sure if the syntax will work elsewhere. Specifically, the
```
#!/bin/sh
list() {
echo "snp snippets:\n"
echo "$(ls -R $snippets_dir)"
}
move() {
if [[ "$2" == "" && $3 == "" ]]; then
echo "snp usage:\n"
echo "snp move "
elif [[ -e $snippets_dir/$2 ]]; then
if -d $snippets_dir/$3 ]]; then
mv $snippets_dir/$2 $snippets_dir/$3/$2
else
echo "ERROR: Group $snippets_dir/$3 does not exist."; exit 1
fi
else
echo "ERROR: Snippet $2 does not exist."; exit 1
fi
}
new() {
if [[ "$2" == "" && "$3" == "" ]]; then
echo "snp usage:\n"
echo "snp new \"\""
echo "snp new group "
elif [[ "$2" == "group" || "$2" == "g" ]]; then # New group
if [[ "$3" != "" ]]; then
mkdir $snippets_dir/$3
echo "Created new group \"$3\"."
else
echo "snp usage:\n"
echo "snp new group "
fi
else # New snippet
if [[ "$2" == "" ]]; then
echo "snp usage:\n"
echo "snp new \"\""
else
if [[ -e $snippets_dir/$2 ]]; then
echo "Snippet $snippet_dir/$2 already exists."
echo "Overwrite? [y/n]"
read yn
if [[ $yn == "y" || $yn == "Y" ]]; then
rm $snippets_dir/$2
else
return
fi
fi
printf
blanks function is used when copying a snippet with a blank (@) character in it. The user fills in the blanks.I am looking for a review of the script: Is it easy to use? Is the code confusing? Are there any horrible bugs that I left unnoticed?
It is tested in OS X, so I am not sure if the syntax will work elsewhere. Specifically, the
pbcopy and pbpaste aliases are untested.```
#!/bin/sh
list() {
echo "snp snippets:\n"
echo "$(ls -R $snippets_dir)"
}
move() {
if [[ "$2" == "" && $3 == "" ]]; then
echo "snp usage:\n"
echo "snp move "
elif [[ -e $snippets_dir/$2 ]]; then
if -d $snippets_dir/$3 ]]; then
mv $snippets_dir/$2 $snippets_dir/$3/$2
else
echo "ERROR: Group $snippets_dir/$3 does not exist."; exit 1
fi
else
echo "ERROR: Snippet $2 does not exist."; exit 1
fi
}
new() {
if [[ "$2" == "" && "$3" == "" ]]; then
echo "snp usage:\n"
echo "snp new \"\""
echo "snp new group "
elif [[ "$2" == "group" || "$2" == "g" ]]; then # New group
if [[ "$3" != "" ]]; then
mkdir $snippets_dir/$3
echo "Created new group \"$3\"."
else
echo "snp usage:\n"
echo "snp new group "
fi
else # New snippet
if [[ "$2" == "" ]]; then
echo "snp usage:\n"
echo "snp new \"\""
else
if [[ -e $snippets_dir/$2 ]]; then
echo "Snippet $snippet_dir/$2 already exists."
echo "Overwrite? [y/n]"
read yn
if [[ $yn == "y" || $yn == "Y" ]]; then
rm $snippets_dir/$2
else
return
fi
fi
printf
Solution
Overall
-
you only ever use this function to copy stuff to the clipboard, so you don't need a temp file:
-
don't use printf "$astring"
- You're using some bash-specific things (
[[ ... && ... ]]), so your shebang line should be#!/bin/bash
- very good indentation, readable code
- more quotes:
mv "$snippets_dir/$2" "$snippets_dir/$3/$2"
list functionlsknows how to print to the screen, don't needecho $(ls ...)
move function- you need
echo -eif you want\nto be interpreted as a newline.
- syntax error on 2nd
if, missing[[
new function- if you use outer single quotes, don't need to escape inner double quotes (unless you need variable interpolation, of course)
- I would test for "yes" like this:
if [[ ${yn,} == y* ]]-- see http://www.gnu.org/software/bash/manual/bashref.html#Shell-Parameter-Expansion
blanks function- declare local variables with
local
- to read content from a file in bash:
data=$(
- bash can do arithmetic: (( count++ ))
-- see http://www.gnu.org/software/bash/manual/bashref.html#Conditional-Constructs
-
you only ever use this function to copy stuff to the clipboard, so you don't need a temp file:
blanks "$snippets_dir/$1" | pbcopy-
don't use printf "$astring"
-- if $astring contains %-directives you'll get "not enough arguments" errors. Stick to echo, or if you're specifically avoiding a trailing newline, printf "%s" "$astring"
paste function
-
I would use case` here, if you decide you need OS-specific aliases:case $(uname -a) in
Darwin) : ;;
*) alias pbcopy='...'
alias pbpaste='...'
;;
esacCode Snippets
blanks "$snippets_dir/$1" | pbcopycase $(uname -a) in
Darwin) : ;;
*) alias pbcopy='...'
alias pbpaste='...'
;;
esacContext
StackExchange Code Review Q#42159, answer score: 7
Revisions (0)
No revisions yet.