inteliture.com
Google
Showing posts with label LINUX. Show all posts
Showing posts with label LINUX. 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

How to install LINUX

Introduction.

Basically i will try to explain u how to install linux. Also how to install linux on a PC with already windows installed on it.

NOTE: Yes u can have linux and windows on same PC.

Procedure.

  1. Prepare the Hard Disc (HD): 80MB - 2GB+ free space for installing
  2. Partition: If ur HD is new/empty, the installation procedure will ask u to choose the partition and simply ignore almost everything below. Otherwise, if ex. windows are already installed, we must split the HD by creating new partition. To do this u must have Partition Magic (get it here) or Fips (get it here)
  3. Create minimum 2 new partitions. 1 for system files and 1 for SWAP (max 128 MB)
  4. Boot ur PC from CD-ROM (boot ur PC and press F2... in the BIOS setup set boot to CD-ROM... reboot)
  5. When the linux CD will ask us to select the partition to install in to, u must know the followings:
  • If u have a HD for a primary master and a CD-ROM for a secondary master, will be readed as "hda" and "hdc". Primary slave "hdb" and Secondary slave "hdd".
  • When dialing with partitions, windows changes letters to units, but linux adds numbers. So if u have a HD as primary master (hda) and if u created two partitions in it, then those will be "hda1" and "hda2".

NOTE: After we sellect the partition to install to, it will ask us for "mount point" (a directory which will recognize the partition). If u created one partition, the mount point will be slash(/). If u created more partitions, then u can give a different mount point like ex. /home, /usr etc.

New Partitions with Fips.

Windows are automatically put files at the end of HD. So by creating new partitions, those files will be lost. So, if we are trying to install linux on a PC with windows installed, this may cause errors to windows and may not function properly. So, we do a Defrag (C:\ right mouse click -> properties -> tools -> defragmentation), so if are any files at the end of the HD, to place them at the beginning. The reason, except that we may cause errors to previous ex. windows already installed, is that if there are any files at the end of HD, the Fips wont let us continue to the creation of new partitions.

To avoid any crash/errors of ex. windows, we must boot our system from CD-ROM or MS-DOS (boot ur PC and press F2... in the BIOS setup set boot to MS-DOS and use Fips from a floppy disc or set boot to CD-ROM and use Fips from a CD... reboot)

When Fips starts, will scan ur system for HDs and if found more than one, will ask u which one to use. Then it will ask u the size of the partition. Then hit ENTER and Fips will modify the partition table.

NOTE: In case u restart ur PC, u will see a ne unit which u cant use and ur system will automatically ask u to format it. Doesnt matter if u format to FAT or NTFS, coz in this space we will create a File System(ext2). So, when u format in ext2, this unit will disappear from windows, coz windows cant read partition ext2.

NOTE: We can create as many partitions as we want ex. a different partition for the users.