PoiNtEr->: shell programming

                             Difference between a dream and an aim. A dream requires soundless sleep, whereas an aim requires sleepless efforts.

Search This Blog

Showing posts with label shell programming. Show all posts
Showing posts with label shell programming. Show all posts

Monday, March 26, 2012

How To Use Grep Command In Ubuntu(Linux)

Grep Command(linux)
                                                              


The grep command is one of the most useful in Linux. It may not seem that exciting at first, but once you learn about pipes you'll begin to see why it's so indispensable.Put simply, grep searches through one or more text files for a specific word or phrase. For any lines it finds which include the specified word or phrase, it will display them on the screen. It will also work with standard output , when combined with pipes.
Simple example: Your name is vishal and you know that somewhere in your /home directory is a file you've written for work. Let's say you know that it has the word vishalmishra in it. So you can instruct grep to search through all the files in your home directory like this:
grep -r vishalmishra /home/vishal/*


if you wanted to make it include results in both upper and lower case, then use the -i option:
grep -ir vishalmishra /home/vishal/*



Suppose you want to see n lines after the match has found then use -A:
grep -A 3 -ir vishalmishra /home/vishal/*

Now above command will show you next 3 lines after the match word.


Suppose you want to see n lines before the match has found then use -B:
grep -B 3 -ir vishalmishra /home/vishal/*

Now above command will show you  3 lines before the match word.


Use -C option to Display N lines around the match:
 grep -C 3 -ir vishalmishra /home/vishal/*


Use -v option for invert match:
grep -v -r vishalmishra /home/vishal/*


If You want to count the number of matches then use -c option:
grep -c -r vishalmishra /home/vishal/*


If you want to known only file names that contain a specific match then use -l:
grep -l -r vishalmishra /home/vishal/*




Friday, March 9, 2012

Linux Signals – Example C Program to Catch Signal (SIGINT)


What is a signal? Signals are software interrupts.
A robust program need to handle signals. This is because signals are a way to deliver asynchronous events to the application.A user hitting ctrl+c, a process sending a signal to kill another process etc are all such cases where a process needs to do signal handling.
Linux Signals
In Linux, every signal has a name that begins with characters SIG. For example 
 * +--------------------+------------------+
 * |  POSIX signal      |  default action  |
 * +--------------------+------------------+
 * |  SIGHUP               |  terminate |
 * |  SIGINT                  | terminate |
 * |  SIGQUIT                | coredump |
 * |  SIGILL                     | coredump |
 * |  SIGTRAP                | coredump |
 * |  SIGABRT/SIGIOT    | coredump |
 * |  SIGBUS            | coredump |
 * |  SIGFPE            | coredump |
 * |  SIGKILL           | terminate(+) |
 * |  SIGUSR1           | terminate |
 * |  SIGSEGV           | coredump |
 * |  SIGUSR2           | terminate |
 * |  SIGPIPE           | terminate |
 * |  SIGALRM           | terminate |
 * |  SIGTERM           | terminate |
 * |  SIGCHLD           | ignore   |
 * |  SIGCONT           | ignore(*) |
 * |  SIGSTOP           | stop(*)(+)   |
 * |  SIGTSTP           | stop(*)   |
 * |  SIGTTIN           | stop(*)   |
 * |  SIGTTOU           | stop(*)   |
 * |  SIGURG            | ignore   |
 * |  SIGXCPU           | coredump |
 * |  SIGXFSZ           | coredump |
 * |  SIGVTALRM         | terminate |
 * |  SIGPROF           | terminate |
 * |  SIGPOLL/SIGIO     | terminate |
 * |  SIGSYS/SIGUNUSED  | coredump |
 * |  SIGSTKFLT         | terminate |
 * |  SIGWINCH          | ignore   |
 * |  SIGPWR            | terminate |
 * |  SIGRTMIN-SIGRTMAX | terminate       |
 * +--------------------+------------------+
 * |  non-POSIX signal  |  default action  |
 * +--------------------+------------------+
 * |  SIGEMT            |  coredump |
 * +--------------------+------------------+

Example C Program to Catch Signal "SIGINT"
//sigint.c
#include<stdio.h>
#include<signal.h>
#include<unistd.h>
void sig_handler(int signo)
{
  if (signo == SIGINT)
    printf("received SIGINT\n");
}
int main(void)
{
  if (signal(SIGINT, sig_handler) == SIG_ERR)
  printf("\ncan't catch SIGINT\n");
  // A long long wait so that we can easily issue a signal to this process
  while(1) 
    sleep(1);
  return 0;
}


gcc -o sigint sigint.c
Output:



Friday, March 2, 2012

BSS Section in Process Structure

Global variable's life time is until the completion of the program. But what is the point of having two sections for global variables based on initialized and uninitialized. The point is to reduce the executable size of the program before going to discuss about the BSS section we need to understand what process structure, and how the structure looks in the memory (RAM) at the time of execution.


Program Memory Layout

If we are not initialized the global variables the loader will take care to initialize all global variables to zero (0). The loader is designed in such a way to initialize the global to zero while loading the process into RAM. If we are keeping initialized and uninitialized variable in the same block loader will initialize to zero for all the initialized variables too. Compiler developer decided to divide the global sectioninto two parts

1. DATA
2. .BSS

All initialized variable will go to DATA section, this will be decided in compilation time itself. But uninitialized variable will go into BSS section. This block will be created at the time of loading so it reduces the executable size of the program.


Now lets try out some example to proof how bss is effective save memory....

#include<stdio.h>
int a[10000000];
main()
{
long i;
for(i=0;i<10000000;i++)
printf("%d",a[i]);
}
//save it as bss.c
compile above program
>gcc -o bss bss.c
>ls -l bss
output:
-rwxr-xr-x 1 vishal vishal 7149 2012-03-02 19:44 bss
so size of this executable is 7149= around 7KB



#include<stdio.h>
int a[10000000]={1};
main()
{
long i;
for(i=0;i<10000000;i++)
printf("%d",a[i]);
}
//save it as ubss.c
>gcc -o ubss ubss.c
>ls -l ubss
output:-rwxr-xr-x 1 vishal vishal 40007193 2012-03-02 19:50 ubss
size of this executable is :40MB
because here a[] is initialized and memory is already allotted to it...

Saturday, November 5, 2011

Types of Shell in Linux



Shells plays an important role in Linux and Unix system. Everything that is happening on your distro is either because of the shell or the kernel. Moreover, a backend  shell is initialized as soon as you made a login . There are lots of shells in the Unix and Linux system. Let’s try to know more about them :

What is a shell ?

Shell is the software installed on your system that works as command-line interpreter i.e. it takes the commands you type, interpret them and gives them to operating system (kernel) through system calls to execute. There are many different shells available to choose from in Linux.

C Shell : C shell was created by Bill Joy, while he was studying at University of California, Berkeley. He released it under BSD license, the syntax of C shell is similar to C programming language hence the name C shell.

TC Shell: An improved version of C shell, provides some additional features like command line completion and editing. In most of Linux systems /bin/csh is linked to /bin/tcsh and it alternatives, so when you try to start the C shell, TC shell gets started.

C shell linked to its alternatives

Korn Shell (ksh): Korn shell was developed by David Korn at AT&T Bell Laboratories. It contains may features of C shell and Bourne shell.

Bourne Shell(sh): The default shell in many Unix systems developed by Stephen Bourne of AT&T Bell Laboratories.

GNU Bourne-Again Shell (bash): Bash shell is default shell in most of Linux distributions. Its a free and open source clone of bourne shell. It was developed by Brian Fox for the GNU project. Its the most feature rich shell available and is compatible with bourne shell.

To find out all the shells installed on your system type “$cat /etc/shells”. Your default shell is defined /etc/passwd file.

To switch to a different shell on your system just type the name of that shell (as in /etc/shells) and hit enter.


Z Shell(zsh):  Well The Z shell (zsh) is a Unix shell that can be used as an interactive login shell and as a powerful command interpreter for shell scripting. Zsh can be thought of as an extended Bourne shell with a large number of improvements, including some features of bash, ksh, and tcsh. It includes features like automated spell correction, editing  multiple line command within a single buffer and many more.

Finally My shell....