Tuesday, November 12, 2013

How to use SUID for shell scripts in Linux?

Question : How to write a shell script which will read the required passwords/connect strings from a config file? Other users should be able to execute the scripts, however they should not be able to read the config file.

Say, I have a config file, myconfig.txt, which contains credentials for an oracle connection:
$ cat myconfig.txt

Tuesday, August 13, 2013

Python - How to write a list to a file?

How to write an entire list of elements into a file? Let us see in this article how to write a list l1 to a file 'f1.txt':

1. Using write method:


for ele in l1:

 The list is iterated, and during every iteration, the write method writes a line to a file along with the newline character.

Tuesday, July 30, 2013

Python - How to find the location of a module?

When a module is imported into a program, how to find out the path from where the module is getting picked?

    In the Unix world, when a command say ls is executed, the operating system searches in a list of directories for the command executable and once found, executes the command. The list of directories is maintained in the PATH variable.
    Similarly, in Python, whenever a module is imported, say "import collections', python also searches in a list of directories to find the module file "collections.py", and once found, includes the module. The list of directories to be searched is maintained in a list, sys.path, from the sys module. Hence, this can be accessed using the sys.path.

Friday, July 19, 2013

Perl : Foreach loop variable is an alias

 In Perl, when an array is traversed using the foreach loop, the variable holding the element of the array during the iteration is actually an alias to the actual element itself. Due to this, any change done to this variable automatically updates the array itself.
   Let us see some examples where every element of an array has to be added by 10:

1. Array elements get updated when updating $_:
use warnings ;
use strict ;

