Shell scripting is a file which
contains the list of commands for executing. What is Shell? Shell is
special program or application provided for the user interaction.
firstscript.sh:
There are different kinds of shells in
Unix/Linux, they are
- Sh: Bourne Shell
- Csh: C Shell
- Ksh: Korn Shell
- Bash: Bourne again Shell.. etc
Some of the advantages of the shell
scripting are,
- Automation of tasks such as backups,log monitoring etc
- To do repetition of of tasks
- Save lots of time..
Unix/Linux Shell scripting also
containg the functions, control statements,loops like other
programming languages C, C++...
- To create a variable, we use the following syntax,
variablename=value
Note: there is no semicolon(;) at the end of each line like other languages
Note: there is no semicolon(;) at the end of each line like other languages
variable name can contain alphabet,
digits.Remember Spaces and special characters are not allowed except
underscore( _ ) like in other programming languages C,C++.
- Comments in the shell script are start with # symbol
- To retrieve the value of a variable use $ symbol
- To do arithmetic operations use ((expression)) syntax
To start shell scripting program, we need any text editor like vi, vim etc.
Here is the simple Linux Shell
Scripting program.
firstscript.sh:
#!/bin/bash
# variable declaration
str="Hello World"
#echo command used to display text
echo "$str"
echo "From DevelopersFactory"
To execute the shell script we should
use any of the following methods,
First Method
2nd Method
- Give executable permissions to the file
- Then execute it
2nd Method
- $sh filename or
Output:
$chmod 0755 firstscript.sh
$./firstscript.sh
Hello World
From DevelopersFactory
Hello World
From DevelopersFactory
- The following code snippet will describe arithmetic operations in shell script
add.sh:
#!/bin/bash
sum=0
echo "enter two numbers"
read a
read b
#we can use any of the following
sum=$(($a+$b))
#sum=`expr $a + $b`
# bc command can calucate accurately with floating point
#here we use division to examine the floating point result
#echo "$a/$b" | bc -l;
echo "sum of the two numbers is $sum"
Output:
$chmod 0755 add.sh
$./add.sh
enter two numbers
3
5
sum of the two numbers is 8
No comments:
Post a Comment