Saturday 21 March 2015

CMD tutorial part 1

title
I think i have done lots of general purpose,kind of posts in past,So i thought to do something different than what i do generally..
Hence i decided to dedicate this post to CMD's tutorial,This is the first part of two part tutorial,
Before continuing with anything i would like to introduce you with this fellow called CMD. It is commonly used to mean "command" and is a command line interface provided by Microsoft's windows,Command line interface means you don't have a beautiful and attractive GUI to communicate with your computer instead you have a black window which has some text written in white grayish white color and same colored horizontal blinker which looks as ugly as toad is.But don't ever underestimate the power of it.You can practically do anything on it.You know windows started as a plain dark DOS shell which is now known as CMD , and..


 FYI , Developers first create a command line or non GUI version of the application program then afterwards they overlay a GUI on it.Which is why you have more flexibility in a non GUI version,there are lot more benefits of using a text based application rather than image and graphics based ,like they use less CPU juice ,they consume less RAM ,and using them will make you like a computer expert..Sounds Interesting huh,So lets dive in.
First thing,First.
Commands : In computing, a command is a directive to a computer program acting as an interpreter of some kind, in order to perform a specific task. Most commonly a command is a directive to some kind of command-line interface, such as a shell. Specifically, the term command is used in imperative computer languages.
Well that was just another technical jargon thrown by wikipedia , by the way commands simply means a special keyword to the computer or a system which it interprets to perform some actions,like halting or creating a process,printing a document,shutting down a PC and so on.Windows divided Commands into two major categories Internal and External.

Internal Commands
The commands included in the "COMMAND.COM" file located at "%windir%/system32" directory,are the internal commands.
These are they..
ASSOC , BREAK , CALL , CD/CHDIR , CLS , COLOR , COPY , DATE , DEL , DIR , DPATH , ECHO , ENDLOCAL , ERASE , EXIT , FOR , FTYPE , GOTO , IF , KEYS , MD/MKDIR , MKLINK (vista and above) , MOVE , PATH , PAUSE , POPD , PROMPT , PUSHD , REM , REN/RENAME , RD/RMDIR , SET , SETLOCAL , SHIFT , START , TIME , TITLE , TYPE , VER , VERIFY , VOL

External Commands
Well,this is the interesting part..
While Internal commands are limited in nos and their's definition is packed in one file,there could be practically infinite external commands,Hold on !! Hold on!
Although Microsoft provides only a certain set of external commands,but they allow you to use much more than that.
As you know now that internal commands are the one which are there at "COMMAND.COM" so its easier to guess that the external commands are the one which aren't packed in that file.
So Question is where are they then ?
They could be anywhere,anywhere in your computer including the USB drive you plug in.
The reason is these(external commands) are not actually commands(to CMD), they are the programs which you access via cmd, CMD just gives you a platform kind of thing to interact and run these programs,These programs can be in the form of .exe,com,vbs,bat and more,You will find and can edit the type and order of program you want to run by following the following path
System Properties>Advanced system settings>Environment Variables>PATHEXT


PATHEXT, is a word made up of two words PATH and EXT.The word PATH has its usual meaning and EXT means extension and you can see some of the extension listed on the right of the word PATHEXT,these extensions denotes the program that cmd runs present in the directories mentioned in the Path column present just above the PATHEXT
if you aren't able to understand whats going on just keep on reading and I'm sure you will..
Now lets talk about the order or precedence in which the command/program will be searched and gets executed.
Suppose,you typed "shutdown -s" followed by enter in cmd.It wont directly shuts-down your windows,Lets take a closer look on this,
First thing is,the thing that you typed is a string ,which consists of a several characters one of which is a "SPACE" and "SPACE" sits down in b/w two sub-strings which as you already guessed are "shutdown" and "-s" here shutdown is an executable(.exe) program stored in your %windir%/system32/ and "-s" is the argument passed to "shutdown.exe" but it didn't gone for that directory directly,it firstly searched for the "shutdown" in it's internal command list or in "COMMAND.COM" file then it will go for the directory your cmd is currently in.

The image above shows that cmd is currently in "X:\Users\itworld" if there will be no application titled shutdown with extension .exe .bat or anything which is listed in PATHEXT then it will go for the first directory mentioned in column,Path which is just above the PATHEXT(as you already know) which in default is "%windir%\System32"(and which in my case is X:\Windows\system32) and luckily a shutdown program which has an extension .exe dwells in that directory(if you didn't rehabilitated it),hence CMD will execute it providing it an argument "-s",which it understands as a halt signal and in response will initiate a system call to shutdown and exit the windows normally.

NOTE : Suppose,you have two programs which have same First name,but distinct surnames or extensions Assume these two files ohCrap.exe and ohCrap.bat exist in the directory your cmd is currently pointing to,then cmd will execute only ohCrap.exe,the reason for that is .EXE is mentioned before .BAT in the PATHEXT of Environment Variable,Similarly if there exists another same named file, with .COM extension then that will be the only one to be executed.

Now i think you are very well capable of understanding every bit of this message of cmd.
You can try to create a program and turn it into and external command that you can access via cmd,and pass some arguments as well. Below is the prototype of a C program that accepts argument via cmd(Windows)/terminal(linux).

Bonus : As external commands are just programs and C and java compiler and editor are also programs you can run these or any other similar programs,inside it. You just have to provide the location of the program that you need to run via CMD, in Path section of Environment variable e.g., To Run turbo c in command prompt open up the Environment Variable and add the path "C:\TC\BIN"(change the path according to yours) which is being followed by ;,Semi colon(;) separates two path in that section.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int count,char *value[])
{
  if(count>1)
  {
    if(!strcmp(value[1],"exit"))
      exit(0);
    else if(!strcmp(value[1],"help"))
      printf("A great and awesome program developed by itworld\n");
    else
      printf("BOO..!! invalid argument\n");
  }
 else
    printf("Argue with me,please");
}

Download a commented version of the above c program from here
Try running and understanding this program,if you get stuck,at any part of the program, or of this post,comment below or message me on facebook.
I will be back soon with another part of this tutorial which will deal with really fun commands, So wait till then,
and ,Thanks for visiting..

No comments:

Post a Comment