Posts Tagged ‘bash’

Mutt aka Bash Is Still King

December 5th, 2011 subogero 4 comments

I’ve had problems with sending emails from the command line before. On Windows. That’s how mapis was born. This time it’s the same story on Linux. I want to send an email from the command line with an attachment. The rather lovely Evolution does not support it.

In comes mutt, the even more lovely terminal based email client. But I don’t want to loose features:

  • Gmail IMAP access
  • integration with my Gmail contacts
  • Hungarian spell-check
  • mutt as my default mail client in Gnome, so Firefox uses it when Sending Link…

And while I’m at it, I want some new features as well:

  • editing email with vim (with Hungarian spell-check, obviously)
  • bonus: email desktop notifications, even when the client is not running


apt-get install mutt  # as root

My ~/.muttrc config file:

set from = "your@email"
set realname = "Your Name"
set imap_user = "your@email"
set imap_pass = "yourpassword"
set folder = "imaps://"
set spoolfile = "+INBOX"
set postponed = "+[Gmail],Drafts"
set trash = "imaps://[Gmail]/Trash"
set header_cache = ~/.mutt/cache/headers
set message_cachedir = ~/.mutt/cache/bodies
set certificate_file = ~/.mutt/certificates
set smtp_url = "smtp://your@email:587/"
set smtp_pass = "your_password"
set editor  = "vi"
set query_command = "goobook query '%s'"
set sort = "threads"
set sort_aux = "last-date-received"

color normal white default
color index white default ~p
color index white default ~P
color index white default ~O
color index brightwhite default ~N
color tree red default
color hdrdefault cyan default
color quoted green default
color tilde white default
color signature blue default
color attachment brightyellow default
color status brightwhite green
color indicator black white

You’ve probably noticed vi as the editor, some options that don’t let you miss new posts to old email threads and a colour scheme that fits well into Linux Mint. Also goobook is mentioned, which can access your Gmail  contacts from the command line. Homework: find the goobook website on the internet! Then download and extract the source tarball, run

sudo ./ install

Then create its config file ~/.goobookrc

email: your@email
password: your_password

As the above config files store your password, don’t forget to change their permissions:

chmod 700 ~/.muttrc ~/.goobookrc

As to the Hungarian spell-check in vi, add a line to ~/.vimrc which maps it to <F7>, like some office tools:

map <F7> :setlocal spell spelllang=hu<Enter>

The first time you activate it, it will download the Hungarian database, then highlight the misspelled words with red. Move around them with [s and ]s and let vim suggest correct spelling with the z= key combo. It kicks ass indeed.

Mutt as Gnome’s default email client? Main Menu (I don’t use the Mint Menu), System, Preferences, Preferred Applications. Mail Reader. Choose Custom, then specify “/usr/bin/mutt %s” as the command. Run in terminal of course. Test with Send Link… in Firefox.

Now all is well. Just try to send a file attachment from the command line.

mutt -a foo.txt

opens mutt, which in turn asks for the mail recipient. Just type a part of the name or email, hit Ctrl-t, and mutt will complete the email address from your Gmail Contacts. Then enter a subject and voilá, vi opens up to edit the mail body. Edit, spellcheck, ZZ, y. Done.

And now for something completely different. Desktop notifications for new email, even when no email client is running. I’ve found a nice mutt-Gmail tutorial blogpost somewhere, I forget where. It contained a Python script for email notifications. I opened it. I got scared. 200 lines of code! I nearly gave up. But after  taking a courageous look I realized it just parsed the Gmail atom feed and called Gnome’s notify-send. Using a class and using an XML parsing module.

Python is a rather lovely language, but I have to say its adherents are famous for writing bloated code. I bet I can write a 10-line shell script for that. I ended up with 11 lines, but including the shebang and a configurable checking period. It’s among my Gnome Startup Applications:

