[ Site Home | Journal Home ]
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:
Vectormvs = 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
sudo screen /dev/ttyUSB0 115200
posted at: 00:00 | path: /raspberrypi | permanent link to this entry
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
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 prefixjamespk@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
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