Thursday 25 August 2011

What is Processes

Process is kind of program or task carried out by your PC. For e.g.
$ ls -lR

Process defined as:
"A process is program (command given by user) to perform specific Job. In Linux when we start process, it gives a number to process (called PID or process-id), PID starts from 0 to 65535."

As We know Linux is multi-user, multitasking Os. It means we can run more than two process simultaneously if we wish. For e.g. To find how many files do we have on your system we may give command like:

$ ls / -R | wc -l

This command will take lot of time to search all files on your system. So we can run such command in Background or simultaneously by giving command like

$ ls / -R | wc -l &

The ampersand (&) at the end of command tells shells start process (ls / -R | wc -l) and run it in background takes next command immediately.
Process & PID defined as:
"An instance of running command is called process and the number printed by shell is called process-id (PID), this PID can be use to refer specific running process."

Following tables most commonly used command(s) with process:
For this purposeUse this CommandExamples*
To see currently running process ps$ ps
To stop any process by PID i.e. to kill processkill    {PID}$ kill  1012
To stop processes by name i.e. to kill processkillall   {Process-name}$ killall httpd
To get information about all running processps -ef$ ps -ef
To stop all process except your shellkill 0$ kill 0
For background processing (With &, use to put particular command and program in background)linux-command  &$ ls / -R | wc -l &
To see if a particular process is running or not. For this purpose you have to use ps command in combination with the grep command  ps -ef | grep  process-U-want-to seeFor e.g. you want to see whether Apache web server process is running or not then give command$ ps -ef | grep httpd
To see currently running processes and other information like memory and CPU usage with real time updates.top
See the output of top command.

$ top


Note
that to exit from top command press q.

Pipes

A pipe is a way to connect the output of one program to the input of another program without any temporary file.
Pipe - Redirecting output of 1st command to 2nd without creating temporary file
Pipe Defined as:
"A pipe is nothing but a temporary storage place where the output of one command is stored and then passed as the input for second command. Pipes are used to run more than two commands ( Multiple commands) from same command line."
Syntax:
command1 | command2

Filter

If a Linux command accepts its input from the standard input and produces its output on standard output is know as a filter. A filter performs some kind of process on the input and gives output. For e.g.. Suppose you have file called 'hotel.txt' with 100 lines data, And from 'hotel.txt' you would like to print contains from line number 20 to line number 30 and store this result to file called 'hlist' then give command:
$ tail +20 < hotel.txt | head -n30 >hlist
Here head command is filter which takes its input from tail command (tail command start selecting from line number 20 of given file i.e. hotel.txt) and passes this lines as input to head, whose output is redirected to 'hlist' file.
Consider one more following example
$ sort < sname | uniq > u_sname
Here uniq is filter which takes its input from sort command and passes this lines as input to uniq; Then uniqs output is redirected to "u_sname" file.

 Example :-
-bash-3.00$ ls -lrt
total 5828
-rwxrwxrwx   1 qa1      qa       1350256 Jun 11  2010 opx_PAR_ERI_720
-rwxrwxrwx   1 qa1      qa       1597852 Jun 22 03:20 opx_PAR_ERI_6as
-rwxrwxrwx   1 qa1      qa            91 Aug  8 07:16 Din
-rwxrwxrwx   1 qa1      qa            26 Aug  8 07:19 sname
-bash-3.00$ for i in $(ls -lrt)
> do
> echo $i
> done
total
5828
-rwxrwxrwx
1
qa1
qa
1350256
Jun
11
2010
opx_PAR_ERI_720
-rwxrwxrwx
1
qa1
qa
1597852
Jun
22
03:20

-bash-3.00$ IFS="
> "
-bash-3.00$ for i in $(ls -lrt); do echo $i; done
total 5828
-rwxrwxrwx   1 qa1      qa       1350256 Jun 11  2010 opx_PAR_ERI_720
-rwxrwxrwx   1 qa1      qa       1597852 Jun 22 03:20 opx_PAR_ERI_6as
-rwxrwxrwx   1 qa1      qa            91 Aug  8 07:16 Din
-rwxrwxrwx   1 qa1      qa            26 Aug  8 07:19 sname
-rwxrwxrwx   1 qa1      qa            26 Aug  8 07:21 sorted_name
-rwxrwxrwx   1 qa1      qa            26 Aug  8 07:23 cap_names
-rwxrwxrwx   1 qa1      qa            26 Aug  8 07:26 new_sorted_names
-rwxrwxrwx   1 qa1      qa            44 Aug  8 07:50 er



Monday 8 August 2011

Unix : I/O Redirection

There are three main redirection symbols >,>>,<
1) Linux-command > filename
eg:-

$ ls > myfiles
Now if 'myfiles' file exist in your current directory it will be overwritten without any type of warning.

2) Linux-command >> filename 
 eg:-
$ date >> myfiles 
To output Linux-commands result (output of command or shell script) to END of file. Note that if file exist , it will be opened and new information/data will be written to END of file, without losing previous information/data, And if file is not exist, then new file is created.

3) Linux-command < filename
eg:-
$ cat < myfiles 
To take input to Linux-command from file instead of key-board. For e.g. To take input for cat command give 