my @arr=(5..8);
print "@arr";
foreach (@arr){
print "@arr";
 This snippet when run outputs:
5 6 7 8
15 16 17 18
  As seen above, just by updating the $_, the array elements get updated automatically.

Wednesday, July 17, 2013

Linux For You - A magazine for Open Source techies

    Linux For You(LFY) or the OpenSource For You is the magazine which you should be having if you are a techy, especially for the ones working in Unix or Linux flavors. I accidentally got my hands on the magazine once, and made it a point to subscribe to it immediately.

  My 5 reasons why it is a must in your shelf:

1. Knowledge on Linux World: Linux is one such technology which has no limits. You keep learning, Linux keeps providing something new for you to satisfy your hunger. However, its not possible to learn everything by ourself through our experiments. It takes lot of time to learn anything by oneself. This magazine  is a place wherein you get lot of knowledge, good knowledge, in a short span of time. The topics covered can be of any programming language, scripting language, system administration stuff, apps programming stuff, review of any particular software, etc. Last but not the least, the articles always come with lots of explanation.

Tuesday, May 14, 2013

Perl : Remove duplicate elements from arrays

How to remove duplicate element from arrays in Perl? Let us see in this article how can duplicates be removed in different ways?

1. Copying distinct elements to new array using grep function:
my @arr=qw(bob alice alice chris bob);
my @arr1;
foreach my $x (@arr){
        push @arr1, $x if !grep{$_ eq $x}@arr1;
print "@arr1";
   A loop is run on the array elements. For every element, it is checked to find out whether the element is already present in the new array. If present, it is skipped, else added to the new array.

Wednesday, April 10, 2013

Perl - 6 examples to fetch date / time without Linux date command

How to fetch date and time related stuff within in Perl?
   Sometimes, Perl developers resort to using of Unix system date command to fetch a date or time within Perl script. This is not needed at all since Perl itself has a set of built-ins using which dates and epoch can be retrieved easily without using the system date command. In this article, let us see how to simulate the date related commands in Perl.

1. Getting the date command output:
Linux command:
$ date
To get the date command like output in Perl:

Tuesday, March 26, 2013

Perl - How to find the sum of digits in a number?

How to find the sum of all the digits in a number or a string? OR How to find the sum of all elements in array? Let us see the different ways how it can be done in Perl?

1. Divide and accumulate:
my $x=3456;
my $sum=0;
while ($x) {
print $sum;
  The digits are extracted by taking the modulus of the number with 10, and then dividing the number by 10. This is repeated till the original number becomes zero at the end of which the sum is retrieved.

Thursday, March 14, 2013

How to remove leading zeros in a string in Linux?

How to remove leading zeros in a variable or a string?

Let us consider a variable "x" which has the below value :
$ x=0010
$ echo $x
1. Using typeset command of ksh:
$ typeset -LZ x
$ x=0010
$ echo $x
   The variable x is declared using the typeset command. The -Z option of typeset will strip the leading zeros present when used along with -L option.

Tuesday, February 26, 2013

Perl - What is __DATA__ ?

What is __DATA__ ? Why is it used?
    Generally, Perl reads data from a file which in the Perl program can be accessed using open and readline functions. However, for users who just want to test something for an urgent requirement, it is pretty boring to put the test data in a file and access it using the open and readline functions. The solution for this : __DATA__.

   Let us consider a sample text file:
$ cat f1.txt

Tuesday, February 19, 2013

What is the time command in Linux for?

time command in Unix is used to find the time taken by a particular command to execute. This is very useful when we want to time a particular command or a particular script to know the amount of time it consumes. Not only the elapsed time, the time command also tells the amount of time spent in the user and kernel spaces as well.
$ time

real    0m0.000s
user    0m0.000s
sys     0m0.000s
time command output shows 3 different components:
real - The total time elapsed during the command which includes the time taken for the process.
user - The time taken for the process only in the user space. This does not include kernel calls.
sys - The time taken for the process in system space (for kernel related calls )

Tuesday, February 12, 2013

sed - 10 examples to replace / delete / print lines of CSV file

How to use sed to work with a CSV file? Or How to work with any file in which fields are separated by a delimiter?

Let us consider a sample CSV file with the following content:
cat file

1. To remove the 1st field or column :
$ sed 's/[^,]*,//' file
   This regular expression searches for a sequence of non-comma([^,]*) characters and deletes them which results in the 1st field getting removed.

Wednesday, February 6, 2013

How to delete every nth line in a file in Linux?

How to delete or remove every nth line in a file? The requirement is to remove every 3rd line in the file.

Let us consider a file with the below content.
$ cat file

1. awk solution :
$ awk 'NR%3' file
   NR%3 will be true for any line number which is not multiple of 3, and hence the line numbers which are multiple's of 3 does not get printed.
Note: NR%3 is same as NR%3!=0

Tuesday, February 5, 2013

Perl - How to split a string into words?

How to split a string into individual words? Let us see in this article how to split a string which has 2 words separated by spaces.

1. Using the split inbuilt function:
my $str="hi  hello";
my ($var1,$var2)=split /\s+/,$str;
   \s+ stands for one or more spaces. By using \s+ as the expression for the split function, it splits everytime it encouters a series of spaces and hence the words are retrieved.

   In case multiple words are present in a string, the result can be collected in an array:
my $str="hi  hello";
my @arr=split /\s+/,$str;

Wednesday, January 23, 2013

gawk - Calculate date / time difference between timestamps

How to find the time difference between timestamps using gawk?
  Let us consider a file where the
        1st column is the Process name,
        2nd is the start time of the process, and
        3rd column is the end time of the process.

The requirement is to find the time consumed by the process which is the difference between the start and the end times.

1. File in which the date and time component are separated by a space:
$ cat file
P1,2012 12 4 21 36 48,2012 12 4 22 26 53
P2,2012 12 4 20 36 48,2012 12 4 21 21 23
P3,2012 12 4 18 36 48,2012 12 4 20 12 35
Time difference in seconds:
$ awk -F, '{d2=mktime($3);d1=mktime($2);print $1","d2-d1,"secs";}' file
P1,3005 secs
P2,2675 secs
P3,5747 secs
  Using mktime function, the Unix time is calculated for the date time strings, and their difference gives us the time elapsed in seconds.

Monday, January 21, 2013

gawk - Date and time calculation functions

gawk has 3 functions to calculate date and time:
  • systime
  • strftime
  • mktime
   Let us see in this article how to use these functions:

   This function is equivalent to the Unix date (date +%s) command. It gives the Unix time, total number of seconds elapsed since the epoch(01-01-1970 00:00:00).
$ echo | awk '{print systime();}'
Note: systime function does not take any arguments.

Thursday, January 17, 2013

What is UNIX time?

1. What is Unix Time?
  Unix time is total number of seconds measured since  01-Jan-1970 00:00:00.  This time format is used in all the Unix flavors for time computing activities such as dates associated with files/inodes, etc. This date , 1-Jan-1970 00:00:00 is also called epoch date.

2. How to find the Unix time ?
    The date command with %s option :
$ date '+%s'
  This number is the total number of seconds elapsed since 01-Jan-1970 till now.

Tuesday, January 15, 2013

Perl - Split a string into individual character array

How to split a string into individual characters and store in an array in Perl? Let us see in this article how to split a string 'welcome' into separate characters.

1. Using the split function:
my $x='welcome';
my @arr=split (//, $x);
print "@arr";
    The split function is used to split the string $x by giving the delimiter as nothing.  Since the delimiter is empty, split splits at every character, and hence all the individual characters are retrieved in the array.
  On running the above snippet, the result produced will be:
w e l c o m e

Wednesday, January 2, 2013

Perl - How to print a hash?

How to print the contents of a hash? Let us see in this article the different options to print a hash.

1. Using foreach and keys:

use warnings;
use strict;


my %h1=(Jan => 31, Feb => 28, Mar => 31);

foreach my $key (keys%h1) {
        print "$key => $h1{$key} ";
   Using the keys function, the keys are generated and processed in a loop. In every iteration, the key and its corresponding value is printed using the $key variable which holds the key.