Tuesday, October 25, 2011

C shell History Commands

CommandFunction
!!Repeat the previous command
!$Repeat the last word of the previous command.
!*Repeat all but the first word of the previous command.
^a^bReplace a with b in the previous command.
!nRepeat command n from the history list.

Friday, October 21, 2011

Identifying the type of shell

If you screen shows a % symbol, it means you are using the C shell or modified C shell (tcsh
). If your prompt contains $you could be using the Bourne shell, the Korn shell, or a variant thereof.

UNIX difference btw :q and :q!

Open a file in vi editor and modify the content of the file.

Now try entering :q

of modern UNIX is the energy of the UNIX programming community; plenty of UNIX users
decide to write a new version of a command in order to solve slightly different problems, thus
spawning many versions of a command.
C
No write since last change (:quit! overrides)


You get the error as above. In this case you should enter :q!

Hence
:q means quit vi editor
:q! quit regardless of whether any changes have occured

UNIX: Searching within the file

What is the command to go to 10th line of the given file?
10G

Will 10g works?
No. Remember in UNIX case sensitive matters.
Then what happens if you enter G alone?
The cursor will go to last line of your file.

Search for a pattern in the file
Enter '/', will take you to the end of the file and then provide the patter to search...

primary players. Each is a little different from the other.) Another contributor to the sprawl
of modern UNIX is the energy of the UNIX programming community; plenty of UNIX users
decide to write a new version of a command in order to solve slightly different problems, thus
spawning many versions of a command.
C
/



primary players. Each is a little different from the other.) Another contributor to the sprawl
of modern UNIX is the energy of the UNIX programming community; plenty of UNIX users
decide to write a new version of a command in order to solve slightly different problems, thus
spawning many versions of a command.
C
/decide



primary players. Each is a little different from the other.) Another contributor to the sprawl
of modern UNIX is the energy of the UNIX programming community; plenty of UNIX users
decide to write a new version of a command in order to solve slightly different problems, thus
spawning many versions of a command.
C
/decide



To find next occurence press "n"


Here also the case matters. If you type "DECIDE" after the search command "/" UNIX will look for "DECIDE" not "decide"


What if I need to search backward
ans:
use the command ?
primary players. Each is a little different from the other.) Another contributor to the sprawl
of modern UNIX is the energy of the UNIX programming community; plenty of UNIX users
decide to write a new version of a command in order to solve slightly different problems, thus
spawning many versions of a command.
C
?decide



Now when you enter command "n" it will search backward!!

****
Hope this is helpful. Thanks Phoenix...

Thursday, October 20, 2011

Vi Editor

When we need to add some more lines to existing file we can use "cat" commnad, if we need to replace some string we have "sed"

But when we need to create a new file or edit already existing files we have to go for screen oriented editors.
There are different types of editors vi, ed, ex and emacs of which vi is the most choosen one. vi and emacs use the entire screen to edit.

Different modes of vi:
vi editor has two different modes, hence it is also called "modaleditor". The two different modes are "insert" and "command" mode. The way UNIX interprets your input depends on which mode we are. For example when we are in commnad mode and enter "dd", it will delete one single line, whereas if we are in edit mode entering "dd" will add "dd" to the file. So whenever you are in doubt in which mode you are, just keep on pressin escape button which will take you to command mode.


Diffrence beween vi and emacs:
emacs is a modeless editor. ie whenver you enter any key it adds to the file. Command in emac is entered by holding the control key while entering the command. For example ctrl+c will delete a character.

Cursor Moverments:

In keyboard the letters H,J,K,L comes in a series.
To move the curson left in a file -H
Right - L
Down - J
Up -K

You can move to the begining of next line by pressing the return key.
Move the cursor to the begining of line - ^,0 (zero)
Move the cursor to the end of line - $
Move forward word by word - w
Move backward word by word- b
Move forward to mid of the text in screen - ctrl+d
Move backward mid of the text - ctrl+u
Page down - ctrl+f
Page up -ctrl+b

vi insert modes:
There are basically four different ways you can get into vi insert mode.
i - insert text into file
a - append text into file
o - Open up a line below current line
O - Open up a line above current line (capital o)

Difference btw i and a for edit mode:
at present the cursor is on a, and now I am going to enter edit mode by entering "i"
On entring "i" the curson doesnt move and it will be there in the same location as above and now I am entering "cursor not moved" and you can see that the text entered to the left of "a" with cursor is not moved at all from "a" and then  press escape, you will find cursor move one space back.

On entering "a" to get into the edit mode, vi behaves differently.
Assume the cursor position is on "a" same as above and press "a" to move to edit mode, you can see that the cursor moves one place forward to "l" and whatever we enter is appended after the letter "l" and press escape, you see cursor moves one space back.

Deleting text with "vi" editor

