Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

James Robertson http://www.r0bertson.co.uk

« Newer Snippets
Older Snippets »
Showing 1-10 of 13 total  RSS 

Changing the Bash prompt

The following shell command changes the bash prompt from this "james@cryton:~/projects/pear_housekeeping2/housekeeping$" to this "james@cryton> "
export PS1="\n\u@\H>"


I tried the above example on Ubuntu 7.10.

Reference:
Tip: Prompt magic [ibm.com]
Bash Prompt HOWTO [tldp.org]

Finding your WAN IP address

My server sits behind a NAT router, so finding out my public IP address is a non-trivial task. I can use curl to poll checkip.dyndns.org for my current address:
curl -s checkip.dyndns.org

The current IP check returns the information in this format: <html><head><title>Current IP Check</title></head><body>Current IP Address: 216.239.39.99</body></html>

Using cut, I can extract just the information that I need:
curl -s checkip.dyndns.org|cut -d ":" -f2|cut -d "<" -f1

That produces something a bit more readable: 216.239.39.99

-------------------------
This article snippet was copied from My sysadmin toolbox [linux.com] while I was googling for 'apt-cache search dyndns'.

Adding new files to your github repository

Here is a set of instructions to apply new files to my github repository which is called projectx.

Before you start make sure you don't already have the repository name listed as a directory in the local current directory.

1) copy the remote repository to your local machine
syntax: git clone [uri] # eg.
git clone git@github.com:jrobertson/projectx.git

1.5) cd into the newly created local repository eg.
cd projectx

2) copy the local file to the local repository directory
eg.
cp ../projectx2/feed.rb .

3) add the local files to the local repository
syntax: git add [file] # eg.
git add feed.rb

4) Inform the git system that you have completed the required changes for this session.
git commit -a # add a message associated with this file revision

5) copy the new local repository files back to the remote repository.
git push # updates the changes back to the server

Note: The text with the square-brackets should be replaced with your own values.

Reference: A tour of git: the basics [cworth.org]

Twitter and Jaiku from the command line

The following instructions make it easy to post to Twitter and Jaiku from the command line. The instructions were copied from the article "Ubuntu Unleashed: Howto Twitter From the Command Line in Ubuntu!" [ubuntu-unleashed.com] and modified to post via Rorbuilder's ProjectX API.

sudo apt-get install curl

sudo gedit /usr/bin/jaitwit

Now Paste this in gEdit and simply replace "YourUsername" with your username and "YourPassword" with your twitter passwd, then replace the Jaiku variables (YourUsername, YourPassword, YourCity, YourAccessKey) and ctrl-s to save, then alt-F4 to exit!
curl http://rorbuilder.info/api/projectx.cgi?xml_project=%3Cproject%20name=%22micro_blog%22%3E%3Cmethods%3E%3Cmethod%20name=%22post2jaiku%22%3E%3Cparams%3E%3Cparam%20var=%22user%22%20val=%22YourUsername%22/%3E%3Cparam%20var=%22msg%22%20val=%22`echo $@|tr ' ' '+'`%22/%3E%3Cparam%20var=%22location%22%20val=%22YourCity%22/%3E%3Cparam%20var=%22apikey%22%20val=%22YourAccessKey%22/%3E%3C/params%3E%3C/method%3E%3Cmethod%20name=%22post2twitter%22%3E%3Cparams%3E%3Cparam%20var=%22user%22%20val=%22YourUsername%22/%3E%3Cparam%20var=%22msg%22%20val=%22`echo $@|tr ' ' '+'`%22/%3E%3Cparam%20var=%22password%22%20val=%22YourPassword%22/%3E%3C/params%3E%3C/method%3E%3C/methods%3E%3C/project%3E -o /dev/null
echo Message Sent!

Then chmod for exec privileges:
chmod +x /usr/bin/jaitwit

Then from the CLI type jaitwit followed by your message.
jaitwit "message here without the quotes"

Using Espeak

Espeak is a Text-to-speech program which I have used on Ubuntu. A sample of the audio can be found from my twittergram [twittergram.com]. Note: I made a few spelling mistakes in the audio, listen and tell me if you can hear them.

espeak -f hello_jr -s 120

Searching through your Twitter archive

This Ruby code downloads previous twitter entries as html files to a local file directory.
(1..5).each {|i| `wget --user=jrobertson --password=secret http://twitter.com/account/archive?page=#{i}`}

then using grep -i <keyword> * you can search for anything you've twittered in the past.

