vi Editor, Shell Variables and Shell Scripting I/O for Linux

vi Editor: Modes and Commands

The vi editor is a powerful and commonly used text editor in Linux and Unix systems. It is mainly used to create, edit, and manage text files such as configuration files, shell scripts, and program source code. vi works in different modes, which makes it efficient but slightly difficult for beginners.

Modes of the vi Editor

vi editor works in three main modes:

Command Mode

  • Default mode when vi starts.
  • Used for navigation, deleting text, copying, and saving files.
  • Example commands:
    • dd — delete a line
    • yy — copy a line
    • p — paste

Insert Mode

  • Used to insert or edit text.
  • Entered by pressing: i, a, o.
  • Exit insert mode by pressing Esc.

Last Line (Ex) Mode

  • Used for file operations like save, exit, and search.
  • Entered by pressing :.
  • Example commands:
    • :w — save file
    • :q — quit
    • :wq — save and quit

Starting vi Editor

To open or create a file using vi:

$ vi filename

Basic Editing Commands

Common commands:

  • i — insert before cursor
  • dd — delete a line
  • a — append after cursor
  • x — delete a character
  • u — undo last change

Navigation Commands

  • h — left
  • l — right
  • k — up
  • j — down
  • G — go to end of file
  • gg — go to beginning

Search and Replace

  • Search a word: /word
  • Replace a word: :%s/old/new/g

Saving and Exiting

  • Save file: :w
  • Exit without saving: :q!
  • Save and exit: :wq

Advantages of vi Editor

  • Very fast and lightweight
  • Available on almost all Linux systems
  • Powerful editing and automation features
  • Ideal for system administrators and programmers

Shell Variables: Definition and Usage

Shell variables are used to store data temporarily in the shell so that it can be reused during program execution. They help in controlling shell scripts, storing values, and passing information between commands. Shell variables are an important part of shell programming.

Definition of Shell Variable

A shell variable is a named memory location that stores values such as numbers, strings, or command output. Shell variables do not need a data type declaration. Example: name=Rohit

Rules for Defining Shell Variables

  • No space is allowed around the = sign.
  • Variable name should start with a letter or underscore.
  • Variables are case-sensitive.
  • Example: count=10 TOTAL=50

Accessing Shell Variables

To access the value of a variable, use the $ symbol.

Example: echo $name

Types of Shell Variables

(a) Local Variables

  • Defined inside the shell or script.
  • Available only in the current shell.
  • Example: x=5

(b) Environment Variables

  • Available to all child processes.
  • Created using export.
  • Example: export PATH

(c) System Variables

  • Predefined by the system.
  • Examples: $HOME $USER $SHELL $PWD

Special Shell Variables

Linux provides special variables with predefined meanings:

  • $0 — script name
  • $1, $2 ... — command line arguments
  • $# — number of arguments
  • $* — all arguments
  • $? — exit status of last command
  • $$ — process ID of the shell

Read Command for Variables

User input can be stored using read:

read name
echo $name

Advantages of Shell Variables

  • Makes scripts flexible
  • Reduces repetition
  • Helps in decision making
  • Easy data handling

Control Structures in Shell Scripting

Control structures in shell scripting are used to control the flow of execution of a program. They allow the script to make decisions, repeat commands, and choose different paths based on conditions. Control structures make shell scripts flexible and powerful.

Types of Control Structures

Shell scripting mainly supports three types of control structures:

  • Decision making
  • Looping
  • Case selection

Decision Making Structures

if Statement

Used to execute commands when a condition is true.

Syntax:
if [ condition ]
then
  commands
fi

Example:
if [ $a -gt $b ]
then
  echo "a is greater"
fi

if–else Statement

Used when two conditions are involved.

if [ condition ]
then
  commands
else
  commands
fi

if–elif–else Statement

Used to check multiple conditions.

if [ condition1 ]
then
  commands
elif [ condition2 ]
then
  commands
else
  commands
fi

Looping Structures

Loops execute a set of commands repeatedly.

for Loop

Used when the number of repetitions is known.

for i in 1 2 3
do
  echo $i
done

while Loop

Executes while a condition is true.

while [ $a -lt 10 ]
do
  echo $a
  a=`expr $a + 1`
done

until Loop

Executes until a condition becomes true.

until [ $a -eq 5 ]
do
  echo $a
  a=`expr $a + 1`
done

Case Statement

Used to select one option from many.

case $choice in
  1) echo "One" ;;
  2) echo "Two" ;;
  *) echo "Invalid" ;;
esac

Loop Control Statements

  • break — exits loop
  • continue — skips current iteration

Importance of Control Structures

  • Controls program flow
  • Enables decision making
  • Reduces code repetition

Shell Input and Output (I/O)

Input and output (I/O) in the shell refers to the way a shell script takes input, displays output, and handles data flow between commands and files. The Linux shell provides powerful mechanisms like standard streams, redirection, and pipes to manage I/O efficiently.

Standard I/O Streams

Linux shell uses three standard streams:

Standard Input (stdin)

  • Used to take input from keyboard or file.
  • Represented by file descriptor 0.

Standard Output (stdout)

  • Used to display normal output on screen.
  • Represented by file descriptor 1.

Standard Error (stderr)

  • Used to display error messages.
  • Represented by file descriptor 2.

Input Redirection

Input redirection takes input from a file instead of the keyboard.

Example: $ wc -l < file.txt

Output Redirection

Used to send output to a file.

  • Overwrite file: $ ls > output.txt
  • Append to file: $ ls >> output.txt

Error Redirection

Used to redirect error messages.

Example: $ command 2> error.txt

Combined Output and Error Redirection

Redirect both output and error:

Example: $ command > all.txt 2>&1

Pipes (|)

Pipes send the output of one command as input to another.

Example: $ ls | wc -l

Read and Echo Commands

read — takes input from user

echo — displays output

Example:

read name
echo "Hello $name "