Shell Scripting: -
Part – 1:
Q) Why we go for shell scripting ?
Ans – In Unix command prompt, we
can run a single command at a time. To combine and run multiple command for
performing certain tasks, shell script is evolved.
Q) What is shebang or Hash line or
magic line or dang line ?
Ans – The 1st line of
the shell script is called hash line or shebang or magic line or dang line.
It is written as below.
For default shell,
#! /bin/sh
For K – Shell,
#! /bin/ksh
Q) Which command is used in shell
script to print output in screen ?
Ans – echo command
Q) Print ‘Hello World’ in shell
script ?
Ans –
#! /bin/sh
echo “Hello World”
Q) How to run a shell script ?
Ans – There are two ways to run a
shell script. Such as –
1. 1st Way –
$ vi script1.sh<Enter – Key>
#! /bin/sh
echo “Hello World”
Esc + :wq<Enter – Key>
$ chmod 755 script1.sh<Enter –
Key>
$ ./script1.sh<Enter – Key>
Hello World
2. 2nd Way –
#! /bin/sh
echo “Hello World”
Esc + :wq<Enter – Key>
$ sh script1.sh<Enter – Key>
For K – shell, $ ksh script1.ksh<Enter
– Key>
Q) How to comment lines present in
shell script ?
Ans – By inserting single ‘#’ or
multiple ‘#’ in the beginning of the line make the line comment.
Example –
$ vi script2.ksh
#! /bin/ksh
###echo “Hello World” – Commented line
echo “Time Begins” – Uncommented
line
###echo “Social Activity” –
Commented line
ESC + :wq<Enter – Key>
$ ksh script2.ksh<Enter –
Key>
Output – Time Begins
Q) What are variables in shell script
?
Ans – There are two types of shell
script variable. Such as –
1. Scalar Variable – This Variable
holds data of any data type. It stores only a single value at a time.
Example –
$ vi script3.ksh
#! /bin/ksh
i=5
echo “i value = $i”
i=4.5
echo “i value = $i”
i=’India’
echo “I value = $i”
i=’you are a good boy’
echo “i value = $i”
Esc + :wq<Enter – Key>
$ ksh script3.ksh<Enter –
Key>
Output –
i value = 5
i value = 4.5
i value = India
i value = you are a good boy
2. Array – To store multiple values
of different datatype in continuous memory location, we go for array.
Q) What are ways to read input from
command line ?
Ans – There are two ways to read an
input from command line. Such as –
1. 1st Way –
$ vi script4.sh
#! /bin/sh
echo “Enter values of a and b: “
read a b
echo “Value of a = $a and Value of
b = $b”
Esc + :wq<Enter – Key>
$ sh script4.sh<Enter – Key>
Output –
Enter values of a and b:
23 13
Value of a = 23 and Value of b = 13
2. 2nd Way –
$ vi script5.sh
#! /bin/sh
echo “Value of a = $1”
echo “Value of b = $2”
echo “Value of c = $3”
Esc + :wq<Enter – Key>
$ sh script5.sh 43 23 67<Enter –
Key>
Output –
Value of a = 43
Value of b = 23
Value of c = 67
Or
$ chmod 755 script5.sh<Enter –
Key>
$./script5.sh 43 23 67<Enter –
Key>
Output –
Value of a = 43
Value of b = 23
Value of c = 67
Q) What is the output of below
script ?
#! /bin/sh
i=5
j=7
echo “Value of I = $i and j = $j”
echo ‘Value of I = $i and j = $j”
Ans –
Value of I = 5 and j = 7
Value of I = $i and j = $j
Q) Which variable is used in shell
script to keep the value of the variable constant throughout the script?
Ans – readonly variable
Example –
#! /bin/sh
a=78
echo “Value of a = $a”
readonly a
a=56
echo “Value of a = $a”
Output –
Value of a = 78
Value of a = 78
No comments:
Post a Comment