Creating commands in linux by a simple C program


Below i’ll try to  give an idea about how bash commands are implemented in linux by illustrating a small example.

        Simple way to create a command( in C ) ‘hello’ which prints hello world.

  • Step1 :  Create a cprogram “test.c” to print the string “hello world” by          first opening the file “test.c” using the text editor “vim”

                    

                                 vim test.c  
                           
                         and then writing into it

                                #include
                                main(){
                                           printf(“hello world\n”); 

                                 }

  • Step 2  : now compile the program by using the compiler gcc

                                gcc test.c

                       to produce an executable file” a.out”.Now this file is the object file which prints the string “hello world” to the monitor.for executing the file we use as

                               ./a.out

  • Step 3 :  Actually, a command like “cp” has it’s executable file in the directory /bin ,so by copying our executable file to that folder we can use it as a command .

                              sudo cp a.out /bin/hello

         which prompts for the root password

             Now we can use the command “hello” in the bash prompt to print “hello world” on the screen.

Advertisements
  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: