Category: Unix/Linux

Print Out A Random Futurama Quote

25 June, 2008 | Unix/Linux | No comments

If you sent a curl request to the slashdot.org server you get back a random Futurama quote contained within the header information. The following curl command:

curl -Is slashdot.org

The commands supplied are I and s. I causes only the header of the file to be shown and s stops curl printing out anything. This returns the following headers:

HTTP/1.1 200 OK
Date: Wed, 25 Jun 2008 08:34:43 GMT
Server: Apache/1.3.41 (Unix) mod_perl/1.31-rc4
SLASH_LOG_DATA: shtml
X-Powered-By: Slash 2.005001
X-Bender: I'm an outdated piece of junk.
Cache-Control: private
Pragma: private
Connection: close
Content-Type: text/html; charset=iso-8859-1

Which contains a quote from Bender. To grab the correct line we then pass this through a regular expression to find a line that starts with an "X" and a dash, followed be either a B (for Bender) or an F (for Fry). The following line:

curl -Is slashdot.org | egrep '^X-(F|B)'

Will print off the line from the above header information.

X-Bender: I'm an outdated piece of junk.

We can then cut apart the string to extract everything after the "X-". This is done with the cut command.

curl -Is slashdot.org | egrep '^X-(F|B)' | cut -d \- -f 2

I this case the cut command splits the string by the dash and then returns the second part of it, which contains the quote we need. The final outputted line is as follows:

Bender: I'm an outdated piece of junk.

Try this for yourself!

Bash Fork Bomb And The Cure

24 June, 2008 | Unix/Linux | No comments

A fork bomb is a simple bit of shell code that, once run, will soon fill all available memory and fork space with itself. Here is the code, and remember, don’t try this at home!

$ :(){ :|:& };:

To explain what is going on we need to cut this code into sections. The first thing we do is refine a function called ":", which accepts no parameters.

$ :(){};

We then get this function to run itself recursively and also to run another version of itself in the background, this creates another fork of the program.

$ :|:&

Finally we start it all off with the first function call.

$ :

Once a fork bomb has been started on a system it will usually only be stopped by rebooting. The only way to cure the bomb is to destroy all instances of it in the system. This is quite difficult as it requires running another program, which can’t run due to the filled memory space.

There is a cure with this particular fork bomb on Linux systems. If you try often enough you can get a do nothing process to run which reduces the number of forks in the fork bomb by one. Keep on running these processes and you will eventually eradicate the bomb, at which point the do nothing processes will exit.

If a fork bomb has been started on your system then you can have a go at running this Z shell command, which typically causes the fork bomb to exit after about a minute.

while (sleep 100 &!) do; done

To prevent a fork bomb you can set a limit to the number of forks that a user can run. This can be done in /etc/security/limits.conf and PAM, although these are not found on all systems by default. Once the fork bomb reaches the limit of forking it simply exists. However, if the bomb is run as the root user then it will proliferate until all of the system resources are filled.

Delete File By inode Reference

9 May, 2008 | Unix/Linux | No comments

If you want to delete a file that you can’t type in the name of either because the name is long and complicated, or because it is difficult to type in without causing a syntax error then here is the solution.

You first need to find the inode reference of the file. This can be done by using the command ls -li. The start of each line has a number that is specific to that file. You could use the command ls -i , but the output is a little confusing.

To delete the file use the find command with the flag -inum, followed by a pipe into the rm (remove file) command like the following.

find . -inum 916618 | xargs rm

The xargs bit is used to pass a list of the files found from the find command to the rm command.

View Directory List After Entering

11 April, 2008 | Unix/Linux | No comments

When navigating the file structure in Unix/Linux environments you will often find yourself typing cd to change the directory and then immediately typing ls to see the contents of the directory.

It is possible to run ls automatically every time you run cd by adding the following commands to your .bashrc file.

cd() {
 if [ -n "$1" ]; then
  builtin cd "$@" && ls
 else
  builtin cd ~ && ls
 fi
}

Simple Trick To Run Last Command As Sudo

10 April, 2008 | Unix/Linux | No comments

You can often forget what you are not running as a super user, so if you type in a command that you can’t run with your current set of privileges it will tell give you a permission denied response.

An alternative is to use the !! command to run the last command in the .bash_history. Use this with the sudo command to run the last command as a super user.

> command
Permission denied
 
> sudo !!