$cat > sname
vivek
ashish
zebra
babu
Press CTRL + D to save.

$ sort < sname > sorted_names
$ cat sorted_names
ashish
babu
vivek
zebra

$ tr "[a-z]" "[A-Z]" < sname > cap_names
$ cat cap_names
VIVEK
ASHISH
ZEBRA
BABU

tr command is used to translate all lower case characters to upper-case letters. It take input from sname file, and tr's output is redirected to cap_names file 

For sorting in descending order:-
$ sort -r 


 
 

Friday 5 August 2011

Why Command Line arguments required


  1. Telling the command/utility which option to use.
  2. Informing the utility/command which file or group of files to process (reading/writing of files). 
To run two command with one command line.
Syntax:
command1;command2

$ date;who

Get input from keyboard and store to a variable.

-bash-3.00$  vi Din
echo "Enter The Number : "
read num
echo "Square of your number is `expr $num \* $num` "

bash-3.00$ chmod +x *
-bash-3.00$ ./Din
Enter The Number :
4
Square of your number is 16

Use of  bc command:-
-bash-3.00$ bc
scale=2
10 / 3
3.33
scale=5
10 / 3
3.33333


OR

-bash-3.00$ echo "scale=2" > myfile
-bash-3.00$ echo "5 / 2" >> myfile
-bash-3.00$ bc < myfile
2.50












UNIX : Quotes


There are three types of quotes
Quotes
Name
Meaning
" Double Quotes "Double Quotes" - Anything enclose in double quotes removed meaning of that characters (except \ and $).
' Single quotes 'Single quotes' - Enclosed in single quotes remains unchanged.
` Back quote
`Back quote` - To execute command

Example:
-bash-3.00$  echo "Today is date"
Today is date
-bash-3.00$ echo "Today is `date`".
Today is Friday,  5 August 2011 05:49:13 IST.
-bash-3.00$ echo "sum is `expr 2 + 3` "
sum is 5


By default in Linux if particular command/shell script is executed, it return two type of values which is used to see whether command or shell script executed is successful or not.
(1) If return value is zero (0), command is successful.
(2) If return value is nonzero, command is not successful or some sort of error executing command/shell script.
This value is know as Exit Status

To determine this exit Status you can use $? special variable of shell.

-bash-3.00$ rm unknowfile
unknow1file: No such file or directory
-bash-3.00$ echo $?
2
-bash-3.00$ ls
d                Din.sh           opx_PAR_ERI_6as
dev              first            opx_PAR_ERI_720
-bash-3.00$ echo $?
0
-bash-3.00$ expr 1 + 3
4
-bash-3.00$ echo $?
0



Unix : Display colorful text on screen


Parameter Meaning Example
0 Sets default color scheme (White foreground and Black background), normal intensity, no blinking etc.  
1 Set BOLD intensity $ echo -e "I am \033[1m BOLD \033[0m Person"I am BOLD Person
Prints BOLD word in bold intensity and next ANSI Sequence remove bold effect (\033[0m)
2 Set dim intensity $ echo -e "\033[1m  BOLD \033[2m DIM  \033[0m"
5 Blink Effect $ echo -e "\033[5m Flash!  \033[0m"
7 Reverse video effect i.e. Black foreground and white background in default color scheme $ echo -e "\033[7m Linux OS! Best OS!! \033[0m"
11 Shows special control character as graphics character. For e.g. Before issuing this command press alt key (hold down it) from numeric key pad press 178 and leave both key; nothing will be printed. Now give --> command shown in example and try the above, it works. (Hey you must know extended ASCII Character for this!!!) $ press alt + 178
$ echo -e "\033[11m"
$ press alt + 178
$ echo -e "\033[0m"
$ press alt + 178
25 Removes/disables blink effect  
27 Removes/disables reverse effect  
30 - 37 Set foreground color
31 - RED
32 - Green
xx - Try to find yourself this left as exercise for you :-)
$ echo -e "\033[31m I am in Red"
40 - 47 Set background color
xx - Try to find yourself this left as exercise for you :-)
$ echo -e "\033[44m Wow!!!"

UNIX : echo Command


Use echo command to display text or value of variable.
echo [options] [string, variables...]
Displays text or variables value on screen.
Options
-e Enable interpretation of the following backslash escaped characters in the strings:
\b backspace
\c suppress trailing new line
\n new line
\r carriage return
\t horizontal tab
\\ backslash
For e.g. $ echo -e "Everyday is not \a\t\tSunday\n"

expr 20 %3 - Remainder read as 20 mod 3 and remainder is 2.
expr 10 \* 3 - Multiplication use \* and not * since its wild card.

ls Command:-

-bash-3.00$ ls *
d                dev              Din              first            myfile           opx_PAR_ERI_6as  opx_PAR_ERI_720


-bash-3.00$ ls [def]*
d      dev    first


-bash-3.00$ ls [d-f]*
d      dev    Din    first



$ ls ?   will show all files whose names are 1 character long 
$ ls fo? will show all files whose names are 3 character long and file name begin with fo.
$ ls /bin/[!a-o]
$ ls /bin/[^a-o]

If the first character following the [ is a ! or a ^ ,then any character not enclosed is matched i.e. do not show us file name that beginning with a,b,c,e...o.