tag:blogger.com,1999:blog-27498169162264544222024-03-18T21:41:35.229-07:00Karl Lankester-CarthySystem.out.println("Keeping the brain pain at bay.");Anonymoushttp://www.blogger.com/profile/03631013823036663113noreply@blogger.comBlogger36125tag:blogger.com,1999:blog-2749816916226454422.post-54797447476502386992014-09-26T08:25:00.000-07:002014-09-26T08:26:53.167-07:00Arduino MEGA 2560 Board issuesRecently at work I found these on my desk:<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgEXoh5IxUWhjgDpbMB-h6v5TzHcIqEwHEwNLfSSoEvbGQf7zHL7un7i75K-N5_TBbThUDpNLomHEIWCrr62C1bkPc0mX3WC1dkz1gIAGQ_c8CF77YZMdzKb98Y0U18MI6VQubkZUEsLSW0/s1600/IMG_20140918_133242.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgEXoh5IxUWhjgDpbMB-h6v5TzHcIqEwHEwNLfSSoEvbGQf7zHL7un7i75K-N5_TBbThUDpNLomHEIWCrr62C1bkPc0mX3WC1dkz1gIAGQ_c8CF77YZMdzKb98Y0U18MI6VQubkZUEsLSW0/s1600/IMG_20140918_133242.jpg" height="227" width="400" /></a></div>
<div style="text-align: center;">
<span style="font-size: x-small;"><i>ARDUCAM Shield, </i><i>Arduino MEGA 2560 Rev3 (microprocessor), </i><i>2MP OV2640 (camera)</i></span></div>
<br />
<a name='more'></a><br />
<br />
With these were the instructions "Make this work."<br />
<br />
That's pretty much all I had to go on, so I made a start by looking on-line for ARDUCAM, thankfully I found a helpful tutorial on the ARDUCAM website, this made things a lot easier<br />
and I managed to assemble the boards and "Make it work":<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgx4y3z8wOFviQlYJmQdjmvx2raUyu6iysAF9AFQ6NaKSL4QDt6Hlj57R1SlPVTvpLV6jvdjUPEHe3i1AulC0CiRFX1rUAsaiB4eS85Sy-oOx_Iu2PxVBB19jgIMWrmCvjnFhss5R7DqT7b/s1600/IMG_20140918_145717.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgx4y3z8wOFviQlYJmQdjmvx2raUyu6iysAF9AFQ6NaKSL4QDt6Hlj57R1SlPVTvpLV6jvdjUPEHe3i1AulC0CiRFX1rUAsaiB4eS85Sy-oOx_Iu2PxVBB19jgIMWrmCvjnFhss5R7DqT7b/s1600/IMG_20140918_145717.jpg" height="227" width="400" /></a></div>
<br />
Now my challenge was to take a photograph with this camera and have it save the image to the SD card on the MEGA 2560. Arduino use there own IDE which you need to download in order to install the firmware into these boards. Fortunately for me they also provide a wide variety of example scripts written in C which you can upload to the board to test certain functions. (in the photo above I'm running the UTFT_Demo to see the screen working)<br />
<br />
I have some history with C but I wouldn't say I was a professional, I know enough to recognise the language and fumble my way through it. Anyway I found a script which lets me take a photo and save it to the SD card, although like most things it's never that simple.<br />
<br />
For reasons unknown to me at the time the device would let me take a photo but then freeze and not save anything to the SD card, so I decided to view the output of the serial port it was connected to. Turns out it couldn't recognise the SD card, so when I was taking a photo it was stuck in an endless loop trying to save the image.<br />
<br />
The issue turned out to be SPI (Serial Peripheral Interface) on the MEGA 2560 board. The hardware SPI was doing what it was supposed to do but it was being overridden by a software SPI (Both called SPI in the code.....) The hardware SPI was needed for the TFT display to show the image, and the software SPI was needed to access the SD card. So I had to change the pin set for the software SPI in order to make it work. After I did that it started working perfectly fine.<br />
<br />
<br />
I'm a total noob when it comes to hardware, but this has inspired me to learn more about how to program microprocessors, so I did what any noob would do, I bought a Raspberry Pi.<br />
<br />
<a href="http://www.adafruit.com/categories">This site </a>is enough to inspire anyone to get started with this stuff. My end goal with my new Pi is to control a robotic arm with a glove. Until then I think I'm going to attempt a retro game station. watch this space!<br />
<br />
<br />
<br />
<br />
<br />Anonymoushttp://www.blogger.com/profile/03631013823036663113noreply@blogger.com0tag:blogger.com,1999:blog-2749816916226454422.post-1698932598640302652014-09-15T05:36:00.001-07:002014-09-15T05:36:49.012-07:00Problem 6<b>Problem 6</b><br />
<br />
Sixth problem from <a href="https://projecteuler.net/">project Euler</a>.<br />
<a name='more'></a>This one was surprisingly easy, I remember having issue with this problem back at uni while learning C#. This time around it seemed quite simple<br />
<br />
<b>Question:</b><br />
<div style="font-family: 'Trebuchet MS', sans-serif; font-size: 16px;">
<i>The sum of the squares of the first ten natural numbers is,</i></div>
<div style="font-family: 'Trebuchet MS', sans-serif; font-size: 16px; text-align: center;">
<i>1<sup>2</sup> + 2<sup>2</sup> + ... + 10<sup>2</sup> = 385</i></div>
<div style="font-family: 'Trebuchet MS', sans-serif; font-size: 16px;">
<i>The square of the sum of the first ten natural numbers is,</i></div>
<div style="font-family: 'Trebuchet MS', sans-serif; font-size: 16px; text-align: center;">
<i>(1 + 2 + ... + 10)<sup>2</sup> = 55<sup>2</sup> = 3025</i></div>
<div style="font-family: 'Trebuchet MS', sans-serif; font-size: 16px;">
<i>Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 − 385 = 2640. </i><i>Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.</i></div>
<br />
Answer:<br />
<pre class="java" name="code">public class problem6 {
public static void main(String[] args) {
double rootSum = 0;
double sum = 0;
for(double i = 0; i < 101; i++){
sum += i;
double sqr = i * i;
rootSum += sqr;
}
System.out.println((int)((sum * sum)-rootSum));
}
}</pre>
Anonymoushttp://www.blogger.com/profile/03631013823036663113noreply@blogger.com0tag:blogger.com,1999:blog-2749816916226454422.post-50416479578551740782014-09-09T12:43:00.000-07:002014-09-09T12:43:20.741-07:00Slow internet = ParanoiaI'm not exactly a network guy, Back in the first year of uni I did a few Cisco certification courses and it bored me to death.<br />
<br />
<a name='more'></a><br /><br />
Today while sitting in bed, eating shreddies (I'm definitely one of life's winners :D) my internet started to slow down. I started to think "If someone was to hack into my network would I know about it?" So I started to do some research.<br />
<br />
I thought I would start by looking at some basic attacks, Man In The Middle (MITM) was one that I knew of so I thought I would look into how it actually worked. It's basically ARP Spoofing from what I could tell. ARP stands for Address Resolution Protocol, it's this protocol that maps the IP addresses to the hardware addresses (MAC). The basic idea can be described by the diagram:<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg2ioa4dmGiaL9pEA_d99jK4LVcKlwFhO2e9M4HSWZhIChd7M4fWmOODtqFvVQQGFTA19WRL3ChtpqtjL2MVxhNgBHhyphenhyphenHhqEmMmKENs5yuTFsl4wNK5LeA4N1G4oHFlOh7Gx-hSdAxr25vi/s1600/Main_the_middle.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg2ioa4dmGiaL9pEA_d99jK4LVcKlwFhO2e9M4HSWZhIChd7M4fWmOODtqFvVQQGFTA19WRL3ChtpqtjL2MVxhNgBHhyphenhyphenHhqEmMmKENs5yuTFsl4wNK5LeA4N1G4oHFlOh7Gx-hSdAxr25vi/s1600/Main_the_middle.JPG" height="221" width="400" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
While looking into ARP I started to understand the use of MAC addresses. Each hardware device has a hard coded mac address, but this can be "spoofed" in the software so when you sent information you can make it look like it's coming from some other hardware. This is used for ethical and non-ethical reasons, If you're going to look into this I suggest you do it on your own network with your own devices.<br />
<br />
I downloaded a tool called <a href="http://ettercap.github.io/ettercap/">ettercap</a> which helped me test this attack out on my own network, it worked quite well and just goes to show how easy it is when you know how. Needless to say after this I changed my router password to something stronger, for all the good it will do...<br />
<br />
Turns out my network was not compromised, it's just slow and desperately needs an upgrade...<br />
<br />
<i>(Note: Did you know that the first three octets of a mac address identify the manufacture of the hardware it is assigned to? Seriously type the first three octets of your mac address in <a href="https://www.wireshark.org/tools/oui-lookup.html">here</a> )</i><br />
<br />
<br />Anonymoushttp://www.blogger.com/profile/03631013823036663113noreply@blogger.com0tag:blogger.com,1999:blog-2749816916226454422.post-44161428061478298492014-09-08T09:53:00.000-07:002014-09-09T07:12:34.999-07:00Adding The Konami Code!I'm currently designing a website for my wedding next year. The idea is that people will be able to R.S.V.P., add a song to the play list and choose dietary options via the website.<br />
<br />
<a name='more'></a><br />
So I'm using a <a href="http://getbootstrap.com/">bootstrap</a> modal to show my R.S.V.P., I'm using a mix of plain old JavaScript and jQuery to fade elements in and out. For example Question 1: "Are you coming?", if answered 'Yes' fade out question 1 and fade in Question 2...etc..<br />
<br />
SO far it's going pretty well, I'm learning quite a bit about browser compatibility and what's new in CSS.<br />
<br />
One cool thing I have learnt is the <a href="http://en.wikipedia.org/wiki/Konami_Code">konami code</a>.<br />
<br />
The JavaScript for this is actually quite easy, the code and keyPadCode variables represent the characters, which can be found <a href="http://www.cambiaresearch.com/articles/15/javascript-char-codes-key-codes">here</a>. All I'm doing is waiting for the key sequence and then launching my countdown function.<br />
<br />
<pre class="javascript" name="code">$(document).ready(function(){
var keys = [];
var code = '52,48,52';
var keyPadCode = '100,96,100'
$(document).keydown(
function(e) {
keys.push( e.keyCode );
if ( keys.toString().indexOf( code ) >= 0 ){
$("#secret").modal('show');
CountDownTimer('09/19/2015 1:0 PM', 'countdown');
keys = [];
} else if ( keys.toString().indexOf( keyPadCode ) >= 0 ){
$("#secret").modal('show');
CountDownTimer('09/19/2015 1:0 PM', 'countdown');
keys = [];
}
});
});
</pre>
<br />
I was going to keep the original:<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjgurPUW4HQoQvsTSlYZuFnLa6-y376MxFkBk_j7JEyGwSAhjYDqu21LX1j9wM7Kh6S2mKwlOU15I1Khw1s3f0CdXNQ_rlMMjMC8XS3IqeRXCgH2QKOYF30pgc98d9IpRZajOJxkgAAKO4T/s1600/Konami.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjgurPUW4HQoQvsTSlYZuFnLa6-y376MxFkBk_j7JEyGwSAhjYDqu21LX1j9wM7Kh6S2mKwlOU15I1Khw1s3f0CdXNQ_rlMMjMC8XS3IqeRXCgH2QKOYF30pgc98d9IpRZajOJxkgAAKO4T/s1600/Konami.png" height="37" width="400" /></a></div>
<br />
<br />
<br />
<br />
But I thought I'd use '404' (from the 404 Not Found HTTP request). So when the user types '404' (on the number pad or top line of numbers) The following countdown to WEDDINGGEDON! is shown:<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgXQwASokDbHpLaUom_Pt9NHn10exZt0hRMRPXcxR7vypayGsKUNvKOVXMpYngjKIFBnUodOxeCv6bissx86OB6bRTimgm3wb_WunX6BfUJ2J6hSqkstyDAVh9G_rqJwwi7yQwHntctdN4F/s1600/weddinggedon.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgXQwASokDbHpLaUom_Pt9NHn10exZt0hRMRPXcxR7vypayGsKUNvKOVXMpYngjKIFBnUodOxeCv6bissx86OB6bRTimgm3wb_WunX6BfUJ2J6hSqkstyDAVh9G_rqJwwi7yQwHntctdN4F/s1600/weddinggedon.png" height="122" width="400" /></a></div>
<div>
<br /></div>
<div>
Pretty cool eh?</div>
<br />Anonymoushttp://www.blogger.com/profile/03631013823036663113noreply@blogger.com0tag:blogger.com,1999:blog-2749816916226454422.post-1355778101088162582014-08-29T08:09:00.000-07:002014-08-29T08:44:18.702-07:00Problem 5<b>Problem 5</b><br />
<br />
Fith problem from <a href="https://projecteuler.net/">project Euler</a>. <br />
<a name='more'></a>This is my second attempt. My first attempt took roughly 5 seconds to run, not very good really, I wasn't using the value they gave as a starting point so it was counting to 2520 when it didn't need to... Feedback on better ways to do these are most welcome!<br />
<br />
<b>Question:</b><br />
<br />
<i>2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.
<br />
What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?</i><br />
<br />
Answer:<br />
<br />
<pre class="java" name="code">
public class problem5 {
public static void main(String[] args){
int current = 2520;
while (true) {
if (isDivisible(current))
break;
current++;
}
System.out.println("Smallest multiple: " + current);
}
public static boolean isDivisible(int num) {
for (int i = 2; i <= 20; i++) {
if (num % i != 0)
return false;
}
return true;
}
}
</pre>Anonymoushttp://www.blogger.com/profile/03631013823036663113noreply@blogger.com0tag:blogger.com,1999:blog-2749816916226454422.post-92107675104032736352014-08-29T06:18:00.000-07:002014-08-29T06:18:54.036-07:00Checked versus unchecked exceptionsI get asked this question in interviews. "<span style="color: orange;">What is the difference between checked and unchecked exceptions?</span> "<br />
It's a questions that any programmer with a bit of experience should know. <br />
<a name='more'></a>It's not a difficult question but I do tend to miss out some points occasionally, so I though I'd write down the differences here:<br />
<br />
<b>Unchecked exceptions:</b><br />
<br />
<ul>
<li>These are actual bugs in the program, so bugs which reflect errors in the logic of your program. Programs with unchecked exceptions will compile just fine, but when they run, they will break. For example, if you wrote a program to divide by 0, it would compile, but when you run it you will see a <span style="color: orange;">ArithmeticException</span>. The most common unchecked exception you will see as a Java developer is most likely to be the <span style="color: orange;">NullPointerException</span>.</li>
</ul>
<br />
<b>Checked exceptions:</b><br />
<ul>
<li>These are foreseen bugs in the program, they will stop the program compiling. An example of a checked exception would be the <span style="color: orange;">FileNotFoundException</span>. pretty self explanatory really, but if you write a program which reads in a file using something like <span style="color: orange;">FileInputStream</span>, it will error with a unhandled exception type <span style="color: orange;">FileNotFoundException</span>, so you must use try catch, or throw the exception.</li>
</ul>
<ol>
</ol>
<div>
<br /></div>
<div>
<br /></div>
Anonymoushttp://www.blogger.com/profile/03631013823036663113noreply@blogger.com0tag:blogger.com,1999:blog-2749816916226454422.post-34104036797372240812014-08-10T05:09:00.003-07:002014-08-10T05:09:32.633-07:00Show Commits To SVN BranchThis script is designed to show a number of recent commits to the users current SVN branch in an easy readable format.<br />
<a name='more'></a> I'm up for criticism on this as I'm not a huge fan of the amount of if statements...<br />
<pre class="bash" name="code">#!/bin/bash
NUM=$1
URL=`(cd /home/path/to/workspace ; svn info |grep '^URL:' | sed -e 's/^URL: //')`
txtylw=$(tput setaf 3)
txtgrn=$(tput setaf 2)
txtblu=$(tput setaf 4)
txtbld=$(tput bold)
txtrst=$(tput sgr0)
if [ -n "$1" ]; then
echo "${txtbld}${txtylw} Showing you the last $1 commits to $URL ${txtrst}"
echo "________________________________________________________________________"
svn log --verbose --limit $1 $URL | while read line; do
if [[ $line == "M /"* ]]; then
echo "${txtbld}${txtblu} $line ${txtrst}"
else if [[ $line == "A /"* ]]; then
echo "${txtbld}${txtgrn} $line ${txtrst}"
else if [[ $line == "---"* ]]; then
echo "${txtbld} $line ${txtrst}"
else if [[ $line == "r"[0-9]* ]]; then
echo "${txtgrn}${txtbld} $line ${txtrst}"
else
echo "$line"
fi
fi
fi
fi
done
else
echo "Please provide a limit parameter! (e.g 10)"
fi
</pre>
Anonymoushttp://www.blogger.com/profile/03631013823036663113noreply@blogger.com0tag:blogger.com,1999:blog-2749816916226454422.post-77449292530698051092014-08-10T05:09:00.002-07:002014-08-10T05:09:24.212-07:00Host InformationThis script is designed to retrieve host information from the kernel to the memory. It assigns standard commands to variables and then echo's everything out in a nice readable form.<br />
<br />
<a name='more'></a><br /><br />
<pre class="bash" name="code">#!/bin/sh
_HOSTNAME=`hostname`
_HOSTTYPE=`echo $HOSTTYPE`
_MACHINETYPE=`echo $MACHTYPE`
_OSTYPE=`echo $OSTYPE`
_VENDOR=`echo $VENDOR`
_KERNEL=`uname -r | awk -F- '{print $1}'`
_MEM=`cat /proc/meminfo | awk '/MemTotal/ {print $2 $3}'`
_CPU=`cat /proc/cpuinfo | grep 'cpu MHz' | awk '{print $4}'`
echo '=============================='
echo 'HOSTNAME ' $_HOSTNAME
echo 'HOSTTYPE ' $_HOSTTYPE
echo 'MACHINETYPE ' $_MACHINETYPE
echo 'OSTYPE ' $_OSTYPE
echo 'VENDOR ' $_VENDOR
echo 'KERNEL ' $_KERNEL
echo 'MEM INFO ' $_MEM
echo 'CPU INFO ' $_CPU
</pre>
Anonymoushttp://www.blogger.com/profile/03631013823036663113noreply@blogger.com0tag:blogger.com,1999:blog-2749816916226454422.post-77173077440143884452014-08-10T05:09:00.001-07:002014-08-10T05:09:15.893-07:00IP TelnetThis script is your basic Telnet script.<br />
<b>In order to use this script you must create a text file called 'ip' and fill it with the ip(s) you wish to connect to.</b><br />
<a name='more'></a><br />
The script loops through the list of IP addresses in the file and ping's them. Once it has a ping it starts Telnet and issues commands between sleeps.
Just fill out the relevant information (username, password and required commands). The script loops through the ip.txt file and ping's the address, if the ping is successful it will go on to do the telnet, if not it will skip that IP and move on to the next.
<br />
<pre class="bash" name="code">#!/bin/bash
while read line
do
# -c 1 means ping only once
# -w means time-out, so if nothing is received within 5 seconds then it's a fail
if ! ping -c 1 -w 5 $line &/dev/null ;
then
echo "Failed to connect to $line"
else
echo "Connected to $line!"
username=user
passwd=password
port=23
cmd1=""
cmd2=""
cmd3=""
( echo open ${line} ${port}
sleep 2
echo ${username}
sleep 2
echo ${passwd}
sleep 2
echo ${cmd1}
sleep 2
echo ${cmd2}
sleep 3
echo exit ) | telnet
fi
done < ip.txt
</pre>
Anonymoushttp://www.blogger.com/profile/03631013823036663113noreply@blogger.com0tag:blogger.com,1999:blog-2749816916226454422.post-17734753748778648502014-08-10T05:09:00.000-07:002014-08-10T05:09:02.377-07:00Pack Up Tar Files ScriptThis script is used to pack up files into a "tar.gz", this can easily be change to a "tar.xz" by replacing all instances of "tar.gz". <br />
<a name='more'></a>This script just makes life a bit easier. The script uses parameters so the format would be:
<br />
<b> ./pack.sh afile.txt anotherFile.txt (and so on) </b>
<br />
<pre class="bash" name="code">#!/bin/bash
# if no parameters then show help
# $@ is the parameter
# -z checks is parameter is empty and if so then show help.
if [[ -z "$@" ]]; then
#echo " ${0##*/} <file dir=""> <*file/dir2> <*name>.tar.gz - archive file/folders"
# ${0##*/} show name of file
echo " ${0##*/} <file dir=""> <*file/dir2> - archive and compress files and dirs"
exit
fi
# choose a file name for your new tar.gz
#read the line, -p means prompt for a responce
while true; do
read -p " Enter archive filename: " filename
# -n means none emtpy string (checking that you wrote something)
if [ -n "$filename" ]; then
break
fi
done
# Check if selection(s) exists
# for loop looping through the two files and checking there ok
for a in "$@"; do
if [ ! -f "$a" ]; then
echo " Selection \""$@"\" does not exist."
exit
fi
done
# Create archive
tar -c --xz -f "$filename".tar.gz "$@" && \
#-c means create file.
#--xz means extract files and zip them up
#-f means file
echo " Created "$filename".tar.gz"
</file></file></pre>
Anonymoushttp://www.blogger.com/profile/03631013823036663113noreply@blogger.com0tag:blogger.com,1999:blog-2749816916226454422.post-56091349978775883152014-08-10T05:08:00.001-07:002014-08-10T05:08:55.142-07:00Watcher ScriptYou just need to set this script to run on startup and there you go, if anyone tries to connected to your pc via ssh,<br />
<a name='more'></a> telnet or ftp then you're going to know about it (I'm using xmessage here but you can set this up to send you an email or show a notification)
<br />
<pre class="bash" name="code">#!/bin/bash
tail -fn0 /var/log/auth.log | \
while read line ; do
echo "$line" | grep "Accepted"
if [ $? = 0 ]
then
xmessage "$line" | grep "Accepted"
fi
done
</pre>
Anonymoushttp://www.blogger.com/profile/03631013823036663113noreply@blogger.com0tag:blogger.com,1999:blog-2749816916226454422.post-23631992514868389342014-08-10T05:08:00.000-07:002014-08-10T05:08:20.055-07:00Chmod Examples<span style="color: #b45f06;">chmod [OPTION]... MODE[,MODE]... FILE...</span><br />
<br />
Chmod (Change Mode) is used to change the permissions of a file, this is needed if you want to create shell scripts or set the permissions of a file.<br />
<a name='more'></a><br />
The permissions are as follows:<br />
<br />
<span style="color: #b45f06;">400 read by owner</span><br />
<span style="color: #b45f06;">040 read by group</span><br />
<span style="color: #b45f06;">004 read by anybody (other)</span><br />
<span style="color: #b45f06;">200 write by owner</span><br />
<span style="color: #b45f06;">020 write by group</span><br />
<span style="color: #b45f06;">002 write by anybody</span><br />
<span style="color: #b45f06;">100 execute by owner</span><br />
<span style="color: #b45f06;">010 execute by group</span><br />
<span style="color: #b45f06;">001 execute by anybody</span><br />
<br />
now these values added up can set permissions for files, for example:<br />
<br />
<span style="color: #b45f06;">chmod 755 test.sh</span><br />
This command give<span style="color: #b45f06;"> test.sh</span> all rights, the reason for this is because <span style="color: #b45f06;">400+040+004+200+100+010+001 = 755.</span><br />
<br />
I hope this makes sense, there is a lot more to it then this but this should give you the basics.<br />
<div>
<br /></div>
Anonymoushttp://www.blogger.com/profile/03631013823036663113noreply@blogger.com0tag:blogger.com,1999:blog-2749816916226454422.post-67046996184816693162014-08-08T04:55:00.000-07:002014-08-10T05:09:51.706-07:00Problem 4<b>Problem 4</b><br />
<br />
Forth problem from <a href="https://projecteuler.net/">project Euler</a>. <br />
<a name='more'></a>I'm not sure I like my answer to this just yet, but it runs in under a second so it can't be that bad. Feedback on better ways to do these are most welcome!<br />
<br />
<b>Question:</b><br />
<br />
<i>A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.
<br />
Find the largest palindrome made from the product of two 3-digit numbers.</i><br />
<br />
Answer:<br />
<br />
<pre class="java" name="code">import java.util.SortedSet;
import java.util.TreeSet;
public class problem4 {
public static void main(String[] args) {
problem4 p = new problem4();
SortedSet set = new TreeSet();
for(int i = 100; i < 999; i++){
for(int j = 100; j < 999; j++){
if(p.isPalendrome(j * i)){
set.add(j * i);
}
}
}
System.out.println(set.last());
}
boolean isPalendrome(int num){
String number = String.valueOf(num);
StringBuilder sb = new StringBuilder();
for(int i = (number.length() - 1); i >= 0; i--){
sb.append(number.charAt(i));
}
if(sb.toString().equals(number)){
return true;
}
return false;
}
}
</pre>
Anonymoushttp://www.blogger.com/profile/03631013823036663113noreply@blogger.com0tag:blogger.com,1999:blog-2749816916226454422.post-327664412998378922014-08-08T02:19:00.001-07:002014-08-10T05:09:59.703-07:00Problem 3<b>Problem 3</b><br />
<br />
Third problem from <a href="https://projecteuler.net/">project Euler</a>.<br />
<a name='more'></a> Quite easy this one. Feedback on better ways to do these are most welcome!<br />
<br />
<b>Question:</b><br />
<br />
<i>The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ?.</i><br />
<br />
Answer:<br />
<br />
<pre class="java" name="code">public class problem3 {
private static final long largenumber = 600851475143L;
public static void main(String[] args) {
problem3 p = new problem3();
for(int i = 2; i < 10000; i++){
if(p.isPrime(i)){
if(largenumber % i == 0){
System.out.println(i);
}
}
}
}
boolean isPrime(int n) {
for(int i=2; 2*i< n; i++)
if(n%i==0)
return false;
}
return true;
}
}
</pre>
Anonymoushttp://www.blogger.com/profile/03631013823036663113noreply@blogger.com0tag:blogger.com,1999:blog-2749816916226454422.post-18032203080323717532014-08-08T02:15:00.001-07:002014-08-10T05:10:06.036-07:00Problem 2<b>Problem 2</b><br />
<br />
Second problem from <a href="https://projecteuler.net/">project Euler</a>.<br />
<a name='more'></a> Feedback on better ways to do these are most welcome!<br />
<br />
<b>Question:</b><br />
<br />
<i>Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:
<br />
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
<br />
By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.</i><br />
<br />
Answer:<br />
<br />
<pre class="java" name="code">public class problem2 {
public static void main(String[] args) {
problem2 p = new problem2();
int val = 0;
int sum = 0;
for (int i = 2; i < 102; i++) {
val = p.fibonacci(i);
if (val % 2 == 0) {
if (val <= 4000000) {
sum = sum + val;
} else {
break;
}
}
}
System.out.println(sum);
}
public int fibonacci(int n) {
if (n == 0)
return 0;
else if (n == 1)
return 1;
else
return fibonacci(n - 1) + fibonacci(n - 2);
}
}
}
</pre>
<br />
The good thing about <a href="https://projecteuler.net/">project Euler</a> is that each question relys on something learned in the previous. Also I would just like to point out that this is the first time in a long time that I have used recursion...dosn't seem to be used that much in the feild..Anonymoushttp://www.blogger.com/profile/03631013823036663113noreply@blogger.com0tag:blogger.com,1999:blog-2749816916226454422.post-24667330829432120802014-08-08T01:59:00.001-07:002014-08-10T05:10:13.708-07:00Problem 1<b>Problem 1</b><br />
<br />
First problem from <a href="https://projecteuler.net/">project Euler</a>.<br />
<a name='more'></a> I thought when I had some free time and nothing to write about I would see how many of these I could get though. I'm not going to write the actual answer down, but I'll just show the code which gave me the correct answer. Feedback on better ways to do these are most welcome!<br />
<br />
<b>Question:</b><br />
<br />
<i>If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.<br />
Find the sum of all the multiples of 3 or 5 below 1000.</i><br />
<br />
Answer:<br />
<br />
<pre class="java" name="code">public class problem1 {
public static void main(String[] args){
int sum = 0;
for(int i = 1; i < 1000; i++){
if(i % 3 == 0 || i % 5 == 0){
sum = sum + i;
}
}
System.out.println(sum);
}
</pre>
<div>
<br /></div>
Anonymoushttp://www.blogger.com/profile/03631013823036663113noreply@blogger.com0tag:blogger.com,1999:blog-2749816916226454422.post-25904876271396921092014-08-06T02:44:00.001-07:002014-08-10T05:10:24.848-07:00Factory PatternI'm going to go over the Factory design pattern in Java. The factory design pattern is probably the most commonly used design pattern,<br />
<a name='more'></a> it lets the user create objects without using the 'new' operator. This gives the program more flexibility in deciding which objects need to be created for a given use case.<br />
<br />
The design pattern comes under the category of Creational Patterns. A creational pattern is a pattern which creates objects while hiding the logic. The below image is a UML of what we are going to create.<br />
<br />
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhmbPOANijNhEXFfWkdH2ctp1EtFGxwhfztXVYJBZ2D0an8nCXcuuWxMGfwxP4BQbh9ZxWtTgv0nBi96YzD_uI5Vacdjy2Jtw6HpAqNEWgDkzdRnO8epnHv6ZysfRynHotH7AZJnY3MFbuP/s1600/factory_pattern_uml_diagram.jpg" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhmbPOANijNhEXFfWkdH2ctp1EtFGxwhfztXVYJBZ2D0an8nCXcuuWxMGfwxP4BQbh9ZxWtTgv0nBi96YzD_uI5Vacdjy2Jtw6HpAqNEWgDkzdRnO8epnHv6ZysfRynHotH7AZJnY3MFbuP/s1600/factory_pattern_uml_diagram.jpg" height="230" width="400" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Taken from http://www.tutorialspoint.com/</td></tr>
</tbody></table>
<br />
<br />
So I've taken this image from a tutorial website which is why it's using shapes as it's data. I'll do the same<br />
<br />
First off we are going to create our interface. Our interface will have a draw method, this tells all classes who implement this interface that they need a draw method.<br />
<br />
<pre class="java" name="code">public interface Shape {
void draw();
}
</pre>
<br />
Then we need to create three shape classes which will implement the Shape interface, and thus overriding the draw method with their own implementation.<br />
<br />
<pre class="java" name="code">public class Square implements Shape {
@Override
public void draw() {
System.out.println("Square");
}
}
</pre>
<br />
<pre class="java" name="code">public class Triangle implements Shape {
@Override
public void draw() {
System.out.println("Triangle");
}
}
</pre>
<br />
<pre class="java" name="code">public class Circle implements Shape {
@Override
public void draw() {
System.out.println("Circle");
}
}
</pre>
<br />
Now we need to create the ShapeFactory class. In this class we will create a method which returns a Shape object. As Triangle, Square and Circle all implement Shape they are now classed as Shape Objects. The method will have a parameter named 'type' which will decided which shape to create.<br />
<br />
<pre class="java" name="code">public class ShapeFactory {
public Shape getShape(String type){
if(type == "SQUARE")
return new Square();
if(type == "CIRCLE")
return new Circle();
if(type == "TRIANGLE")
return new Triangle();
return null;
}
}
</pre>
<br />
<br />
Now we need to a class to call this method. Lets create our FactoryPatternDemo class. Within this class we will have a main method which will call our ShapeFactory.getShape method and pass in a string value of the shape we wish to create. Calling the Shapes draw method will return the draw method of that Shape.<br />
<br />
<pre class="java" name="code">public class FactoryPattern {
public static void main(String[] args){
ShapeFactory factory = new ShapeFactory();
Shape square = factory.getShape("SQUARE");
square.draw();
Shape circle = factory.getShape("CIRCLE");
circle.draw();
Shape triangle = factory.getShape("TRIANGLE");
circle.draw();
}
}
</pre>
<br />
<br />
And that's pretty much it, when we run this program we should see and output like the following<br />
<br />
<pre class="java" name="code">Square
Circle
Triangle
</pre>
<br />
This proves that each of these objects have been created. Pretty cool!Anonymoushttp://www.blogger.com/profile/03631013823036663113noreply@blogger.com0tag:blogger.com,1999:blog-2749816916226454422.post-31853190667085264662014-08-05T08:46:00.000-07:002014-08-10T05:10:33.376-07:00Moto 360 made of Plastic?<br />
The Moto 360 STILL has not been released! But some images of the watch have been released <a href="http://evleaks.at/2014/08/02/motorola-moto-360-2014/">here</a><br />
<br />
<a name='more'></a><br /><br />
Come on guys.., leaking images isn't going to be enough, I might have to buy the *shudder* G watch...<br />
<br />
People have also been saying that the material of the watch is actually plastic?! Well reviews on YouTube state otherwise. According to the people at Google IO the version in the video (hardware wise) is the finished product.<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<iframe allowfullscreen='allowfullscreen' webkitallowfullscreen='webkitallowfullscreen' mozallowfullscreen='mozallowfullscreen' width='320' height='266' src='https://www.youtube.com/embed/U13DF7R-iuw?feature=player_embedded' frameborder='0'></iframe></div>
<br />
<div class="separator" style="clear: both; text-align: center;">
</div>
Anonymoushttp://www.blogger.com/profile/03631013823036663113noreply@blogger.com0tag:blogger.com,1999:blog-2749816916226454422.post-21976083163590294772014-08-04T04:09:00.001-07:002014-08-10T05:10:44.471-07:00Immutable and Mutable Objects<b><u>Immutable Objects</u></b><br />
<br />
Immutable objects are object whose data (state) cannot change after construction. For example the String object and the Integer object.<br />
<a name='more'></a><br />
<br />
<b>Why is String and Integer immutable?</b><br />
<br />
Each String and Integer object must be immutable. Consider a collection which contains the following Strings:<br />
<br />
<pre class="java" name="code">String A = "JAVA";
String B = "JAVA";
</pre>
<br />
If String was mutable and String B called<i> "JAVA".toLowerCase()</i>, String A's content would also changes to lower-case<br />
<br />
As a String is immutable "<i>A.toLowerCase();</i>" will only effect String A. this is what we would expect. The same goes for Integer.<br />
<br />
When an immutable object such as String or Integer is created its <a href="http://en.wikipedia.org/wiki/Java_hashCode()">hashcode</a> is cached. This is because the state of this object will never change, so we might as well store its <a href="http://en.wikipedia.org/wiki/Java_hashCode()">hashcode</a> in cache memory as we will not need to calculate it again, This makes it quicker to identify objects within a collection. A good benefit of immutable objects.<br />
<br />
<b><u>Mutable Objects</u></b><br />
<br />
Mutable objects are objects who data (state) CAN change. This is good as we need objects like this, but we must make sure things are consistent, for example, in terms of concurrency, if the structure of an immutable object is valid, it will always be valid. A mutable object wont always be valid as its state can change, this could cause huge problems.<br />
<br />
<a href="http://en.wikipedia.org/wiki/Joshua_Bloch">Joshua Bloch</a> (Author of Effective Java, READ IT) gives this advice:<br />
"<i>Classes should be immutable unless there's a very good reason to make them mutable....If a class cannot be made immutable, limit its mutability as much as possible.</i>"<br />
<br />
<br />
<b><u>Quick example of Immutable verses Mutable in efficiency</u></b><br />
<b><u><br /></u></b>
This is pseudo-code so it may not compile.<br />
<br />
The following code shows how not to use string concatenation in a loop:<br />
<br />
<pre class="java" name="code">public String concat(String[] stringArray) {
int count = 0;
String retVal = "";
for(String s : stringArray){
retVal = retVal + s + count;
count++
}
return retVal;
</pre>
}
<br />
<b><u>Explanation</u></b><br />
What is actually going on here is we are creating a new String object on each iteration of the loop. Because String is immutable it's state cannot be changed, therefour when we do "retVal = retVal + s + count" we are actually throwing away the old retVal String and creating a new one. As I'm sure you can see over a large loop this can be quite costly and is not efficient.<br />
<br />
The following code shows how you should accomplish concatenation in a loop:<br />
<br />
<pre class="java" name="code">public String concat(String[] stringArray) {
int count = 0;
StringBuilder sb = new StringBuilder();
for(String s : stringArray){
sb.append(s);
sb.append(count);
s = s + count;
count++
}
return sb.ToString();
</pre>
}<br />
<i><br /></i>
<b><u>Explanation</u></b><br />
Here we are using a StringBuilder, which happens to be a mutable object. This means we can change it's state without creating a new instance (nice and cheap :))<br />
<br />
<b><u>Conclusion</u></b><br />
In the first example we see that if the loop needs to iterate 20 times we will effectively create and throw away 19 String objects.<br />
In the second example if the loop needs to iterate 20 times we will only ever create 1 String object. A clear winner in efficiency.<br />
<i><br /></i>
<br />
<br />
<br />
<br />
<b><u><br /></u></b>
<b><u><br /></u></b>
<b><u><br /></u></b>
<br />
<br />
<br />
<br />Anonymoushttp://www.blogger.com/profile/03631013823036663113noreply@blogger.com0tag:blogger.com,1999:blog-2749816916226454422.post-78541749368101078172014-08-04T03:01:00.002-07:002014-08-04T03:01:14.648-07:00Learning PascalI've been trying to learn Pascal recently...<br />
<br />
There is is a good reason why! I know it's hard to believe. Basically Pascal is a procedural programming language which differs in many ways from object orientated programming. I program mainly in Java, I studied C# and university and I also know C++. I though it would be beneficial for me to understand more about programming by learning such a language as Pascal.<br />
<br />
There are very few tutorials when it comes to Pascal. I'm currently following this one which takes you through the basics and gives you a small assignment at the end. I've did the assignment a few weeks back but when I try again I realise I've forgotten the syntax....back to square one!<br />
<br />
More to follow on the pascal front!Anonymoushttp://www.blogger.com/profile/03631013823036663113noreply@blogger.com0tag:blogger.com,1999:blog-2749816916226454422.post-18324288466267002182014-08-04T02:55:00.000-07:002014-08-04T03:01:53.045-07:00Work In Progress...My boss has been away for a week now and he has given one (seemingly simple) task to do.<br />
Within the first couple of days I thought I had completed this task, then I noticed something I had missed which needed to be done but would have major repercussions on the project, essentially this meant a lot more work.<br />
<br />
My task is to copy the functionality of a feature we have on a web server, to an application. So the code is already there mainly it's just going to take some time to re-factor. It opens up a lot of questions though:<br />
<br />
<ul>
<li>Do I use the same methods but add a boolean parameter which will let the algorithm know if this is an application or not? Or do I overload each method for the application?</li>
<li>When is it OK/preferable to have duplicate code? I would have thought never...</li>
<li>Do I go with my gut and start making changes only to have my boss come back and say I should have done the opposite?</li>
</ul>
<div>
I decided (as any good programmer would do) to spend some time researching my questions before I started to code. I found <a href="http://www.javapractices.com/">this</a> great site which answered all my questions and also gave me answers to future questions. I suggest that every Java Developer has this site saved in their bookmarks. I'm currently just finishing off my work now so hopefully when my boss gets back all will be well.</div>
Anonymoushttp://www.blogger.com/profile/03631013823036663113noreply@blogger.com0tag:blogger.com,1999:blog-2749816916226454422.post-3978426304989675692014-01-29T08:47:00.000-08:002014-08-10T04:51:01.937-07:00What are Index Nodes?I'm writing about this because it came up as an issue while at work one day.<br />
<br />
<b>Background info!</b><br />
<i>Quite a while ago I wrote some software which keeps an eye on a number of servers and sends me an email if anything goes wrong, one of the things being if the servers hard disk becomes full / the system cannot create new files. </i><br />
<br />
So I'm sat doing my daily java programming and I receive and email saying our main server is full, pretty strange because each server should have at least 20GB of space free at all times. So I log on to the system to see whats taking up all this room, the first command I do is <span style="color: #b45f06;">df -h</span><span style="color: lime;"> </span>and I see the following output:<br />
<br />
<div class="separator" style="clear: both; text-align: left;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhgnboM6erWooyg69GY_UmZSu4xuGrUGSe9nVxzya3OrRePVg1z_k9FgZSWjkQHCDdWcmm2U949z0RCik6OV-QehkMBMAfmOvVJ0BAxukw-2GojG-s729P1U-NaO3iEgwY4wGvxWmwxs2EK/s1600/df.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhgnboM6erWooyg69GY_UmZSu4xuGrUGSe9nVxzya3OrRePVg1z_k9FgZSWjkQHCDdWcmm2U949z0RCik6OV-QehkMBMAfmOvVJ0BAxukw-2GojG-s729P1U-NaO3iEgwY4wGvxWmwxs2EK/s1600/df.png" /></a></div>
<br />
This is not the output I expected, so I thought maybe there was a problem writing files to the disk so I run a <span style="color: #b45f06;">touch text.txt</span> command and this seems to run perfectly fine. After looking thought my email software to see if it had somehow made a mistake I get another email saying the server is having problems writing logs, this is when I start getting confused.<br />
<br />
I log back onto the system and check the logs. The logs had indeed stopped, as a test I wrote a small bash script to create a new file, it didn't work, instead it came back saying the hard disk was full.<br />
<br />
After some extreme head scratching and chin stoking I found the source of my problem by running the command <span style="color: #b45f06;">df -i </span>and seeing that my IUse% was 100%.<br />
The problem was these strange little things called 'i-nodes'.<br />
<br />
<u><b>Index Nodes (i-nodes)</b></u><br />
An index node is a data-structure used to represent a file system object, this can be disk block location, creation time and a whole host of other metadata. Each index node is roughly 128 bytes in size and all index nodes should really only ever take up 1% of your hard disk. <br />
<br />
So for each file on your hard disk there is a i-node. So the solution presented itself, All I had to do was find the largest directory and backup and remove the files. To find the largest directory on the system I ran this command: <span style="color: #b45f06;">du -a | sort -n -r</span> and I instantly found the issue. Another developer had written a program which was producing a hell of a lot of log files, every 2 seconds a new log file (very small in size) was created. While this wasn't taking up much room on the hard drive it was taking up alot of index nodes.<br />
<br />
So there's a solution to anyone else having this issue. Anonymoushttp://www.blogger.com/profile/03631013823036663113noreply@blogger.com1tag:blogger.com,1999:blog-2749816916226454422.post-56992025336653690862013-11-15T07:01:00.000-08:002014-08-10T04:51:01.945-07:00What The Hell is A Shell?!I've had a lot of people ask this question before, myself included. I thought I would write a small straight to the point post about the differences between the most common. <br />
<br />
I'll start by explaining exactly what a shell is and what it is used for. (Although most of you will probably already know)<a href="https://www.blogger.com/null" name="more"></a><br />
<br />
<b>What the hell is a shell?!</b><br />
Before graphical user interfaces the command line was the main interface for users, just like now where we have KDE, Gnome etc... the command line had shells. Shells were used to give the user a slightly different environment in terms of functionality and scripting. In technical terms a shell is a command line interpreter developed to make things easier for users. One example of this would be filename/command completion (do this by pressing tab after the first part of the command/filename). These small features that a lot of people see as part of linux is actully part of the shell. <br />
<br />
<b>Popular shells</b><br />
The most popular shell (and the shell most of you will probably be using) is the Bash Shell written by a guy called <a href="http://en.wikipedia.org/wiki/Brian_Fox_(computer_programmer)">Brian Fox</a>. This is the most popular shell due to its robustness and tons of features. I use this shell at home all the time for scripting. <br />
<br />
The Bourne Shell, AKA 'sh'. This shell was developed by a guy called <a href="http://en.wikipedia.org/wiki/Stephen_R._Bourne">Stephen Bourne</a>, It quickly became the default shell for Unix due to its support for variables, piping, looping and a verity of other usefull stuff. I use this shell mainly when writing software for embedded systems as it is the default shell. <br />
<br />
The C-Shell AKA 'csh' was developed by a guy called <a href="http://en.wikipedia.org/wiki/Bill_Joy">Bill Joy</a>. This guy added useful things like aliases (short cuts for long commands) and command history. This shell was modeled on the C programming language, this was a good thing as Unix was written in C...although it did mean users of the bourne shell had to learn C in order to use the C-Shell. <br />
<br />
<b>Which is the best shell?</b><br />
There are a large number of shells to choose from, I've only named three here as these are the most popular. I would say the best shell is the Bash Shell due to how popular it is, this means most scripts written with this shell will most likely work on any machine. Anonymoushttp://www.blogger.com/profile/03631013823036663113noreply@blogger.com0tag:blogger.com,1999:blog-2749816916226454422.post-22274452576924418792012-08-03T08:24:00.000-07:002014-08-10T04:51:01.923-07:00Sed command examples<span style="font-family: Arial, Helvetica, sans-serif;">A command I use constantly at work is the '<span style="color: #b45f06;">sed</span>' command. This command is used when you want to make changes to a file automatically. Most people who use the '<span style="color: #b45f06;">sed</span>' command use it to replace text (which it does brilliantly) but there is so much more</span><br />
<span style="font-family: Arial, Helvetica, sans-serif;">I'm going to show you a few examples of the awesomeness of this command, if you have never used it before I guaranty you will use it after reading this post.</span><br />
<span style="font-family: Arial, Helvetica, sans-serif;">Lets say we have a file which contains the following text:</span><br />
<span style="font-family: Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Arial, Helvetica, sans-serif;">"<i>This is my favourite place to learn about Linux. Linux is an opensource OS. Linux has a variety of different flavours.</i>"</span><br />
<span style="font-family: Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Arial, Helvetica, sans-serif;"><b>Search and Replace</b></span><br />
<span style="font-family: Arial, Helvetica, sans-serif;">Using the '<span style="color: #b45f06;">sed</span>' command we can search and replace. for example if I wanted to replace the word 'Linux' with the word 'Unix' I would do the following:</span><br />
<span style="font-family: Arial, Helvetica, sans-serif;"><br /></span>
<span style="color: #b45f06; font-family: Arial, Helvetica, sans-serif;"> sed 's/Linux/Unix/' aFile.txt</span><br />
<span style="font-family: Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Arial, Helvetica, sans-serif;">This would change the first occurrence of "<span style="color: #b45f06;">Linux</span>" to "<span style="color: #b45f06;">Unix</span>" as shown below:</span>
<span style="font-family: Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Arial, Helvetica, sans-serif;">"<i>This is my favourite place to learn about Unix. Linux is an opensource OS. Linux has a variety of different flavours.</i>"</span><br />
<span style="font-family: Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Arial, Helvetica, sans-serif;">Replacing the nth occurrence of a word is just as easy:</span><br />
<span style="font-family: Arial, Helvetica, sans-serif;"><br /></span>
<span style="color: #b45f06; font-family: Arial, Helvetica, sans-serif;">sed 's/Linux/Unix/2' aFile.txt</span><br />
<span style="font-family: Arial, Helvetica, sans-serif;">This will skip the first occurrence of "<span style="color: #b45f06;">Linux</span>" and replace the second one with "<span style="color: #b45f06;">Unix</span>".</span><br />
<span style="font-family: Arial, Helvetica, sans-serif;">"<i>This is my favourite place to learn about Linux. Unix is an opensource OS. Linux has a variety of different flavours.</i>"</span><br />
<span style="font-family: Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Arial, Helvetica, sans-serif;">If we would like to replace all occurrences of a word then we use the "<span style="color: #b45f06;">/g</span>" (Global) option:</span><br />
<span style="font-family: Arial, Helvetica, sans-serif;"><br /></span>
<span style="color: #b45f06; font-family: Arial, Helvetica, sans-serif;">sed 's/Linux/Unix/g' aFile.txt</span><br />
<span style="font-family: Arial, Helvetica, sans-serif;">All occurrences of the word "<span style="color: #b45f06;">Linux</span>" have now been replaced with "<span style="color: #b45f06;">Unix</span>"</span><br />
<span style="font-family: Arial, Helvetica, sans-serif;">"<i>This is my favourite place to learn about Unix. Unix is an opensource OS. Unix has a variety of different flavours.</i>"</span><br />
<span style="font-family: Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Arial, Helvetica, sans-serif;">We can mess about with these options and merge them together to replace all occurrences of a word from a certain point, for example replacing all occurrences of the word "<span style="color: #b45f06;">Linux</span>" with the word "<span style="color: #b45f06;">Unix</span>" after the first occurrence (the example below will make this a bit clearer): </span><br />
<span style="font-family: Arial, Helvetica, sans-serif;"><br /></span>
<span style="color: #b45f06; font-family: Arial, Helvetica, sans-serif;">sed 's/Linux/Unix/2g' aFile.txt</span><br />
<span style="font-family: Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Arial, Helvetica, sans-serif;">So after the second occurrence the word "<span style="color: #b45f06;">Linux</span>" changes to "<span style="color: #b45f06;">Unix</span>".</span><br />
<span style="font-family: Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Arial, Helvetica, sans-serif;"><b>Add a line after or before a match</b></span><br />
<span style="font-family: Arial, Helvetica, sans-serif;">Here we can add a line after a match is found. For this we use the "<span style="color: #b45f06;">a</span>" command, for example:</span><br />
<span style="font-family: Arial, Helvetica, sans-serif;"><br /></span>
<span style="color: #b45f06; font-family: Arial, Helvetica, sans-serif;">sed '/flavours/ a "a new line!"' aFile.txt</span><br />
<span style="font-family: Arial, Helvetica, sans-serif;">and the result is as follows:</span><br />
<span style="font-family: Arial, Helvetica, sans-serif;">"<i>This is my favourite place to learn about Linux. Unix is an opensource OS. Linux has a variety of different flavours.</i></span><br />
<span style="font-family: Arial, Helvetica, sans-serif;"><i>a new line!</i>"</span><br />
<span style="font-family: Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Arial, Helvetica, sans-serif;">The same thing can work adding a line before a match, we do this using the "<span style="color: #b45f06;">i</span>" command.</span><br />
<br />
<span style="color: #b45f06; font-family: Arial, Helvetica, sans-serif;">sed '/flavours/ i "a new line!"' aFile.txt</span><br />
<span style="font-family: Arial, Helvetica, sans-serif;">and the result is as follows:</span><br />
<span style="font-family: Arial, Helvetica, sans-serif;">"<i>This is my favourite place to learn about Linux. Unix is an opensource OS. Linux has a </i></span><br />
<span style="font-family: Arial, Helvetica, sans-serif;"><i>a new line!</i></span><br />
<span style="font-family: Arial, Helvetica, sans-serif;"><i>variety of different flavours.</i>"</span><br />
<span style="font-family: Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Arial, Helvetica, sans-serif;"><b>Replace a line</b></span><br />
<span style="font-family: Arial, Helvetica, sans-serif;">We can search for a match and then replace that whole line using the '<span style="color: #b45f06;">c</span>' command. It pretty much the same format as before:</span><br />
<span style="font-family: Arial, Helvetica, sans-serif;"><br /></span>
<span style="color: #b45f06; font-family: Arial, Helvetica, sans-serif;">sed '/flavours/ c "a replaced line!"' aFile.txt</span><br />
<span style="font-family: Arial, Helvetica, sans-serif;">and the result is as follows:</span><br />
<span style="font-family: Arial, Helvetica, sans-serif;">"<i>This is my favourite place to learn about Linux. Unix is an opensource OS. Linux has a </i></span><br />
<span style="font-family: Arial, Helvetica, sans-serif;"><i>a replaced line!</i>"</span><br />
<span style="font-family: Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Arial, Helvetica, sans-serif;">The last thing I'm going to show you is the transform option. This is used to transform lower case letters to upper case letters. We use the '<span style="color: #b45f06;">y</span>' command for this. In this example I'm going to change all the small l's and v's to large l's and v's, and vice versa:</span><br />
<span style="font-family: Arial, Helvetica, sans-serif;"><br /></span>
<span style="color: #b45f06; font-family: Arial, Helvetica, sans-serif;">sed 'y/lv/LV/ aFile.txt</span><br />
<span style="font-family: Arial, Helvetica, sans-serif;">"<i>This is my faVourite pLace to Learn about linux. Unix is an opensource OS. linux has a Variety of different fLaVours.</i>"</span><br />
<span style="font-family: Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Arial, Helvetica, sans-serif;"> I hope this helps you guy's with the '<span style="color: #b45f06;">sed</span>' command, it can be very useful once you get to know it. Any questions are welcome.</span><br />
<br />Anonymoushttp://www.blogger.com/profile/03631013823036663113noreply@blogger.com3tag:blogger.com,1999:blog-2749816916226454422.post-75500546120774079322012-06-20T01:12:00.000-07:002014-08-10T04:51:01.947-07:00How To Install Software On Linux (It's not that hard!)<span style="font-family: Arial, Helvetica, sans-serif;">OK, so I have had enough with people asking me how to install stuff on Linux, it's really not that difficult but it does seem to put a lot of people off the OS. I'm going to go through a few things here such as rpm, tar.gz and tar.bz2.</span><br />
<span style="font-family: Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Arial, Helvetica, sans-serif;"><b>RPM</b></span><br />
<span style="font-family: Arial, Helvetica, sans-serif;">RPM stands for "<span style="color: #b45f06;">Red hat Package Manager</span>". Files with this extension are installed and managed with the rpm program which I will take you through now.</span><br />
<span style="font-family: Arial, Helvetica, sans-serif;">The rpm program not only installs the software but it checks your system has all the prerequisites. For example if you are trying to install a package called "LinuxWow" but that package requires "LinuxFTW" that you haven't installed rpm will not install "LinuxWow". However if you have "LinuxFTW" then the package will be install as it should. This can save a lot of frustration... believe me. </span><br />
<span style="font-family: Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Arial, Helvetica, sans-serif;">before installing an rpm file it might be a good idea to check if you already have this installed, to do this run the following command:</span><br />
<span style="color: #b45f06; font-family: Arial, Helvetica, sans-serif;">rpm -q package_name</span><br />
<span style="font-family: Arial, Helvetica, sans-serif;"><span style="color: #b45f06;">-q </span>= query</span><br />
<span style="font-family: Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Arial, Helvetica, sans-serif;">The commands you would use to install, update or remove a .rpm file are as follows:</span><br />
<span style="font-family: Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Arial, Helvetica, sans-serif;"><b>Install one or multiple rpm files</b></span><br />
<span style="color: #b45f06; font-family: Arial, Helvetica, sans-serif;">rmp -ivh packages1.rpm packages2.rpm</span><br />
<span style="font-family: Arial, Helvetica, sans-serif;"><span style="color: #b45f06;">-i </span>= install</span><br />
<span style="font-family: Arial, Helvetica, sans-serif;"><span style="color: #b45f06;">-v</span> = verbose information (additional info)</span><br />
<span style="font-family: Arial, Helvetica, sans-serif;"><span style="color: #b45f06;">-h</span> = Print 50 hash marks as the package archive is unpacked</span><br />
<span style="font-family: Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Arial, Helvetica, sans-serif;"><b>Update one or multiple rpm files</b></span><br />
<span style="color: #b45f06; font-family: Arial, Helvetica, sans-serif;">rpm -Fvh packages1.rpm packages2.rpm</span><br />
<span style="font-family: Arial, Helvetica, sans-serif;"><span style="color: #b45f06;">-F</span> = Freshen</span><br />
<span style="font-family: Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Arial, Helvetica, sans-serif;"><b>Remove one or multiple rpm files</b></span><br />
<span style="color: #b45f06; font-family: Arial, Helvetica, sans-serif;">rmp -e packages1.rpm packages2.rpm</span><br />
<span style="font-family: Arial, Helvetica, sans-serif;"><span style="color: #b45f06;">-e</span> = erase.</span><br />
<span style="font-family: Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Arial, Helvetica, sans-serif;"><b>TAR.GZ & TAR.BZ2</b></span><br />
<span style="font-family: Arial, Helvetica, sans-serif;">tar files usually contain source code so the idea is that you compile (build) them yourself rather then relying on a program like rpm. </span><br />
<span style="font-family: Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Arial, Helvetica, sans-serif;">I'll show you how to install these files in 3 steps:</span><br />
<span style="font-family: Arial, Helvetica, sans-serif;">1. First this is to list the packages in the file, this is to make sure we don't over right anything precious on our system. We will use the '<span style="color: #b45f06;">less</span>' command for this. The '<span style="color: #b45f06;">|</span>' can be used as an extension of a command, so we could do '<span style="color: #b45f06;"> | grep "fileName"</span>' if we wanted to grep the package for a certain file name.</span><br />
<span style="color: #b45f06; font-family: Arial, Helvetica, sans-serif;">tar tvzf package.tar.gz | less</span><br />
<span style="font-family: Arial, Helvetica, sans-serif;">or</span><br />
<span style="color: #b45f06; font-family: Arial, Helvetica, sans-serif;">tar tvzf package.tar.bz2 | less</span><br />
<span style="font-family: Arial, Helvetica, sans-serif;"><span style="color: #b45f06;">-t </span>= list, this is to list the contents of the package</span><br />
<span style="font-family: Arial, Helvetica, sans-serif;"><span style="color: #b45f06;">-v</span> = verbose, you know this by now</span><br />
<span style="font-family: Arial, Helvetica, sans-serif;"><span style="color: #b45f06;">-z </span>= this filters the package through gzip, like win-zip if you're from windows</span><br />
<span style="font-family: Arial, Helvetica, sans-serif;"><span style="color: #b45f06;">-f</span> = uses the package file</span><br />
<span style="font-family: Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Arial, Helvetica, sans-serif;">2. If we are happy with everything then we extract the files to a new directory, as shown below:</span><br />
<span style="font-family: Arial, Helvetica, sans-serif;">we make a new directory called newdir:</span><br />
<span style="color: #b45f06; font-family: Arial, Helvetica, sans-serif;">mkdir newdir</span><br />
<span style="font-family: Arial, Helvetica, sans-serif;">Enter the directory:</span><br />
<span style="color: #b45f06; font-family: Arial, Helvetica, sans-serif;">cd newdir</span><br />
<span style="font-family: Arial, Helvetica, sans-serif;">and extract the files:</span><br />
<span style="color: #b45f06; font-family: Arial, Helvetica, sans-serif;">tar xvzf <path>/package.tar.gz</span><br />
<span style="font-family: Arial, Helvetica, sans-serif;">or</span><br />
<span style="color: #b45f06; font-family: Arial, Helvetica, sans-serif;">tar xvzf <path>/package.tar.bz2</span><br />
<span style="font-family: Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Arial, Helvetica, sans-serif;">3. once extracted look for a file called '<span style="color: #b45f06;">install</span>' or '<span style="color: #b45f06;">readme</span>' these will give you the rest of the information you need. They usually tell you to run a script called 'configure', after that we run the '<span style="color: #b45f06;">make</span>', (this will ask us to enter our root password) command and then finally the '<span style="color: #b45f06;">make install</span>'.</span><br />
<span style="color: #b45f06; font-family: Arial, Helvetica, sans-serif;">./configure</span><br />
<span style="color: #b45f06; font-family: Arial, Helvetica, sans-serif;">make</span><br />
<span style="color: #b45f06; font-family: Arial, Helvetica, sans-serif;">su</span><br />
<span style="color: #b45f06; font-family: Arial, Helvetica, sans-serif;">(enter password)</span><br />
<span style="color: #b45f06; font-family: Arial, Helvetica, sans-serif;">make install</span><br />
<span style="font-family: Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Arial, Helvetica, sans-serif;">The '<span style="color: #b45f06;">readme</span>' file may contain some other information about the configure script which may require you adding some options but the file should give you all the information you need.</span><br />
<span style="font-family: Arial, Helvetica, sans-serif;">It's really not that difficult to install software on a Linux machine. If you use Ubuntu then a simple "<span style="color: #b45f06;">apt-get install package</span>" would suffice. I hope this post makes people realise how easy it is.</span>Anonymoushttp://www.blogger.com/profile/03631013823036663113noreply@blogger.com2