While in command mode there are few commands provided by UNIX to delete the text from the file.

<<file content>>
you can learn the one-and-only
in doing that task as is the next person. It.s easy.
do it, too.
UNIX, by contrast, is much more like a spoken language, with
command options (which you learn about later in this lesson)
more complex commands acting akin to sentences. How you do a specific task can, therefore,
(pronounce that .system five release four. or, to sound like an ace, .ess-vee-are-four.), and
BSD UNIX (pronounced .bee-ess-dee.) from University of California at Berkeley are the
<<>>

Try entering "dd" which deletes an entire line and shift the lines up
<<file content>>
you can learn the one-and-only
in doing that task as is the next person. It.s easy.
UNIX, by contrast, is much more like a spoken language, with
command options (which you learn about later in this lesson)
more complex commands acting akin to sentences. How you do a specific task can, therefore,
(pronounce that .system five release four. or, to sound like an ace, .ess-vee-are-four.), and
BSD UNIX (pronounced .bee-ess-dee.) from University of California at Berkeley are the
<<>>

"2dd" - will delete two lines
<<file content>>
you can learn the one-and-only
in doing that task as is the next person. It.s easy.
more complex commands acting akin to sentences. How you do a specific task can, therefore,
(pronounce that .system five release four. or, to sound like an ace, .ess-vee-are-four.), and
BSD UNIX (pronounced .bee-ess-dee.) from University of California at Berkeley are the
<<>>

 "D" - Delete the line to the right of the cursor
<<file content>>
you can learn the one-and-only
in doing that task as is the next person. It.s easy.
_

(pronounce that .system five release four. or, to sound like an ace, .ess-vee-are-four.), and
BSD UNIX (pronounced .bee-ess-dee.) from University of California at Berkeley are the
<<>>


<<file content>>
you can learn the one-and-only
in doing that task as is the next person. It.s easy.

pronounce that .system five release four. or, to sound like an ace, .ess-vee-are-four.), and
BSD UNIX (pronounced .bee-ess-dee.) from University of California at Berkeley are the
<<>>

Here after entering "D", we have
<<file content>>
you can learn the one-and-only
in doing that task as is the next person. It.s easy.

pronounce that .system_
BSD UNIX (pronounced .bee-ess-dee.) from University of California at Berkeley are the
<<>>


Now let us tweek a little of the "d" commnad
when "w" (word) is used with "d", dw this delete word by word


What is the command in Unix equivalent to delete and Backspace that we use in windows?
x - Deletes the text as like how we use the "delete" button in windows
X [<-] - Deletes the text as like when we use the backspace in windows.


What if you need to undo the delete?
You can undo only the last action using "u"

Example Scenario:
<<file content>>
you can learn the one-and-only
in doing that task as is the next person. It.s easy.

pronounce that .system five release four. or, to sound like an ace, .ess-vee-are-four.), and
BSD UNIX (pronounced .bee-ess-dee.) from University of California at Berkeley are the
<<>>

type dw

<<file content>>
you can learn the one-and-only
in doing that task as is the next person. It.s easy.

pronounce that .system release four. or, to sound like an ace, .ess-vee-are-four.), and
BSD UNIX (pronounced .bee-ess-dee.) from University of California at Berkeley are the
<<>>


type "D"
<<file content>>
you can learn the one-and-only
in doing that task as is the next person. It.s easy.

pronounce that .system_
BSD UNIX (pronounced .bee-ess-dee.) from University of California at Berkeley are the
<<>>


type "u"
<<file content>>
you can learn the one-and-only
in doing that task as is the next person. It.s easy.

pronounce that .system release four. or, to sound like an ace, .ess-vee-are-four.), and
BSD UNIX (pronounced .bee-ess-dee.) from University of California at Berkeley are the
<<>>


type "U"

<<file content>>
you can learn the one-and-only
in doing that task as is the next person. It.s easy.

pronounce that .system five release four. or, to sound like an ace, .ess-vee-are-four.), and
BSD UNIX (pronounced .bee-ess-dee.) from University of California at Berkeley are the
<<>>


You can see that entire line is replaced. This can be achieved only if you hadn't moved from the line which you are editing.

Change the case of the letter
Use the command tild (~) or "delete" button in your keyboard.

Tuesday, October 18, 2011

Power Filters and File redirection

Two powerful commands that we learn in this section:
awk:helps you get specific columns of information, modify text, swap the order of column of information in a file
tee: enable you to save a copy of the data being transmitted through a pipeline

awk
Syntax: awk '{commands}'
flags:
-f file  specifies that the instruction should be read from the file rather than from command line
-Fc indicates that the program should consider the letter c as the separator between fields of

