Showing posts with label Scripting. Show all posts
Showing posts with label Scripting. Show all posts

Monday, 17 April 2017

All about the VI editor

The default editor that comes with the UNIX operating system is called vi (visual editor). It has two modes of operation:

1. Command mode commands which cause action to be taken on the file.
In the command mode, every single character typed is a command that performs operation on the text file being edited.

2. Insert mode in which entered text is inserted into the file.
In the insert mode, every character typed is added to the text in the file; pressing the Escape key turns off the Insert mode.

Syntax
vi file_name
It will open an empty file if not present already.

Commands that switch the editor to insert mode
a
It moves the cursor one position to the right before switching to insert mode, basically it will append
i
Insert text at current cursor position.
I
Insert text at the beginning of the current line.
o
Insert a blank line under the current cursor position and move the cursor to that line.
O
Insert a blank line above the current cursor position and move the cursor to that line.

Basic operations
Some of the popular vi commands are:

Moving through the text
h move the cursor to the left
l move the cursor to the right
k move the cursor up
j
move the cursor down
H
moves the cursor to the top line of the screen
M
moves the cursor to the middle line of the screen
L
moves the cursor to the last line of the screen
w
move the cursor forward one word
b
move the cursor backward one word (if you are in the middle of a word, b command will move you to the beginning of the current word)
e
move to the end of a word
(
move to beginning of current block
)
move to beginning of current block
0
move to the beginning of a line
$
move to the end of a line

Screen Movement
Ctrl-f
scrolls down one screen
Ctrl-b
scrolls up one screen
Ctrl-u
scrolls up half a screen
Ctrl-d
scrolls down a half a screen
SHIFT-G
put the prompt at the end of the file, Preceding G with a number will move you to a specific line in the file
1G
move to the beginning of the file

Delete
dw
deletes from the character selected to the end of the word
ndw
deletes n words at the right side of the cursor
dd
deletes the current line
ndd
deletes n lines starting from the current cursor position
D
deletes from the current character to the end of the line
x
deletes the character on which the cursor is positioned
nx
delete the character selected and the next (n-1) characters
X
deletes the character on left of your cursor
nX
deletes n character on left of your cursor
$-9,$d
deletes last ten lines

Copy, Paste, Replacement | Join and Undo
yw
copies a word into a buffer
nyw
copies n word into a buffer
yy
copies a line into a buffer
nyy
copies n line into a buffer
p
paste line or words copied earlier on the line below the cursor
p
paste line or words copied earlier on the line above the cursor
r
replaces the current character with the next character you type and returns to command mode
cw
Changes and replaces the current word with text that you type. Press ESC to get out of replacement mode
:1,$s/word/newword/g
word with newword throughout the file

J join the line on which cursor is placed with another line
nJ join n lines
:u undoes the last change you made in the file. Using it again will "undo the undo"
U undoes all recent changes to the current line


Display line number
:set number shows the line numbers
:n moves to line n of the file
:.= returns line number of current line at bottom of screen
:= returns the total number of lines at bottom of screen
^g provides the current line number, along with the total number of lines, in the file at the bottom of the screen

Search
/pattern search the string(pattern) in the file and position the cursor on the first match below its position
n search in a forward direction
N search in a backwards direction
?pattern search backward for occurrence of the pattern

Save and Quit
:w save the file
:q exit the editor
:q! forces the exit when you want to quit a file containing unsaved changes
:e! reads the original file back in so that you can start over
:wq save and exit
ZZ save file and exit VI
:w newfile
save the text to newfile
:wq! overrides read-only permission
:1,100w newfile write lines 1 to 100 to newfile
:$-9,$w newfile write the last ten lines to newfile (the dollar sign denotes last line)


Some more commands
:recover recover a file after an unexpected interruption
vi –r filename
R puts you in overtype mode until you press ESC
. Repeats the last command executed
:! command
execute external command from VI
:sh start a shell from VI, Return to VI by entering exit or ctrl d
:sp horizontally split current file
:vsp
vertically split current file
:r! command insert output of command to the file

Sunday, 30 October 2016

