Thursday, January 26, 2012

What are the various ways to run a shell script?

There are 4 ways of executing a Shell Script in UNIX / Linux.
1. Execute Shell Script Using File Name
Use the shell script file name to execute it either by using it’s relative path or absolute path as shown below.
$ cd /home/sathiya
$ ./scriptfile

(or)

$ /home/sathiya/scriptfile
If you have the shebang, then it will be executed using the command interpreter specified in the shebang.

2. Execute Shell SCript by Specifying the Interpreter

You can also execute a unix shell script by specifying the interpreter in the command line as shown below.

Execute using sh interpreter

$ sh scriptfile

Execute using bash interpreter

$ bash scriptfile
Irrespective of what is being used as shebang, the interpreter which you have specified will be used for execution. You can use any interpreter (sh, ksh, bash, csh etc.,).

3. Execute Shell Script Using .  ./ (dot space dot slash)

While executing the shell script using “dot space dot slash”, as shown below, it will execute the script in the current shell without forking a sub shell.
$ . ./scriptfile
In other words, this executes the commands specified in the scriptfile in the current shell, and prepares the environment for you.

“dot space dot slash” Usage Example:

Typically we use this method, anytime we change something in the .bashrc or .bash_profile. i.e After changing the .bashrc or .bash_profile we can either logout and login for the changes to take place (or) use “dot space dot slash” to execute .bashrc or .bash_profile for the changes to take effect without logout and login.
$ cd ~
$ . ./.bashrc
$ . ./.bash_profile

4. Execute Shell Script Using Source Command

The builtin source command is synonym for the . (dot) explained above. If you are not comfortable with the “dot space dot slash” method, then you can use source command as shown below, as both are same.
$ source ~/.bashrc

Tuesday, January 24, 2012

Tuesday, January 3, 2012

What’s the difference between accessing an array element with $items[$index] and@items[$index]?

Using a $ retrieves the identified element, which is a scalar. @ takes an array slice from $index to the end. While both these will get your value, the difference can be important, for example:

$arr[0] = ; # reads the first line, as intended.
@arr[0] = ; # reads the whole file, "disappearing" everything after the first line.

The second is a single item array slice and is deprecated.

How do you catch an exception in Perl 5?

1. eval (it isn’t pretty).
2. use Try::Tiny (maybe the right choice at this point).

How do you throw an exception in Perl 5?

1. die (it isn’t pretty).
2.  croak (good).
3. confess (if you please).

What do variable sigils indicate in Perl 5?

They indicate how the variable is being used: $ is for a scalar (reference, filehandle, string, number); @ is an array; % is a hash (an even-sized list); & is a subroutine; and * is a typeglob.

How do you look up error messages in the Perl 5 documentation?

perldoc perldiag has the Perl diagnostic messages, and 'use diagnostics' will give you the explanations as they’re encountered.

When does Perl 5 reclaim the memory used by a variable?

Perl’s garbage collector uses reference-counting. An internal count of references to any data structure is kept; when the count reaches zero, the memory is reclaimed. Perl does not use mark-and-sweep like Java, so circular memory structures cannot be reclaimed without programmer assistance (ie. a DESTROY method), except at the end of execution.