Tag: script files

RaspberryPi and Pushbullet Notifications

I posted previously about using Twlio to get an RPi to send text message notifications to me using a Twilio trial account. These trial accounts used to keep going unless you stopped using them for a period of time, but now they time out like most trial accounts do.

I scouted around looking for another notification method. I didn’t want to use email. I’d previously used sSMTP with some success, but I wanted to try something different.

Pushbullet is working well for me. You can get a free account, and apps for Android and Apple. Once you’ve got your free API authentication key you can make web requests to the Pushbullet service and they’re delivered to your mobile.

I use the cUrl app to make the requests. If you need to install it, you can do so by entering this command in a terminal. (For other unrelated purposes, I also incorporate cUrl into some applications I develop, so I always install the libcurl3 development libraries as well. If you don’t need them you can leave them off the command. For completeness, this is the command I use to install all things cUrl on my boxes.)

sudo apt-get -y install curl libcurl3 libcurl3-dev

Then I create shell scripts similar to the following which can be called by other scripts or applications, or triggered by cron.

In the title= parameter, it is often convenient to use the name of the process you’re reporting on, or the name of the RPi or PC it is running on.

#!/bin/bash

API="enter your API key here"
MSG="This is where you would put your message text"

curl -u $API: https://api.pushbullet.com/v2/pushes -d type=note -d title="Message Title" -d body="$MSG"

Nice and simple, and free.

Tags : , ,

Putting Aliases in the .bash_aliases File

I was doing some more poking around in the .bashrc file in my home folder, and I spotted these three lines.

if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi

What these lines do is look in my home folder for a file called .bash_aliases. If it is found, the file is opened and the contents are read in. Effectively it makes the contents of the .bash_aliases file appear to be part of the .bashrc file.

Because the .bashrc file is executed each time you open a terminal window, anything you put in the .bash_aliases file is executed as well. And as the name implies, the idea is that you put your personal alias definitions in the .bash_aliases file.

By default there is no .bash_aliases file, but you can create one and add some entries to it by typing:

cd ~
gedit .bash_aliases

The first line makes sure you are in your home folder, the second opens the gedit editor and tells it to open a file called .bash_aliases. If no such file exists, it is created. These are some lines from my .bash_aliases file.

alias cls=clear
alias h.=history
alias hf=’history | grep $1′
alias pf=’ps -e | grep $1′

The first alias sets cls to clear the screen. It’s just faster to type cls than it is to type clear. Likewise the second line. It’s fewer keystrokes to hit h. instead of history.

The hf alias is a bit more interesting. I use the history option a lot. hf makes it a bit easier to locate the command you want. For instance, to see the files you have recently opened in gedit, you could type:

hf gedit

The result on my test machine is shown here. The commands that have gedit in them are listed with the gedit highlighted in red.

Output from the hf alias
Output from the hf alias

The pf alias does the same sort of thing, but with process names instead of command names. You can use it to list all of the processes that have a certain word in their name. In the example below I have used the pf alias twice, once to list all the processes that have gnome in their name and once to list all processes that have unity in their name.

Output from the pf alias
Output from the pf alias

Note that when you have typed your aliases into the bash_aliases file, you must save the file, and exit the terminal session, and open a new terminal window before the aliases become available for use.

Remember, because the .bash_aliases file is a dotfile (its name starts with a full stop), the .bash_aliases file will not be listed when you use the ls command.

To see dotfiles you have to use the the -a or -A parameters, or use the la system-provided alias.

Tags : , , ,

Ready Made Aliases

If you go poking around in the .bashrc file, located in your home folder, you’ll see the following lines:

alias ll=’ls -alF’
alias la=’ls -A’
alias l=’ls -CF’

These provide some ready made aliases for you. Alisases take commonly used commands and provide abbreviations for you so that you don’t have to type the original command in again and again.

For example, if you type:

ll

in a terminal window Ubuntu will treat it as though you had typed:

ls -alF

Typing:

la

is the equivalent of having typed:

ls -A

And typing:

l

is the equivalent of having typed:

ls -CF

To see a description of what the various options for ls do, type:

man ls

You will see a display like this:

Output from man is
Output from man is

Use q to quit from the man session when you wish to return to the command prompt.

Tags : , ,

Get a Colour Command Prompt in your Terminal Windows

Whenever you open a terminal window a script file called .bashrc is run. This establishes certain settings within your terminal environment.
In your home folder, if you type:

cat .bashrc

You will see the contents of this file listed to the screen.

To stop the output whizzing past in a flash type the following:

cat .bashrc | less

This will allow the less program to handle the output. You can then scroll backwards and forwards through the contents of the file using the Home, End and Up Arrow and Down Arrow keys. Q will exit back to the prompt. This is a convenient way to examine text files safely – there is no danger of you accidently making changes to them.

Within the .bashrc file are some lines that look like this:

# uncomment for a colored prompt, if the terminal has the capability; turned
# off by default to not distract the user: the focus in a terminal window
# should be on the output of commands, not on the prompt
#force_color_prompt=yes

if you want to have a colour prompt in your terminal windows, in your home folder type:

gedit .bashrc

This causes the gedit editor top open the file so you can make changes to it. Be Careful! Change the line that said:

#force_color_prompt=yes

to now say:

force_color_prompt=yes

Save the file, and close gedit. Close the terminal window and open a new terminal window. I use keyboard shortcuts a lot, so I do this through:

Ctrl+Alt+T

You should now see a colour prompt.

It’s not life-changing perhaps, but it helps to visually distinguish the output from the shell prompt.

Tags : , ,