James PK's Technical Journal

[ Home | Journal ]


Sun, 14 Sep 2014

Java - Basic Example - Inheritance, Overriding, Vectors, Constructors

I needed to recap my Java knowledge recently. Here are snippets of a very basic example I wrote re-familiarise myself with Java.

Inheritance: The extends keyword is used:
class Car extends MotorVehicle {
}
Constructors: Which constructor is used depends on the argument to the constructor:
	MotorVehicle (String mType){
		this.mType=mType;
	}
	
	MotorVehicle (){
	}
Overriding: overriding a method:
   	@Override 
        public String toString() {
        	return mType + " " + maxSpeed;

Collections: A basic example of using a vector to store objects and retrieve:
    Vector  mvs = new Vector(5);

    mvs.addElement(mv1);
    mvs.addElement(mv2);

    for (int i = 0;i < mvs.size();i++) {
          System.out.println(mvs.get(i));
   	 }

posted at: 00:00 | path: /java | permanent link to this entry


Sat, 09 Aug 2014

A relatively straight forward way to connect to your Raspberry Pi from a Linux Machine

  1. Obtain a USB to TTL cable.
  2. Decide how to power the Pi when using the USB to TTL cable. Either 1) Use a mico USB power adapter and do not connect the red lead to the Pi or 2) Connect the red lead to Pin #2 of the GPIO port and do not use a micro USB power adapter. For clear instructions on which lead goes to which pin see these instructions on the Adafruit website, take extra care with this as getting it wrong may lead to permanent damage to the Pi.
  3. Start a terminal & connect to the Pi:
    sudo screen /dev/ttyUSB0 115200
For further details see the Adafruit website.

posted at: 00:00 | path: /raspberrypi | permanent link to this entry


Thu, 03 Apr 2014

Encrypting a directory with ecryptfs

I needed to encrypt a directory on laptop quickly & with the minimum of fuss. So rather than re-partition drives etc (necessary for some encryption types) I used ecryptfs, which is relatively quick & straight forward to set up.

root@sal:~# apt-get install ecryptfs-utils
root@sal:~# modprobe ecryptfs
jamespk@sal:~$ ecryptfs-setup-private
Enter your login passphrase [jamespk]: #login password
Enter your mount passphrase [leave blank to generate one]:

************************************************************************
YOU SHOULD RECORD YOUR MOUNT PASSPHRASE AND STORE IT IN A SAFE LOCATION.
  ecryptfs-unwrap-passphrase ~/.ecryptfs/wrapped-passphrase
THIS WILL BE REQUIRED IF YOU NEED TO RECOVER YOUR DATA AT A LATER TIME.
************************************************************************

Logout, and log back in to begin using your encrypted directory.

All fairly straight forward, *but* I came across this warning: here https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=590081 It seems that when Debian installs ecryptfs, it does not add it to /etc/modules, which is important as that link suggests that you might well get locked out of your desktop. So make sure you add a line for `ecryptfs` in /etc/modules.

posted at: 00:00 | path: /cryptography | permanent link to this entry


Tue, 18 Feb 2014

Bash - Arithmetic Related Error

I used a date calculation in a script, that failed:

jamespk@hal:~$ let day_of_year=$(date "+%j")
bash: let: day_of_year=049: value too great for base (error token is "049")

It turns out bash is treating the number as octal because of the leading zero.

A solution is to specify the base-10 prefix
jamespk@hal:~$ let day_of_year=10#$(date "+%j")
jamespk@hal:~$ echo $day_of_year
49

posted at: 00:00 | path: /shell | permanent link to this entry


Sun, 26 Jan 2014

Bash - Testing the success of commands

When I started bash scripting I typically wrote out the sequential code and ever hopeful, assumed that after a couple of tests that the code will always succeed. Such as in this example where it's assumed a file has been scp-ed therefore the local copy can now be deleted:

#do my important scp job
scp user@host myfile 

#assume that SCP was OK, so delete local file
rm myfile

A quick test or two might prove that the script is successful but in reality, whatever happens to scp, we delete the file anyway. In the eventuality that scp fails, we probably want to keep the file. Bash stores the return status of commands is stored in the $? variable. Usually the result value of 0 indicates success:

if [ "$?" -eq "0" ] ; then
    #sucesss(?)

if [ "$?" -ne "0" ]
    #failed

Re-visiting the scp example,we can now check the return status

#do my important scp job
scp user@host myfile 
if [ "$?" -ne "0" ]
        #failed
        echo "scp error - exiting"
        exit
#should be ok to delete files
rm #etc

The return value can be easily be printed/echo-ed:

 
jamespk@hal:~$ [ "abc" = "abc" ];echo $?
0
jamespk@hal:~$ [ "abc" = "xyz" ];echo $?
1
jamespk@hal:~$ test -d "$HOMEz" ;echo $?
1

Care needs to be taken when checking the return value, particular with pipes, as we see a value of 2 returned in the first example but when used with a pipe we get 0.

jamespk@hal:~$ ls does-not-exist
ls: cannot access does-not-exist: No such file or directory
jamespk@hal:~$ echo $?
2
jamespk@fuji:~$ ls does-not-exist | head
ls: cannot access x: No such file or directory
jamespk@fuji:~$ echo $?
0

This is where the $PIPESTATUS array comes in handy:

jamespk@fuji:~$ ls x | head
ls: cannot access x: No such file or directory
jamespk@fuji:~$ echo $PIPESTATUS
2
jamespk@fuji:~$ ls x | head 
ls: cannot access x: No such file or directory
jamespk@fuji:~$ echo ${PIPESTATUS[@]}
2 0 

posted at: 00:00 | path: /shell | permanent link to this entry


Made with Pyblosxom