using print with awk
bash-3.00$ whobuild      pts/2        Oct 17 11:28    (10.222.46.189)
root       pts/10       Jul 12 18:45    (10.209.188.122)
scholm  pts/12       Oct 18 23:46    (10.203.54.223)
jmadh  pts/15       Oct 19 11:06    (10.203.54.225)


bash-3.00$ who | awk '{print}'
build      pts/2        Oct 17 11:28    (10.222.46.189)
root       pts/10       Jul 12 18:45    (10.209.188.122)
scholm  pts/12       Oct 18 23:46    (10.203.54.223)
jmadh  pts/15       Oct 19 11:06    (10.203.54.225)


bash-3.00$ who | awk '{print$1}'
build
root
scholm
jmadh

bash-3.00$ who | awk '{print$2}'pts/2
pts/10
pts/12
pts/15

bash-3.00$ who | awk '{print$3}'Oct
Jul
Oct
Oct
Here what you have seen is each column is considered as field and each field is represented by $1, $2, $3 etc...for field one, field two, field three respectively.

Much better customization:
bash-3.00$  who | awk '{ print "User " $1 " is on terminal line " $2 }'User build     is on terminal line pts/2
User root     is on terminal line pts/10
User scholm is on terminal line pts/12
User jmadh is on terminal line pts/15
Note: You need to be careful to place the strings to be printed in double quotes, else throw error as below, due to conflict with single quotes of the awk command.

bash-3.00$  who | awk '{ print 'User' $1 ' is on terminal line ' $2 }'awk: syntax error near line 1
awk: illegal statement near line 1


NF Variable:
NF indicates number of fields in a line.
When we use this variable without a $ sign it give us number of fields in each line

bash-3.00$ who | awk '{print NF}'6
6
6
6

When used with $ it displays the value at last field of each line
bash-3.00$ who | awk '{print $NF}'(10.222.46.189)
(10.209.188.122)
(10.203.54.223)
(10.203.54.225)

similarly NR stands for number of records.

Sum up the file size with the awk command:
bash-3.00$ ls -ltotal 80
-rw-r--r--   1 fwire    fwire         12 Oct 18 22:40 MyWords.txt
-rw-r--r--   1 fwire    fwire       2958 Oct 19 00:02 def.txt
-rw-r--r--   1 fwire    fwire       2976 Oct 19 00:01 def_new.txt
-rwxr-xr-x   1 fwire    fwire        141 Oct 18 23:40 search
-rw-r--r--   1 fwire    fwire       2958 Oct 18 21:57 what is unix.txt

file size is the fifthe field
bash-3.00$ ls -l | awk '{print $5}'
12
2958
2976
141
2958

Now we need to sum up all these size

bash-3.00$ ls -l | awk '{total=total+$5;print total}'0
12
2970
5946
6087
9045

To get the summed up vlaue alone use command below
bash-3.00$ ls -l | awk '{total+=$5;print total}'| tail -19045

Much better way is using END command
bash-3.00$  ls -l | awk '{total+=$5} END {print total}'9045
It will be better if we can put this code into some file so that our command line will be less lengthier
% cat << EOF > script
{ totalsize += $4 } END { print “You have a total of “totalsize .”}
EOF
The above command will create a file called script in your current directory with belwo content
{ totalsize += $4 } END { print “You have a total of “totalsize .”}

bash-3.00$ ls -l | awk -f scriptYou have a size of 9150
In above command -f tells awk command to look into the file script

 
We can use awk commnad for conditional execution of statement as well, as shown in the example below
% awk -F: ‘{ if (length($1) == 2) print $0 }’ /etc/passwd | wc -l

Here the passwd file is given as input for the awk command, where it says the field seperator is ':' and if the length of first field, ic account is two character lenght, then print the entire lines from the passwd file and this output  is then supplied to wc where we added -l flag, which gives us the total number of lines.

Say we recived an output of 5, this indicates that there are totally 5 accounts which are two character length.
 
tee  command has only one option, -a, which appends the stream data/ output to a specified file.
bash-3.00$ who | tee who.outfwire      pts/10       Jul 12 18:45    (10.209.188.122)
fwire      pts/12       Oct 18 23:46    (10.203.54.223)
fwire      pts/15       Oct 19 14:33    (10.203.54.224)


bash-3.00$ cat who.outfwire      pts/10       Jul 12 18:45    (10.209.188.122)
fwire      pts/12       Oct 18 23:46    (10.203.54.223)
fwire      pts/15       Oct 19 14:33    (10.203.54.224)


Here who | tee who.out placed the output of who to who.out file, which can also be achieved by the command who > who.out. Hence, what is the difference between these two.

In the first type of command the primary thing is we are displaying who are all there in the system and in the background the contents are saved into a specified file, here who.out (you can see the output in the screen for first type of command whereas you cant see for the second type). In the second type of command, the action is writing the output to  the specified file but not been displayed in the screen.


 





tee command:
information, rather than the default of white space