script to print fibonacci series

echo "Enter n for Fibonacci series: - "
read n
echo "Fibonacci series is: - "
echo "0"
echo "1"
i=0
j=1
cnt=2
while [ $cnt -le $n ]
do
k=`expr $i + $j`
i=$j
j=$k
echo $k
cnt=`expr $cnt + 1`
done

$ ./fibonacci.ksh
Enter n for Fibonacci series: -
10
Fibonacci series is: -
0
1
1
2
3
5
8
13
21
34
55

script to print file type in a directory

for filename in `ls -l`
do          
if [ -d $filename ]
    then
           echo $filename   it is a directory
                    elif [  -h $filename ]
                          then
                     echo $filename  it is a link
                     elif [ -f  $filename ]
                           then
                      echo $filename it is a file
fi
done

$ ./filetypecheck.ksh
filetypecheck.ksh it is a file
flashpoint it is a directory
new it is a file
new1 it is a file
newres it is a file
newrestu it is a file
newworld it is a file
test it is a directory

$ ls -l
total 12
-rwxr-xr-x 1 flash  363 Oct 29 23:31 filetypecheck.ksh*
drwxrwxr-x 2 flash4096 Oct 29 23:33 flashpoint/
-rw-rw-r-- 1 flash   0 Oct 29 23:22 new
-rw-rw-r-- 1 flash   0 Oct 29 23:22 new1
-rw-rw-r-- 1 flash   0 Oct 29 23:22 newres
-rw-rw-r-- 1 flash   0 Oct 29 23:22 newrestu
-rw-rw-r-- 1 flash   0 Oct 29  2016 newworld

drwxrwxr-x 2 flash 4096 Oct 29 23:22 test/

Monday, 24 October 2016

script to print zero to ten number

#!/bin/bash
num=0
while [ "$num" -lt 10 ];
do
echo "$num"
num=$((num+1))
done

In the above script we have first assigned zero value to variable num.
Then we are using while loop to iterate until the condition specified is valid,
That is it will continue to the operation till the num value is less than Ten.
do will perform the task specified.
echo will print the current value of num.


Output
0
1
2
3
4
5
6
7
8
9

script to find prime number

#!/bin/bash
echo -n "Enter a number: "
readnum
expr $num + 1  &> /dev/null

if [ $? -ne 0 ]
then
echo "You did not supplied number value"
exit 1
fi
[ $num -lt 2 ] && echo "Values < 2 are not prime numbers" && exit 1

i=2
while [ $i -lt $num ]
do
if [ `expr $num % $i` -eq 0 ]
then
echo "$num is not a prime number"
echo "Since it is divisible by $i"
exit
fi
i=`expr $i + 1`
done
echo "$num is a prime number "

$ ./primecheck
Enter a number: 17
17 is a prime number

Sunday, 23 October 2016

Script to delete files from particular path

$ vi delete
#!/bin/ksh
LOGFILE=/home/ flash /logs/delete.log
touch $LOGFILE
echo " ------Starting Time `date` --------------- ">>$LOGFILE
cd /home/flash/enemy/bad
echo " ------FILE COUNT-----">>$LOGFILE
ls -lR /home/flash/enemy/bad/* | wc -l >>$LOGFILE
ls -ltr *.bad | awk '{print $9}' | xargs \rm

·        Here delete is the script name.
·        Firstly we have created a LOGFILE variable to create logs and defined the path where the log is to be created.
·        Touch command will create and empty file.
·        Echo command output will be redirected (>>) to LOGFILE in which current date stamp is added.
·        When you want to print output of any command inside echo command use : `  at the beginning and end of command.
E.g.: `date`
·        cd will change directory and then write the current count of files into LOGFILE by wc-l command
ls -lR /home/flash/enemy/bad/* | wc -l >>$LOGFILE
·        Here * means alll the types of file. We can specify type of extension to delete specific file.
E.g.: *.bad, *.txt
·        Last command is to remove files from the directory.
·        Then chmod 755 to change permissions of the file.
·        Now  run the script by ./scriptname and will remove the files.