Note: Use Wget sparingly.

Convert from HTML to XHTML with HTML Tidy

This HTML Tidy example converts an html file into an xml file.

tidy -asxhtml -numeric < index.html > index.xml


example found from Tip: Convert from HTML to XML with HTML Tidy [ibm.com]

Wget's switch to preserve the filename

Download a file and preserve the filename. Without the switch -O the next time the file is downloaded the new filename would be projxmlbase.rb.1

wget -O projxmlbase.rb http://xml.mysqueak.info/p/projxmlbase.rb

Create a crontab entry using XML

This code creates a formatted crontab entry using the files crontab.xml, and crontab_txt.xsl. It is intended that all cron jobs could be stored in the crontab.xml file and then passed to the crontab.

file: crontab_txt.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="text"/>

  <xsl:template match="crontab_entry">

  <xsl:apply-templates select="minutes"/>
  <xsl:apply-templates select="hours"/>
  <xsl:apply-templates select="day_of_month"/>
  <xsl:apply-templates select="months"/>
  <xsl:apply-templates select="day_of_week"/>

  <xsl:value-of select="command" />
  </xsl:template>
  
  <xsl:template match="minutes">
    <xsl:apply-templates select="minute"/>
    <xsl:if test="not(minute) or minute=''">*</xsl:if>
    <xsl:call-template name="format_unit" />
  </xsl:template>
  
  <xsl:template match="hours">
    <xsl:apply-templates select="hour"/>
    <xsl:if test="not(hour) or hour=''">*</xsl:if>
    <xsl:call-template name="format_unit" />
  </xsl:template>
  
  <xsl:template match="day_of_month">
    <xsl:apply-templates select="day"/>
    <xsl:if test="not(day) or day=''">*</xsl:if>
    <xsl:call-template name="format_unit" />
  </xsl:template>
  
  <xsl:template match="day_of_week">
    <xsl:apply-templates select="day"/>
    <xsl:if test="not(day) or day=''">*</xsl:if>
    <xsl:call-template name="format_unit" />
  </xsl:template>
  
  <xsl:template match="months">
    <xsl:apply-templates select="month"/>
    <xsl:if test="not(month) or month=''">*</xsl:if>
    <xsl:call-template name="format_unit" />
  </xsl:template>
    
  <xsl:template match="month">
    <xsl:call-template name="format_value" />
  </xsl:template>
  
  <xsl:template match="day">
    <xsl:call-template name="format_value" />
  </xsl:template>
  
  <xsl:template match="hour">
    <xsl:call-template name="format_value" />
  </xsl:template>
  
  <xsl:template match="minute">
    <xsl:call-template name="format_value" />
  </xsl:template>
  
  <xsl:template name="format_value">
    <xsl:value-of select="."/>
    <xsl:if test="not(position() =last())"><xsl:text>,</xsl:text>
    </xsl:if>
  </xsl:template>
  
  <xsl:template name="format_unit">
    <xsl:call-template name="recurring" />
    <xsl:text> </xsl:text>
  </xsl:template>
  
  <xsl:template name="recurring">
    <xsl:if test="@every">/<xsl:value-of select="@every"/>
    </xsl:if>
  </xsl:template>
  
</xsl:stylesheet>

file: crontab.xml
<crontab_entry>
  <minutes/>
  <hours every="3">
  <day_of_month>
    <day>3</day>
    <day>10</day>
    <day>15</day>
  </day_of_month>
  <months/>
  <day_of_week/>
  <command>ssh james@192.168.1.220 runjob.sh</command>
</crontab_entry>

output: * */3 3,10,15 * * ssh james@192.168.1.220 runjob.sh

A helpful crontab.xml template
<cron>
  <minutes>
    <!-- minute (0 - 59) -->
    <minute></minute>
  </minutes
  <hours>
    <!-- hour (0 -23) -->
    <hour></hour>
  </hours>
  <day_of_month>
    <!-- day of month (1 - 31) -->
    <day></day>
  </day_of_month>
  <months>
    <!-- month (1 - 12) -->
    <month></month>
  </months>
  <day_of_week>
   <!-- day of week (0 - 6) (Sunday=0 or 7) -->
   <day></day>
  </day_of_week>
  <command></command>
</cron>

Using Ruby to delete a file directory

In this example the file we want to delete is a non-empty directory called sample.
  require 'fileutils' 

  FileUtils.rm_r 'sample'
« Newer Snippets
Older Snippets »
Showing 1-10 of 13 total  RSS