Neo

This user hasn't shared any biographical information


Posts by Neo

How to find size of code and data segments of a program

The Linux provides a command “size” which can shows the sizes of various segments of an executable program.

The following example shows how it can be used:

[neo@techpulp ~]# size /usr/lib/libopensync.so.1.0.0
text       data        bss        dec        hex    filename
387800       3516         28     391344      5f8b0    /usr/lib/libopensync.so.1.0.0
[neo@techpulp ~]#

As you can see the “size” command displays size of “text”, “data” and “bss” segments. The above example used a shared library. However the same can be run against a program as well.

[neo@techpulp ~]# size /bin/bash
text       data        bss        dec        hex    filename
729668      19416   More >

How to catch signals in a bash script

POSIX compatible UNIX systems like Linux supports standard signals as part of inter process communication. While some signals like SIGINT can be ignored, SIGKILL and SIGSTOP cannot be caught, blocked or ignored.

Bash provides a command “trap” to assign a signal handler. The following example script catches SIGINT signal that is generally received when user presses Ctrl+C key combination. A bash script will generally terminate on pressing Ctrl+C but it can chose to ignore the signal or do clean up before terminating using signal handler.

#!/bin/bash

trap mysighandler INT

mysighandler()
{
    echo 'You have pressed Ctrl+C.. Ignoring...'
}

for ((i=0;i<10;i++))
do
   echo More >

How to print all lines of a data file in reverse order

The command “tac” (reverse of cat command) which prints contents of a file in reverse order in terms of lines. It can also concatenate multiple files while printing in reverse order.

Sample data file:

[neo@techpulp ~]# cat data.txt
line1: Mango
line2: Apple
line3: Banana
line4: Papaya
[neo@techpulp ~]#

Output of “tac” command on the same data file:

[neo@techpulp ~]# tac data.txt
line4: Papaya
line3: Banana
line2: Apple
line1: Mango
[neo@techpulp ~]#

How to find MAC address of a linux network interface using SIOCGIFHWADDR ioctl

The following C programming example retrieves all network interfaces in a Linux system and their IP addresses and MAC addresses. The name of network interface and IP address are retrieved using SIOCGIFCONF ioctl while the hardware address of each individual interface is found using SIOCGIFHWADDR ioctl.

/* iflist.c */
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <net/if.h>
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>

int get_iface_list(struct ifconf *ifconf)
{
  int sock, rval;

  sock = socket(AF_INET,SOCK_DGRAM,0);
  if(sock < 0)
  {
    perror("socket");
    return (-1);
  }

  if((rval = ioctl(sock, SIOCGIFCONF , (char*) ifconf  )) < 0 )
  perror("ioctl(SIOGIFCONF)");

  close(sock);

  return rval; More >

How to print a bash variable along with other text

A general programmer tends to use a simple dollar sign ($) before each variable. However its gets difficult if a variable hash to be echoed along with some other as shown below.

An attempt to print “redtape” fails using the following method as bash attempts print value of “COLORtape” variable.

bash# COLOR=red
bash# echo $COLORtape
bash#

In such cases, braces should be used to protect the bash variable. The following example does that.

bash# COLOR=red
bash# echo ${COLOR}tape
redtape
bash#