Found reminders of a couple useful scripting tidbits today.
Check the number of commandline arguments in a bash script with
$#
. Check for a commandline argument containing only
alphanumeric values with if test "`echo $1 | egrep -v '\W'`x" !=
"x"
.
This stuff is relevant for my edit-in-gui-editor script, which basically lets you edit the clipboard in a text editor like vim. I use it to write e-mails, blog posts, whatever. Here's the whole script.
#!/bin/bash
# open a temporary file in a gui editor with the contents of the clipboard
# you can set the editor to use by setting GUI_EDITOR in ~/.bashrc
# if you specify a commandline argument, it becomes the temp file's
# extension, e.g. 'edit-in-gui-editor html'
if test "$#" = "1"
then
if test "`echo $1 | egrep -v '\W'`x" != "x"
then
EXTENSION=$1
else
EXTENSION=".txt"
fi
else
EXTENSION=".txt"
fi
# pick the editor to use.
DEFAULT_GUI_EDITOR="gvim -f"
if test "`which ${GUI_EDITOR%% *}`x" != "x"
then
GUI_EDITOR="$GUI_EDITOR"
else
GUI_EDITOR="$DEFAULT_GUI_EDITOR"
fi
# make the tempfile
TMPFILE_ORIGINAL=`mktemp`
TMPFILE="$TMPFILE_ORIGINAL.$EXTENSION"
mv $TMPFILE_ORIGINAL $TMPFILE
# do the editing
xclip -selection clipboard -o > $TMPFILE
$GUI_EDITOR $TMPFILE
xclip -selection clipboard -i $TMPFILE
# done!
exit 0
No comments:
Post a Comment