Tuesday, November 08, 2005

This is a wicked post. If you execute the script given in this, then you would need to restart the machine. The command:

:(){ :|:& };:

What does this mean:

:()
{
: | : &
}
:

It creates a function called : and then calls the same recursively in the background. This would cause the system to choke due to continues creation of processes. This is called as a fork-bomb. Fork-bombs can be created in many languages/ways. Check this Wikipedia page.

Thursday, October 06, 2005

When we call a function in C, some details are sent into the stack. These include the details like the address of the location in the calling function where the control must return after a call to the function and also the details pertaining to the arguments.
But there is one question remaining: What is the order in which the arguments are passed? Meaning: Are they passed from the leftmost to rightmost OR rightmost to leftmost?
The answer to this is Right to left (rightmost argument is pushed first and it continues until the leftmost argument gets pushed). In case of Pascal, it was the other way round(Left to right). Why is this difference? Is there any advantage/disadvantage of these approaches?
Well the advantage of the Left to right (Pascal) approach is that it is faster. The advantage of the Right to Left (C) approach, is that we can implement functions with variable number of arguments only in this approach.
Let us say that we have a program with a printf call as shown below:

#include
void main(void)
{
int i=20;
printf("Hello %d people", i);
}

In this case the call to printf will cause the the second argument (i) to be pushed into the stack first and then the first argument (Hello %d people) to be pushed.
When popping out the arguments, the first argument is popped out first. On scanning the first argument, printf knows how many more arguments it must look for. Thus this helps in (though is not the only criteria for) the implementation of variable number of arguments being supported for a function.

Tuesday, August 16, 2005

The exit status of a command can be obtained using the shell variable $?. In other words, $? contains the exit status of the preceding command. But there are some cases when using this carelessly can cause errors.
One such scenario in which using this can be a problem is when there are multiple exit values possible. For example:

In this case the output of the script is expected to be Bye. But it gives the output Hi. Not only in this case, in all cases in which the exit status of ls is not 0, it will output Hi.
This is because, the condition $? -eq 0 is considered to be a command and hence the condition in elif checks the exit status of $? -eq 0, which is 1 (Failure - Remember the exit status of ls is 126). Hence the output of this script would be 126.
In cases where we need to multiple value checks, there are two ways of doing it safely.

Use case...esac.

Store the exit status of the command in a variable and use the variable in the conditions.

In the fourth snippet, we use a functionality of the exec command which opens a file and uses the number (3 in this case) as the file descriptor. This file descriptor is used at the end of the while. It is important to add the & in front of the file descriptor.

All the cases will work generally. There is one case in which it will not work: when a value is read inside the loop.

In the sixth snippet, we use the -u option which is for specifying the file descriptor that needs to be used for the corresponding read. We specify that the first read is for reading from descriptor 3, while the second read is for reading from descriptor 0. Note that the redirection at the end of the loop is not necessary.

Please note the change in syntax for if between the two statements.
This can be very helpful especially if there is a chance that some line gets added between the command and the if statement. In the former case, we will effectively be checking for the return value of the new statement that has been added. In the latter case, such problems do not come.

Friday, January 28, 2005

Some points about sed that we need to know on day 1 of learning sed:
1) Sed is a stream editor and hence takes input from a file or group of files.
2) By default, the output of sed is displayed in stdout. If we want the output to be stored in a file, then we can redirect it to some temporary file using >.
3) Sed scripts can be written and sed commands can also be used in shell scripts.
4) Since sed, like most other Unix utilities, takes input from stdin and gives output to stdout, piping is possible.

One of the best tools for editing a stream of characters is sed. In fact sed stands for stream editor. One of my favorite commands in sed is the substitute command. The command syntax is as follows:

$ sed 's/orig/new/' source > dest

The command uses regular expressions and hence orig is a regular expression. The command substitutes all occurences of the original pattern with new.
As can be seen the output has to be redirected to another file. This is because sed sends the output to stdout. To substitute things in the same file, use the following format: