Saturday 1 July 2017

Linux-7(ShellScript-1)


                                                                  Vi Editor

There are many ways to edit files in Unix. Editing files using the screen-oriented text editor vi is one of the best ways. This editor enables you to edit lines in context with other lines in the file.
An improved version of the vi editor which is called the VIM.we can use the vi editor to edit an existing file or to create a new file from scratch.we can also use this editor to just read a text file.

vi filename  --> Creates a new file if it already does not exist, otherwise opens an
existing file.
vi -R filename--> Opens an existing file in the read-only mode.
view filename-->  Opens an existing file in the read-only mode.

Vi editor available in two modes −
 Command mode − to perform administrative tasks such as saving the files, executing the commands, moving the cursor, cutting (yanking) and pasting the lines or words, as well as finding and replacing. In this mode, whatever you type is interpreted as a command.

 Insert mode − to insert text into the file. Everything that's typed in this mode is interpreted as input and placed in the file. 
vi always starts in the command mode. To enter text, you must be in the insert mode for which simply type i. To come out of the insert mode, press the Esc key, which will take you back to the command mode.
Shortcuts to the Editor

Esc+i  =To insert the text into the file

Esc+yy =To copy the single line

Esc+2yy = To copy the two lines, place the cursor at starting position

Esc+p =To Paste the copied text

Esc+dd = To delete the line

Esc+2dd = To delete the 2 lines.place the cursor at starting position.

Alt+u = Undo the changes.

Esc+Shift+wq: -To Save and quit

The command to quit out of vi is :q


[dhoni@server1 ~]$vi example

Hai this is example file.In this i am typing some info.

To view contents of any file

[dhoni@server1 ~]$more example

Shell Script:

The prompt, $, which is called the command prompt, is issued by the shell. While the prompt is displayed, you can type a command.Shell reads your input after you press Enter. It determines the command you want executed by looking at the first word of your input. A word is an unbroken set of characters.Spaces and tabs separate words.Following is a simple example of the date command, which displays the current date and time:
$date
Thu Jun 25 08:30:19 MST 2009


In Unix, there are two major types of shells:

 Bourne shell — If you are using a Bourne-type shell, the $ character is the default
prompt.

 C shell — If you are using a C-type shell, the % character is the default prompt.

Shell Scripts

The basic concept of a shell script is a list of commands, which are listed in the order of
execution. A good shell script will have comments, preceded by # sign, describing the
steps. we create a test.sh script. Note all the scripts would have the .sh extension.
Before you add anything else to your script, you need to alert the system that a shell script
is being started. This is done using the shebang construct. For example

#!/bin/sh
This tells the system that the commands that follow are to be executed by the Bourne
shell. It's called a shebang because the # symbol is called a hash, and the ! symbol is
called a bang. To create a script containing these commands, you put the shebang line first and then add the commands −

#!/bin/bash
pwd
ls

[dhoni@server1 ~]$ vi test.sh
#!/bin/bash
pwd
ls

Then give +x permission to the script. To make script excutable.

[dhoni@server1 ~]$ chmod +x test.sh


[dhoni@server1 ~]$ ./test.sh
/home/dhoni
add.sh  array.sh  read.sh  test2.sh  test.sh

If you face any problem while practicing feel free to comment it and Bookmark this blog for quick reference.We will try to help you.

If you like this share with your friends.Follow us by email for our new posts 

Thanks

Devops Desk Team


No comments:

Post a Comment