SLEEP=`echo $1 | egrep '^[1-9][0-9]*$'`
[ -z "$SLEEP" ] && SLEEP=300
USER=`sed -rn 's/^set imap_user = "(.+)"$/\1/p' ~/.muttrc`
PASS=`sed -rn 's/^set imap_pass = "(.+)"$/\1/p' ~/.muttrc`
while true; do
 EMAILS=`curl -u ${USER}:${PASS} \
 | sed -rn 's/<name>(.+)<\/name>$/\1/p'`
 [ -n "$EMAILS" ] && notify-send "NEW EMAIL" "$EMAILS"
 sleep $SLEEP

Bash is still king.

Categories: Uncategorized Tags: , , , ,

CyanogenMod 7 Update

November 11th, 2011 subogero Comments off

A few days after installation, I wanted to install ssh onto the phone. I turned out dropbear is installed by default, I just had to set up some keys.

Then, to my utter amazement, I found a bash directory in /etc. I typed bash, and suddenly there it was, the world’s favourite shell in all its glory and coloured prompt!

Bash – Check if Directory Writable

February 28th, 2010 subogero 2 comments

Back to the basics, a.k.a. the Bourne Again Shell.

How to check in a script whether a directory is writable? Many would suggest the

if [ -w $DIR ]; then echo Do semething; fi

way, but it just checks the writable flag, which may be set on something on a read-only-mounted device. It’s much better to effectively try it.

Do something if $DIR is not writable

touch ${DIR}/foo && rm -f ${DIR}/foo || echo Do something

Do something if $DIR is writable

touch ${DIR}/foo && rm -f ${DIR}/foo && echo Do something

Do something if $DIR is writable, otherwise suppress the error message

touch ${DIR}/foo 2>/dev/null && rm -f ${DIR}/foo && echo Do something

Do something if $DIR is writable, otherwise print a custom diagnostic message

touch ${DIR}/foo 2>foo && rm -f ${DIR}/foo && echo Do something
cat foo | sed 's/.*/Directory not writable!/' && rm -f foo

The above case is a bit special. Let’s see what happens:

  • touch attempts to create file “foo” in $DIR
  • it’s potential error message is redirected to file “foo” in currect dir
  • the commands afters “&&” are only executed if touch has succeeded
  • if so, file “foo” in $DIR is removed and we “Do something”
  • the error message stored in “foo” is replaced by our own, using sed
  • file “foo” with the original error message is removed.

Note: the sed-pipeline cannot be integrated into the first line, because pipelines only fail if their last command does, so we would forget whether “touch” has failed or not.

Categories: Uncategorized Tags: , ,

Bash Job Control

September 24th, 2009 subogero Comments off

Having already explored the  possibilities of starting parallel processes using the Gnome window-manager, as described on my go page, it was high time to immerse myself, being the keyboard-addicted geek that I am, into doing the same by using just a simple terminal.

But what’s the point? In my case, it was a commit process with a revision-control system called foo. The commit is done with a script, and I wanted to make sure the project compiles OK before the actual commit.

This involves two quite long processes, which could nicely run parallel, one is composing the commit-message with an editor, and the other is the compilation. We shall go on with the commit after BOTH are finished. Oh, I nearly forgot, the commit shall be canceled, if the compilation fails!

I studied bash job-control a bit and then:

# The '&' character runs the command in the background
# which in turn becomes job number 1, later referred to as %1.
make &

# Compose the commit-message in the foreground
foo-status > CommitMessage
mcedit CommitMessage

# Wait for the make (job 1, alias %1) to complete,
# check its exit status and abort if it's failed.
# The 'wait' command's exit status is the same as the job's we wait for
wait %1
if [ $? -ne 0 ]; then exit 1; fi

# Do the commit
foo-commit --message=CommitMessage
rm CommitMessage

Try this with batch-files on Windows. Actually, you can do it with Cygwin. I’m sure Richard Stallman calls this platform GNU/Windows.

Bash is cool. Bourne Again Shell for Born Again Christians, that’s what I always say. Or as Master Foo put it once:

“There is more Unix-nature in one line of shell script than there is in ten thousand lines of C.”

Categories: Uncategorized Tags: ,