IL-Module 3
IL-Module 3
Module 3
Topics to be covered:
VI Editor: Introduction to Text Processing, Command & edit Mode
Invoking VI, deleting & inserting Line
Deleting & Replacing Character
Searching for Strings, Yanking,
Running Shell Command Macros
Set Window, Set Auto Indent, Set No.
Communicating with Other Users: who, mail, wall, send, mesg, ftp.
Shell Script: Shell types, shell command line processing.
Shell script features
Executing a shell script
System and user-defined variables, expr command, shell screen interface, read and echo
statement, command substitution, escape sequence characters, Conditional Control
Structures-if statement, case statement Looping Control Structure while, until, for,
statements. Jumping Control Structures – break, continue, exit.
Introduction:
No matter what work you do with a unix system, you will eventually write some C or Java
programs for shell scripts. You may also have to edit some of the system files at a times. For
all this you must learn to use an editor, Unix provides two very versatile ones—vi and emacs.
VI is the full screen editors now available with all UNIX system, and is widely acknowledged
as one of the most powerful editors available in any environment. It was created by a
graduate student Bill Joy later to become the Co founder of Sun Microsystems. VI made its
first appearance in BSD UNIX but is now standard on all UNIX systems.
Objectives:
Know the 3 modes of functioning.
Input and replace text and control characters.
Save your work recover from a crash and quit VI.
Understand the use of operator command and combinations to delete, move and copy
text.
Repeat and undo the last command.
Search for a string in a file and a character in a line.
The 3 modes:
When you open a file with VI, the cursor is positioned at the top left-hand corner of the
screen. You are set to be in the command mode. This is the mode where you pass commands
to act on text. Pressing a key does not show it on screen but make a function like moving the
cursor to the next line or deleting a line. You can’t use the command to enter or replace text.
There are two command mode functions that you should know at this stage. The role of the
space bar and the backspace key. This spacebar takes you one character ahead, while back
space or control h takes you a character back. Backspacing in this mode does not delete text
at all.
To enter text, you have to leave the command mode and enter the input mode. There are 10
keys which take you to this mode when pressed, and whatever you then enter shows up on
screen. Backspace in in this mode, erase all characters that the cursor passes through. To
leave this mode you can press escape key.
You have to save your work, leave VI editor or switch to editing another file. Sometimes Will
need to make a global substitution in the file. Neither of the two modes will quite do the work
for you. You have to use the last line mode or EX mode, where you enter the instruction in
the last line of the screen. Some common mode function also has last line mode equivalents
with this we can summarise that 3 modes in which vi works:
And if you specify an existing file, then the editor would open it for you to edit. Else,
you can create a new file.
Editing require that you be in command mode. Many of the editing commands have a
different function depending on whether they are typed as upper- or lowercase.
To move the cursor to another position, you must be in command mode. The cursor is
controlled with four keys: h, j, k, l.
h left one space
j down one line
k up one line
l right one space
Inserting Text
2. Type i
Replacing Words
2. Type cw
The last letter of the word to be replaced will turn into a $. You are now in insert
mode and may type the replacement. The new text does not need to be the same
length as the original.
Replacing Lines
To change text from the cursor position to the end of the line:
1. Type C (uppercase).
2. Type the replacement text.
3. Press <Esc>.
Deleting Characters
To delete a character from a file, move the cursor until it is on the incorrect character,
then type x
To remove four characters starting from the cursor position type 4x.
To delete the character before the cursor, type X (uppercase)
Deleting Words
To delete a word, move the cursor to the first letter of the word, and type dw
This command deletes the word and the space following it. To delete three words type
3dw
Deleting Lines
The cursor does not have to be at the beginning of the line. Typing dd deletes the
entire line containing the cursor and places the cursor at the start of the next line.
Text Movement (you can delete text from one place and put it at another place, i.e.
cut and paste)
Yanking (you can copy text from one place and put it at another place, i.e. copy and
paste)
Searching String
3. Press <Return>.
The cursor moves to the first occurrence of that text.
To repeat the search in a forward direction, type n
To repeat the search in a backward direction, type N
Joining Lines
Undoing
To undo your most recent editing, type u
To undo all the edits on a single line, type U (uppercase)
Undoing all edits on a single line only works as long as the cursor stays on that
line. Once you move the cursor off a line, you cannot use U to restore the line.
Repeating
To repeat the most recent editing, type . (dot).
With vi, you edit a copy of the file in the Buffer, rather than the original file. Changes
are made to the original only when you save your edits.
To save the file and quit vi, type ZZ . It doesn’t have any other command to do that.
However, the vi editor is built on an earlier Unix text editor called ex. ex commands
can be used within vi. ex commands begin with a : (colon) and end with a <Return>.
The command is displayed on the status line as you type. Some ex commands are
useful when saving and closing files.
To save the editing you have made, but leave vi running and your file open:
1. Press <Esc> to come to the Command Mode.
2. Type :w
3. Press <Return>.
To save the editing you have made, and quit vi use :wq or :x
To quit vi, and discard any changes your have made since last saving:
1. Press <Esc>.
2. Type :q!
3. Press <Return>.
Summary:
Command mode cursor-motion commands:
o j down
o k up
o l right
o h left
o $ move to end of current line
o ^ move to beginning of current line
o G go to bottom line
o nG go to line number n
Command mode screen-motion commands:
o ctr-d down
o ctr-u up
Command mode enter-insert-mode commands (once you are in insert mode you stay
there until you press esc):
o I insert here (before cursor)
o a insert here (after cursor)
o o insert a new line (below current line)
o O insert a new line(above current line)
Insert-mode commands:
o esc return to command mode
o ctr-u erase back line
o ctr-w erase back word
o ctr-h erase back character
Delete commands (from command mode):
o x delete character at cursor
o dd delete entire line
o D delete current to end of line
Copying lines:
o #yy yanks # lines, puts in buffer
o p inserts buffer after current line
Moving lines:
o #dd delete # lines (now in buffer)
o p inserts buffer after current line
Special commands:
o . repeat last command
o u undo last command
o J join next line to end of current line
o :set nu set line numbers on
o :set nonu set line numbers off
Several set commands are available which are suitable for writing programs and
preparing documents in vi editor. These commands work in ex mode.
Syntax: :set option
When the command option is prefixed with string no, it reverses the command.
Autoindent option provides indents to lines for easier readability. Commands are:
:set autoindent and :set noautoindent
Number option shows lines duly numbered that helps to debug programs. However,
the line numbers are not saved to the file. Commands are:
:set number and :set nonumber
Ignorecase option allows to ignore case while searching patterns in the file.
:set ignorecase and :set noignorecase
Showmode option displays a message when vi is in input mode
:set showmode and :set noshowmode
Tabstop sets tab for display (default tab space is eight characters), for example:
:set tabstop=12
Options to vi:
Vi has couple of options to be used while opening file.
The option -r is used to salvage/recover unsaved editing when crash occurred.
However, it does not guarantee complete retrieval.
Syntax: $ vi -r <filename>
The + option with a line number opens the file and positions the cursor at the first
word of that line number. For example: to start at line number 10
Syntax: $ vi +10 <filename>
The +/ option with a string as pattern opens the file and positions the cursor at the
line that matches the pattern.
Syntax: $ vi +/string <filename>
It also offers programming capability using which a user can control how and when
commands are to be executed.
Programs written in shell command language are called shell scripts. At its simplest
form a shell script is just a sequence of commands that have been saved in a file.
enhancements common to the later shells. It can still be useful for certain tasks such
as scripting, but almost all users will prefer one of the newer shells for entering
commands. ( part of AT&T version of Unix)
The C shell, csh, was the first attempt to enhance the original Bourne shell. The
syntax was strongly influenced by the C programming language. The C shell
introduced the concepts of a command history list, job control, and aliases. However,
like sh, it lacks some important features of later shells. A common complaint about
csh is that the new syntax is not compatible with the Bourne shell, and so some scripts
may not work properly in csh. ( part of BSD version of Unix)
The extended C shell, tcsh, has replaced csh entirely on some versions of UNIX
(including Linux). It retains all the features of csh and adds command-line editing (a
very important shell feature) and history completion. It is one of the more popular
shells, although like csh it has been criticized for not being compatible with the
Bourne shell.
The Korn shell, ksh, was developed at AT&T Bell Laboratories by David Korn.
Unlike the C shell, the Korn shell has a syntax compatible with sh. Like tcsh, ksh
includes a command history list, job control, aliases, and command-line editing.
The Bourne Again Shell, bash, is part of the GNU project. It extends ksh further,
while remaining compatible with the original Bourne shell syntax, and adds a few
features from tcsh as well. bash is the default shell in Linux and may be the most
popular shell today. On some systems, the command sh will run bash instead.
Comparison:
sh-Bourne Shell; csh-C Shell; ksh-Korn Shell; tcsh-Extended C Shell; bash-Bourne
Again Shell.
Using any text editor of your choice such as vi or gedit, create a new file hello.sh
with the following:
#!/bin/bash
# This is my First Script
echo "Hello World"
ls
date
Any line starting with #, will be treated as a comment line. An exception to this would be the
first line with #!/bin/bash
The echo command will print HelloWorld on the screen
The ls command will display directory content on the console
The date command will show the current date and time
File name extension .sh is not mandatory
Variables:
In Linux, there are two types of variable: system, and user-defined.
System variables - Created and maintained by Linux itself. This type of variables are
defined in CAPITAL LETTERS.
System variables are used in .profile file to alter operating environment of a user and
are also called environment variables.
To know the complete list give the following command:
$ set
To print a variable’s value, for example:
$ echo $HOME
User defined variables (UDV) - Created and maintained by user. This type of
variables are defined in lower case letters preferably. To define UDV use following
syntax
Syntax: variable_name=value
No spaces are permitted around the equal sign. Adding spaces will change the syntax
profoundly and generate error.
Bash variable are Untyped, basically character strings, but can be numeric also.
To print or access UDV use following syntax
Syntax: $variable_name
Examples:
x=10
echo $x
message="Hello World"
echo $message
Rules for Naming Variables
A variable name can contain only letters (a to z or A to Z), numbers ( 0 to 9)
or the underscore character ( _). Variables are case-sensitive.
Do not use ?,* and other special characters, to name your variable.
Double Quotes: Enclosing characters in double quotes (“ ”) are basically literals, with
the exception of ‘$’, ‘`’ ‘\’. These characters retain their special meaning within
double quotes.
Single Quotes: Enclosing characters in single quotes (‘ ’) are absolutely interpreted as
literal with no exception for special meaning of any character.
Back Quote: ‘`’ is used to execute a command
Back slash: ‘\’ is used to change the special meaning of the characters or to escape
special characters within the double quotation marks.
Examples:
echo “Your shell is: $SHELL”
echo ‘Your shell is: $SHELL’
echo “Today is `date`”
echo “Your shell is: \$SHELL”
Output with echo:
Display text or values of variables on the standard output using echo.
Syntax: echo [options] [string, variables...]
Options:
-n Do not output the newline character at the end
-e Enable back slash escape characters in the strings:
\n new line
\t horizontal tab
\v vertical tab
\\ backslash
\a alert (bell)
\b backspace
Examples:
echo -n “What is your name? ”
echo “My name is Jain”
echo -e “What is your name?\nMy name is Jain”
echo $PWD
echo $name
Example script:
1. #!/bin/bash
2. # A Script to create a 3 level nested directory
3. read -p “Enter 3 directory names to create: ” d1 d2 d3
4. mkdir -p $d1/$d2/$d3
5. echo -e “\nHere goes the structure:”
6. ls -R $d1
Command Substitution:
Command substitution allows the output of a command to replace the command itself.
Command substitution occurs when a command is enclosed as follows:
$(command) OR `command`
Command substitution is generally used to assign the output of a command to a
variable or display back using echo command:
var-name=$(command-name)
var-name=`command-name`
echo “$(command-name)”
echo “`command-name`”
Examples:
$ x=`date`
$ x=$(date)
$ echo “Today is $(date)”
$ echo “Today is `date`”
Command-line Arguments or Positional Parameters:
Command-line arguments are used to input data to a script. When you execute a
script, pre-defined shell variables are automatically set to match the arguments. These
variables are referred to as positional parameters.
The parameters $1, $2, $3, $4 (up to $9) refer to the first, second, third, fourth (and so
on) arguments on the command line. The parameter $0 is the name of the shell
program itself. The parameter $# is the total number of arguments passed to the script.
The parameter $* refers to all of the command-line arguments (does not include the
name of the script).
Example Script:
1. #!/bin/bash
2. #Script to test command-line arguments
3. echo “Script Name: $0”
4. echo “Total No of Arguments: $#”
5. echo “First Argument: $1”
6. echo “All Arguments Passed: $*”
How to run:
1. Create a script file and save this script.
2. Assign execute permission to the script file.
3. Run the script with some arguments.
Exit status:
In Linux, when a command is executed, it return two type of values: a zero (0) if the
command is executed successfully, or a nonzero (>0) if the command is not
successful.
This value is know as Exit Status of that command and is stored in a predefined
variable $?.
You can check this value to determine next course of action in the script.
For example:
In the previous script, you can check the exit status and if the command executes
successfully then you display the directory structure.
Arithmetic Operations
Using expr: The command takes a list of arguments, evaluates them, and prints the
result on standard output. Each term must be separated by spaces. The basic operators
+, −, *, /, and % can be used with expr.
For example: $ expr 1 + 2
You can use command substitution to assign the output from expr to a variable.
For example, you could increment the value of i with : $ i=1; i=`expr $i + 1`
Drawbacks of expr
The * must be escaped with a backslash to prevent the shell from interpreting it as an
asterisk (wild-card):
$ expr 4 \* 4
It can only be used for integer arithmetic. If you use a decimal argument, you get an
error, and it will truncate decimal results.
For example: $ expr 1.5 + 2.5 will result error and $ expr 7 / 2 will result 3
Using let: The let command is a better alternative to expr and provides integer
arithmetic. It accommodates several operators including +, -, *, /, %, and **. Here is a
simple use of let:
$ x=100
$ let y=2*(x+5)
$ echo $y
Note: the let automatically uses the value of a variable like x, no need to add a $ in
front of the variable name. But no decimal argument can be used. No space between
operands. No need of escape character for *
Using declare: We can declare a variable as an integer by using the declare command
and then assign the value of an arithmetic expression. No white space between terms
in the expression permitted.
$ declare –i var
$ var=(5+6)*3+(9-1)/2
$ echo $var
Using arithmetic expansion: We can use two different ways for evaluating
arithmetic expressions:
Syntax: $((expression))
Syntax: $[expression]
Examples:
$ echo $[(3+4)*5+9]
$ var=$[(3+4)*5+9]
$ echo $((5**3)
Example Script:
1. #!/bin/bash
2. echo "Enter first value"
3. read var1
4. echo “Enter second value”
5. read var2
6. echo `expr $var1 + $var2` # Arithmetic Addition
7. echo `expr $var1 - $var2` # Arithmetic Subtraction
8. echo `expr $var1 \* $var2` # Arithmetic Multiplication
9. echo `expr $var1 / $var2` # Arithmetic Division
10. echo `expr $var1 % $var2` # Arithmetic Modular Division
11. let p=var1**var2 # Use of let
12. echo “Power computation: $p”
13. sum=$(($var1+$var2)) # Use of Arithmetic Expansion
14. echo “Sum is $sum”
15. echo “Sum is $[$var1+$var2]” # Use of Arithmetic Expansion.
Operators:
Numeric Comparison Operators: Relational Operators that are specific to
numeric values and used for integer tests
Operator Description
arg1 -eq
True if arg1 equals arg2
arg2
arg1 -ne
True if arg1 is not equal to arg2
arg2
arg1 -lt
True if arg1 is less than arg2
arg2
arg1 -le
True if arg1 is less than or equal to arg2
arg2
arg1 -gt
True if arg1 is greater than arg2
arg2
arg1 -ge
True if arg1 is greater than or equal to arg2
arg2
Operator Description
File Operators: use to test files; Over 20 file operators are available in Bash Shell;
Important operators are listed.
Operator Description
-e filename True if the file exists; it can be empty or have some content
-s filename True if the file exists and has a size greater than zero
Logical Operators:
Operator Description
-o This is logical OR
If / If-else Command:
An if command tests whether a given condition is true. If it is, the commands within
the then block will be executed, otherwise the else block is executed. The condition
can be a logical expression or a shell command.
if command
then
command(s)
fi
Example Script:
# !/bin/bash
# Pass two directory names as command-line arguments.
if cp -r $1 $2
then
rm -rf $1
echo “Backup success!”
fi
How to run:
1. Create a script file.
2. Make it executable and run.
If the file passed as argument does not exist, the script execution stops and it exits to
theSchool
Jain shell. of CS and IT
Introduction to Linux 2020-2021
Case command:
If you need to compare a variable against several possible values, you can use a long
chain of if ... elif ... else statements. However, the case command provides a cleaner,
more readable alternate syntax for a multi-level if-else-fi commands. It also allows
you to compare a variable to a shell wildcard pattern, rather than to a specific value.
Syntax:
case $variable-name in
pattern1)
command(s) ;;
pattern2)
command(s) ;;
.
.
patternn)
command(s) ;;
*)
command(s) ;;
esac
1. The $variable-name is compared against the patterns until a match is found. The shell
then executes all the statements up to the double semicolons (;;) at which point the
case statement terminates.
2. If the value of $variable-name does not match any of the patterns, the program goes
through the entire case statement.
3. The default is *) and it is executed if no match is found.
Example Script:
#!/bin/bash
month=$(date +%m)
case $month in
02)
echo "February usually has 28 days."
;;
04|06|09|11)
echo "The current month has 30 days."
;;
*)
echo "The current month has 31 days."
;;
Esac
For loop:
1. The for loop takes a list of items to iterate through until it reaches the end.
2. The list of items has to be written out explicitly in the script itself .
3. Also, it can iterate through the content of a variable, pattern or even through the
output of other commands execution.
4. If you omit the in list portion of the for loop, the value of $* will be used instead. You
could use this to iterate through the command-line arguments to a script.
The for loop executes a block of commands once for each member of a list. The
syntax is:
Syntax:
for i in list
do
command(s)
done
Example 1:
#The script runs touch command to create empty files
for x in 1 2 3 4 5 6 7 8 9
do
touch file$x.txt
done
While loop:
The while loop keeps on executing the commands in the body of the loop while the
condition that it tests for remains true. The condition is usually a logical test. The
syntax of the while loop is:
Syntax:
while condition
do
command(s)
done
Example 1:
1. #!/bin/bash
2. #Simple counting using while loop
3. i=1
4. while [ $i -le 10 ]
5. do
6. echo $i
7. i=`expr $i + 1`
8. done
until loop:
The until loop is exactly like the while loop but the test is negated. The syntax of the until
loop is:
Syntax:
until condition
do
command(s)
done
Example 1:
1. #!/bin/bash
2. #Infinite loop terminates when user enters zero
3. while true
4. do
Example 3:
1. #!/bin/bash
2. #Script prints only executable script files and skips others
3. for file in *.sh
4. do
5. if ! [ -x $file ]
6. then
7. continue
8. fi
9. echo $file
10. done
select Loop:
The select command is a very useful tool for menus design. It originally comes from
the Korn shell, but is also found in bash.
It displays a numbered list of items, displays a prompt, and waits for input. Once the
selection is processed, the user is prompted for input again. It continuously loops
around until the loop ends.
One interesting aspect of the select loop is that it has no conditional test at all; the
only way to end the loop is to use break or exit command.
The variable PS3 is used to set the prompt for input. It also sets $REPLY to the menu
option entered by the user. If an invalid option is typed in by the user, the variable is
not set.
If the user presses ENTER without making a selection, the list of items is displayed
again.
A case statement is used in the script to execute the chosen menu option.
Syntax:
PS3=“prompting-text”
select var in item1 item2 item3
do
commands
done
Example Script:
1. #!/bin/bash
2. #Simple menu using select loop
3. PS3=“Enter your choice: ”
4. echo “MENU”
5. select item in “File Copy” “File Move” “Quit Menu”
6. do
7. case $REPLY in
8. 1)
9. read -p “Enter source file: ” source
10. read -p “Enter destination: ” destination
11. if cp $source $destination
12. then
13. echo “File successfully copied …”
14. else
15. echo “Copy failed!”
16. fi
17. ;;
18. 2)
19. read -p “Enter source file: ” source
20. read -p “Enter destination: ” destination
21. if mv $source $destination
22. then
23. echo “File successfully moved …”
24. else
25. echo “Move failed!”
26. fi
27. ;;
28. 3)
29. echo “Thanks for using the script!”
30. break
31. ;;
32. *)
33. echo “Invalid choice!”
34. ;;
35. esac
36. done