UNIX

Unix Shell Scripting

shell programming - Unix Shell Scripting

Shell Script:

Shell script in Unix
Unix Shell Scripting

The commands are used on command prompt and generate instant output but Linux system allows us a method to define set of command together as programming instructions that are known as the script. The system executes the scripting commands line by line and generates output. A sequence of shell command stored in a file, the name of which is better use to execute stored sequence known as the shell script. It provides built-in functions and commands to control the user instructions.

1. A simple shell script:

A) Sh command:

It is a command that allows executing the shell script. In this, we pass scripting file for execution.

Syntax:

sh_<file name>.sh

Example:

sh anna.sh↵

B) Shell-variable:

A variable that contains the value as temporary for data processing. Shell variable process similar to a general variable.

Example:

vim anna.sh

a=10

echo”value is”$a↵

2. Initialization File:

Linux system manages the configure files for maintaining system configuration dot(.)profile and dot(.)re are the file that maintains the initialization levels of the system and separates with a dot(.). It also controls environmental variable and its values.

3. Dot Command:

It is a command that also executes shell scripting file without creating the new shell and generates output.

Syntax:

.<file name>.sh

Example:

.anna.sh↵


Hello

Hi

Bye


4. Command Line Argument:

We use instruction in the file and execute in a particular sequence. The user can pass value from the command prompt that automatically identifies dash $1, $2, $x respectively.

Example:

Vim anna.sh


echo $1

echo $2

echo $3

:wq↵

.anna.sh 10 20 30

A) read:

It is a command that allows to get the input through the user and asking on a particular variable.

vim anna.sh

example:

echo”Enter value”

read a

echo “Input value is”$a

:wq↵

sh anna.sh

B) #:

It is a symbol that allows defining comment inside the program.

Example:

Vim anna.sh

echo “Enter value”

read a# input process

echo”Input value is”$a

:wq↵

sh anna.sh

5. Operator:

It is a token that allows to manage the conditions and return logical value and its related statement.

A) &&:

It is an operator that executes the first statement from the given statement from the given instruction.

B) ||:

It is a command just opposite of and that use when we want to execute the second command only the first command gets failed.

Example:

vim anna.sh↵

.grep ‘anna’ abc && echo”Data found”.

.grep ‘anna’ abc || echo”Data not found”.

6. Conditional Statement:

Unix system allow us a method to check the condition and return logical value.

A) If statement:

It is a conditional statement that returns logical value like true, false and executes the instruction when the condition gets true.

Syntax:

If[condition]

then

statement

fi

Example:

echo"Enter value" 
read a 
echo"Enter value"
read b
if[$a-gt$b]
then
echo"a is largest"
fi
if[$b-gt$a]
then 
echo"b is largest"
fi
B) If else statement:

It is a conditional statement that creates the condition and executes statement when the condition gets a true or false statement.

Syntax:

If[condition]

then

statement

else

statement

fi

Example:

echo"Enter value"
read a
echo"Enter value"
read b
if[$a-gt$b]
then 
echo"A is largest"
else 
echo"B is largest"
fi
C) Ladder if:

It is a conditional statement that checks the condition with multiple statements & more than one conditions. It terminates the processing when the condition gets true otherwise switch at another condition.

Syntax:

If[condition]

then

statement

else if[condition]

then

statement

else

statement

fi

fi

Example:

echo"Enter value"
read a
echo"Enter value"
read b
if[$a-gt$b]
then
echo"a is largest"
else if[$b-gt$a]
then
echo"b is largest"
else
echo"Both are equals"
fi
fi
D) Nested if:

It is a way to arrange the conditional statement into an inner or outer format as nested. The inner condition control by its outer condition.

Syntax:

If[condition]

then

if[condition]

then

statement

else

statement

fi

else

statement

fi

Example:

echo"enter value"
read a
echo"enter value"
read b
if[$a-eq5]
then
if[$b-eq6]
then
echo"welcome"
else
echo"B wrong"
fi
else
echo"A wrong"
fi

Test Command:

It is a command that similar to if statement. It means we can use if or test for checking the condition.

Syntax:

test[condition]

statement

Example:

echo”enter value”

read a

test $a-gt$b

echo”A is largest”

7. Case Statement:

It is also a conditional statement that checks the value and executes a statement. We use (; 😉 to terminate the case statement (*) for the difficult statement.

Syntax:

case var in

value statement;;

value statement;;

*) statement;;

Example:

echo “enter value”

read a

case $a in

  1. echo”one”;;
  2. echo”two”;;
  3. echo”Wrong Choice”;;

esac

8. String Handling Using expr:

Shell programming allow us mathematical calculations like addition, subtraction, etc using expr command and define into ‘ ‘()(*) these symbol and control as the string.

Example:

echo”enter value”

read a

echo”enter value”

read b

c=”expr $a + $b”

echo”sum is “$c

9. Looping statement:

It is a statement that allows executing the instructions more than one time on the base of given condition. In this we maintain starting of the loop, ending with conditions and step of the loop. It reduces the programming code and increases the efficiency of the program.

A) For loop:

It is a type of loop that allows defining the looping conditions in same instructions and executes the instruction after checking the conditions with for command.

Syntax:

for(starting of a loop; ending with a condition; the step of a loop)

do statement

done

Example:

echo”enter limit”

read a

for(b=1;b<=10;b++)

do

c=”expr $a\*$b”

echo$c

done

B) while loop:

It is a type of loop that executes the statement after checking the condition. It is similar to for loop but the looping statement are define in multiple instructions, the condition check with while keyword.

Syntax:

starting loop

while[condition}

do

statement

the step of the loop

done

Example:

echo”enter limit”

read a

b=-1

while[$b-le$a]

do

echo$b

a=”expr $b+1″

done

C) Nested loop:

It is a process for arranging the looping statement into the inner & outer format as nested.

Example:

for((i=1;i<10;i++))

do

for((j=2;j<=5;j++))

do

c=”expr $ i\* $j”

echo-n$c

done

echo

done

 

-n:

It is an option that displays the contents of the same line.

10. Positional Parameter:

Ls allows us a method to manage the multiple values in different parameter and access with $1, @$2, $n respectively.

A) Set:

It is a command that set the positional parameter of the given value.

example:

set anna Kanna manna

echo $1 $2 $3

shift

echo $1 $2

shift

echo $1

-set who(user time)

echo $1

set is-l

echo $1 $2 $n

11. Shell Function:

A function is a way to divide a program into the different module and each module content set of instructions. It is basically used to reuse the instructions.

Syntax:

sum()

{

echo”hello”

}

sub()

{

echo”bye”

Example:

echo”enter value”

read a

if[$a-eq1]

sum

else

sub

fi

12. Interrupt Handling:

The data are processed at program execution time, the system generates error at compilation time that is known at compile time error. The system also generates the error at program execution time due to lack of resources, memory overflow etc. The system gets interrupted at program running time using ctrl+d or kills command and it generates a signal then sent to the operating system for controlling. The system use command list which gets interrupted and sent signal.

A) Trap

It is a command that allows getting the signal which is generated at execution time due to interrupt.

Example:

Trap↵

13. Call and Bash shell feature:

The call & bash shell provide the particular command to manage the values and its processing.

A) let:

It is a command that allows to assign the value on a particular variable with assignment operator and also execute the Arithmetic operation.

let a=5

let a=10+5

let a+=20

let a*=5

B) Array:

It is a process to store multiple info at the different index on the particular variable. We can access the data with index position. We can set command to define the array.

Set v=(1,anna.100)

echo v[2].

About the author

Parichay

Add Comment

Click here to post a comment