inteliture.com
Google
Showing posts with label Linux tricks. Show all posts
Showing posts with label Linux tricks. Show all posts

Saturday, July 14, 2007

Shell scripting for linux

Introduction to shell scripting.

Normally shells are interactive. It means shell accept command from you (via keyboard) and execute them. But if you use command one by one (sequence of 'n' number of commands) , the you can store this sequence of command to text file and tell the shell to execute this text file instead of entering the commands. This is know as shell script.

Reasons why to write one.

  • Shell script can take input from user, file and output them on screen.
  • Useful to create our own commands.
  • Save lots of time.
  • To automate some task of day today life.
  • System Administration part can be also automated.
Some examples.

Example No1

$ vi first
#
# My first shell script
#
clear
echo "Knowledge is Power"

Example No2

$ vi ginfo
#
#
# Script to print user information who currently login , current date & time
#
clear
echo "Hello $USER"
echo "Today is \c ";date
echo "Number of user login : \c" ; who | wc -l
echo "Calendar"
cal
exit 0

Important notes.

To write a shell script, u can use any editor like vi or mcedit. For the above examples we used the vi.

After writing ur shell script(s), set execute permission by typing $ chmod permission your-script-name ex. $ chmod 755 first

Then to run it type $ ./your-script-name ex. ./first

NOTE: The syntax ./ means current directory, But only . (dot) means execute given command file in current shell without starting the new copy of shell. The syntax for . (dot) command is $ . command-name ex. $ . first

NOTE: Read the "VI editor" tutorial and "Linux commands" tutorial in this section if not yet.

Learn and use the vi linux editor

Introduction to vi.

VI displays a full screen window into the file you are editing. The contents of this window can be changed quickly and easily within vi. While editing, visual feedback is provided (the name vi itself is short for visual).

vi is one of three standard editors on the Linux system, the other two being:

  • ed - This is a line editor. While powerful in its own right, it is tedious to use.
  • ex - This is another line editor, but with the same powers and commands as vi: the names vi and ex - identify a particular user interface rather than any underlying functional difference.

In both ex and ed, visual updating of the terminal screen is limited, and commands are entered on a command line. vi, on the other hand, is a screen-oriented editor designed so that what you see on the screen corresponds exactly and immediately to the contents of the file you are editing.

Linux offers further editors, such as joe, pico and emacs (although the latter has been available for some years before Linux was created).

Its powerful and flexibile and it is an industry standard that is widely available, not only on Linux and Unix, but also on other operating systems.

Common commands.

To insert new text

esc + i ( You have to press 'escape' key then 'i')

To save file

esc + : + w (Press 'escape' key then 'colon' and finally 'w')

To save file with file name (save as)

esc + : + w "filename"

To quit the vi editor

esc + : + q

To quit without saving

esc + : + q!

To save and quit vi editor

esc + : + wq

To search for specified word in forward direction

esc + /word (Press 'escape' key, type /word-to-find, for e.g. to find word 'shri', type as
/shri)

To continue with search

n

To search for specified word in backward direction

esc + ?word (Press 'escape' key, type word-to-find)

To copy the line where cursor is located

esc + yy

To paste the text just deleted or copied at the cursor

esc + p

To delete entire line where cursor is located

esc + dd

To delete word from cursor position

esc + dw

To Find all occurrence of given word and Replace then globally without confirmation

esc + :$s/word-to-find/word-to-replace/g

For. ex. :$s/mumbai/pune/g
Here word "mumbai" is replace with "pune"

To Find all occurrence of given word and Replace then globally with confirmation

esc + :$s/word-to-find/word-to-replace/cg

To run shell command like ls, cp or date etc within vi

esc + :!shell-command

For ex. :!pwd