Programming

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 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 loop or iterate through the output of ls command in a bash script

If your original purpose is to iterate through the list of files and directories present in the current directory, you don’t even have to run ls command for that.

for i in *; do
   echo "$i"
done

Alternately you can use ls to get the list of files and directories as shown below.

for i in `ls`; do
   echo "$i"
done

Other way of writing the above example is:

for i in $(ls); do echo "$i"; done;

The ls command can be used to select files of certain pattern. The following example selects only files will extension “.sh”.

for i in $(ls *.sh); do echo More >

What are the useful fields of $_SERVER array in PHP

The following table describes useful fields in $_SERVER array in PHP.

Name Description PHP_SELF Returns the filename of the current script SERVER_PROTOCOL Name and revision of the information protocol ‘HTTP/1.0‘ or ‘HTTP/1.1′ REQUEST_METHOD Returns the request method used to access (GET/POST/PUT) REQUEST_TIME Returns the timestamp from the beginning DOCUMENT_ROOT Returns the root directory under which the current script is executing. This directory is based on server’s configuration. HTTP_REFERER Returns the page address that referred HTTP_USER_AGENT Returns the contents of user agent extracted from HTTP header. This information is typically used for identification of user’s browser and operating system. REMOTE_ADDR Returns the 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#