4

I'm using zsh on Ubuntu 14.04 over SSH using Putty and I'm setting up the key bindings for my keyboard. Because zsh doesn't seem to make use of my function keys I thought I'd setup scripts to do operations similar to what the pictures on the keys represent. I'm working on the email button and I have it working pretty well but I would like it to be better. This is what I have in ~/.zshrc:

# Ensure we are in emacs mode
bindkey -e

# This requires you to enable the ATOM feed in Gmail. If you don't know what that is then
# go ahead and try this and let it fail. There will then be a message in your inbox you
# can read with instruction on how to enable it. Username below should be replaced with 
# your email id (the portion of your email before the @ sign).
_check-gmail() {
    echo
    curl -u username:password --silent "https://mail.google.com/mail/feed/atom" | tr -d '\n' | awk -F '<entry>' '{for (i=2; i<=NF; i++) {print $i}}' | sed -n "s/<title>\(.*\)<\/title.*name>\(.*\)<\/name>.*/\2 - \1/p"
    echo
    exit
}
zle -N _check-gmail


# F2 - Display Unread Email
bindkey "^[[12~" _check-gmail

When used like above it works. I'm having two problems.

First and foremost, I would rather have it ask me for a password instead of leaving it in the script like this. This can easily be done by removing :password from the curl command at the command line but when used within this file it causes problems. Specifically, it appears to accept the first key press but the rest drop out to another shell which is not the password input.

Second, the first time I run this in a shell it works perfect. After that it doesn't return to the prompt correctly. I need to press Enter to get another prompt. Is there a way to fix that?

I've put the complete key bindings section of my .zshrc file on GitHub.

2 Answers 2

3

The problem is that curl expects some normal terminal settings and zle doesn't expect you change the terminal settings. So you can write it instead:

_check-gmail() {
  zle -I
  (
    s=$(stty -g)  # safe zle's terminal setting
    stty sane     # sane settings for curl
    curl -u username --silent "https://mail.google.com/mail/feed/atom" |
     tr -d '\n' | awk -F '<entry>' '{for (i=2; i<=NF; i++) {print $i}}' |
     sed -n "s/<title>\(.*\)<\/title.*name>\(.*\)<\/name>.*/\2 - \1/p"
    stty $s       # restore zle's terminal settings
  ) < /dev/tty
}
1

One approach would be to prompt the user prior to running your _check-gmail function so that your script has the password in hand, in a variable. Then you'd pass the password variable into your function so that the curl command can make use of it.

For example:

$ tst_fun () { echo "Parameter #1 is $1"; }
$ tst_fun "my_pass"
Parameter #1 is my_pass
$ 

To get the password from the user of your script you have several options. If you want a nice GUI, you could use zenity to pop a dialog asking for the password.

For example:

my_pass=$(zenity --password)
echo "$my_pass"

                                          ss #1

Now when the above commands are run the results that were typed into the dialog are available in the variable $my_pass. So if I were to type the password "supersecret" in the dialog I'd get this:

$ my_pass=$(zenity --password)
$ echo $my_pass
supersecret
4
  • I'm in Putty so a GUI input box isn't going work. I will try to read a var from cli though.
    – krowe
    Commented Aug 22, 2014 at 13:18
  • It just skips right over the read $input line without taking my input.
    – krowe
    Commented Aug 22, 2014 at 13:23
  • @krowe - to help further I'm gonna need to see the actual script.
    – slm
    Commented Aug 22, 2014 at 13:28
  • github.com/krowe/kshrc-k800-keybindings/blob/master/…
    – krowe
    Commented Aug 22, 2014 at 13:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .