ShellScript
The below script uses variable to read the input from command prompt and display it.Here echo is print statement. Used to print the text. Comments will be written using #. Comments are not excutable .while reading a variable $ symbol is not required but when printing $ symbol is required.
[dhoni@server1 ~]$ vi read.sh
vi read.sh
#!/bin/bash
echo "Who are you give your name"
read person
echo Hai "$person"
[dhoni@server1 ~]$ ./test2.sh
Who are you give your name
kiran(prompt asks input from the keyboard)
Hai kiran
For every arithamatic operation we should write expr in linux. expr should be written inside ``(accent graves).and we should give space between each variable and operator also.
This helps assign a value to one of its indices.
array_name[index]=value
Here array_name is the name of the array, index is the index of the item
Accessing Array Values
After you have set any array variable, you access it as follows −
${array_name[index]}
The below script uses variable to read the input from command prompt and display it.Here echo is print statement. Used to print the text. Comments will be written using #. Comments are not excutable .while reading a variable $ symbol is not required but when printing $ symbol is required.
[dhoni@server1 ~]$ vi read.sh
vi read.sh
#!/bin/bash
echo "Who are you give your name"
read person
echo Hai "$person"
[dhoni@server1 ~]$ chmod +x test2.sh
[dhoni@server1 ~]$ ./test2.sh
Who are you give your name
kiran(prompt asks input from the keyboard)
Hai kiran
------------------------------------------------------------------------------------
-> Script to read the varible and display it. Here first i give sachin for name variable then i use sehwag for the name variable. Sachin name is override by sehwag.
[dhoni@server1 ~]$ vi read1.sh
#!/bin/bash
echo "The name is"
Name="Sachin"
echo "$Name"
Name="Sehwag"
echo "$Name"
[dhoni@server1 ~]$ chmod +x read1.sh
[dhoni@server1 ~]$ ./read1.sh
The name is
Sachin
Sehwag
-----------------------------------------------------------------------------------------------
Readonly variables
--> script to read the variables.Here we use readonly variable. once value intialized the value can not be changed entire the script.First sachin is intialized to name then trying to intialize sehwag to the same variable. so it shows error becuase Name is readonly variable
[dhoni@server1 ~]$ vi read2.sh
#!/bin/bash
echo "The name is"
Name="Sachin"
readonly Name
echo "$Name"
Name="Sehwag"
echo "$Name"
[dhoni@server1 ~]$ chmod +x read2.sh
[dhoni@server1 ~]$ ./read2.sh
The name is
Sachin
./read.sh: line 6: Name: readonly variable
Sachin
----------------------------------------------------------------------------------------
-->Script to add the numbers using arithamatic operators.It will read two numbers from the command prompt and it will do following operations.To do arithamatic operations we use a program called expr in linux .
echo "Addition of $a+$b is" `expr $a + $b`
in above line echo for printing and
`expr $a +$b`
echo "Addition of $a+$b is" `expr $a + $b`
in above line echo for printing and
`expr $a +$b`
For every arithamatic operation we should write expr in linux. expr should be written inside ``(accent graves).and we should give space between each variable and operator also.
[dhoni@server1 ~]$ vi add.sh
#!/bin/bash
echo "Enter The numbers to do Arithamatic Operations "
read a
read b
#c=`expr $a + $b`
echo "Addition of $a+$b is" `expr $a + $b`
echo "Substraction is $a-$b is " `expr $a - $b`
echo "Multiplication is $a*$b is " `expr $a \* $b`
echo "Division is $a/$b is " `expr $a / $b`
echo "Modular is $a%$b is " `expr $a % $b`
[dhoni@server1 ~]$ chmod +x add.sh
[dhoni@server1 ~]$ ./add.sh
Enter The numbers to do Arithamatic Operations
6
3
Addition of 6+3 is 9
Substraction is 6-3 is 3
Multiplication is 6*3 is 18
Division is 6/3 is 2
Modular is 6%3 is 0
--------------------------------------------------------------------------------
-->Script to demonstrate array.
Array is used to store large amount of data of same data type. Array values can accessed using index value.index value starts from ''0".To acces array we should use index number as follow.
This helps assign a value to one of its indices.
array_name[index]=value
Here array_name is the name of the array, index is the index of the item
Accessing Array Values
After you have set any array variable, you access it as follows −
${array_name[index]}
Below example having array called name.it contains 4 values.To display array contents we use
echo ${name[3]} --> Displays index 3 value
To display all index values we can use any one from below
echo ${name[*]}
echo ${name[@]}
[dhoni@server1 ~]$vi array.sh
#!/bin/bash
name[0]="a"
name[1]="b"
name[2]="c"
name[3]="d"
echo ${name[3]}
echo ${name[*]}
echo ${name[@]}
[dhoni@server1 ~]$ chmod +x array.sh
[dhoni@server1 ~]$ ./array.sh
d
a b c d
a b c d
If you face any problem while practicing feel free to comment it and Bookmark this blog for quick reference.We will try to help you.
If you like this share with your friends.Follow us by email for our new posts
Thanks
Devops Desk Team
No comments:
Post a Comment