Shell Script Basics
Let us start the process in this Blog i am going to explain Shell Script Basics and shell script if condition statement ,else if condition statement, and or operators in bash shell
Introduction
Shell: When ever you login to a unix system you are placed in a program called the shell acts as a command interpreter , it takes each command and passes it to the operating system kernel to be acted upon it then displays the results of this operation on completion on your screen
DIFFERENT FLAVOURS OF SHELL IN UNIX
Bourne Shell: Bourne Shell scripts to specify the shell to use for the scripts. The default prompt on the Unix for this is $
#!/bin/bsh
C Shell:C Shell Scripts to specify the shell to use for the scripts.The default prompt on the Unix for this is %,
#!/bin/csh
Korn Shell: Korn Shell Scripts to specify the shell to use for the scripts.The default prompt on the Unix for this is $,
#!/bin/ksh
Shell Script: Shell Scripts collection of command which are executed in a order given. There are conditional statement and looping also available like if ,while which helps in finding if a particular value is greater than another value .
To write any comments in the shell scripts ,it has to be written with # preceded
Example
# Author of the script is
There are some variables Shell Script Basics which are set internally by the shell and which are available to the user
$1 – $9 Variables are the positional parameters.
$0 Name of the command currently being executed.
$# Number of positional arguments given to this invocation of the shell.
$? Exit status of the last command executed is given as a decimal string. When a command completes successfully, it returns the exit status of 0 (zero), otherwise it returns a non-zero exit status.
$$ Process number of this shell – useful for including in filenames, to make them unique.
$! Process id of the last command run in the background.
$* String containing all the arguments to the shell, starting at $1.
Shell scripts and functions are both interpreted. This means they are not compiled.
Commands in Shell
All shell have a number of built in command which are executed in the shell owns process like echo ,cd, when you enter a command first it will check the built in shell command echo or cd or it is directly interpreted by the shell,if the command begins with / shell assumes that command is absolute path name.
unix commands are executabale binary files located in directories with the name bin (for binary) many of the commands that are located in the directory /usr/bin.
Typical path variable might be
/bin:/usr/bin:/usr/local/utils/bin:$HOME/bin
Shell Script Example
cat chaitanya.sh
#Written by ChaitanyaOracleDbaBlog
TODAY=date '+%m%d%y_%H%M'
echo "This is Chaitanya at" $TODAY
The Name of each script must reflect its use,and each script must have a suffix that describes the what type of script it is “.sh” for Bourne shell ,“.ksh” for Korn shell or “.cgi” for Common Gateway Interface scripts
Lets start the first shell script
#!/bin/ksh # This script displays the date, time, username and
# current directory.
# Author Chaitanya
echo "Current date and time is:"
date
echo
echo "Your username is: `whoami` \\n"
echo "Your current directory is: \\c"
pwd echo "Your system name is :`hostname`\\n"
How to execute the shell script
Once the shell scripts are created ,they can be executed in two ways
1. We specify the shell type and then the script name
sh chaitanya.sh
2.We change the permission of the shell scripts to perform execution
chmod +x chaitanya.sh
./ chaitanya.sh
Bash if statement syntax
1.Bash if ...then..fi statement
if [ conditional expression ]
then
statement1
statement2
.
fi
Bash if then fi example
#!/bin/bash
count=300
if [ $count -eq 300 ]
then
echo "The Count is 300"
fi
2. Bash if ..then..else..fi statement syntax
If [ conditional expression ]
then
statement1
statement2
.
else
statement3
statement4
.
fi
Bash if ..then..else..fi statement Example
#!/bin/bash
count=299
if [ $count -eq 300 ]
then
echo " The Count is 300"
else
echo "The Count is not 300"
fi
3. Bash If....elif...else..fi statement syntax
If [ conditional expression1 ]
then
statement1
statement2
.
elif [ conditional expression2 ]
then
statement3
statement4
.
else
statement5
fi
Bash If....elif...else..fi statement Example
#!/bin/bash
count=299
if [ $count -eq 300 ]
then
echo "Count is 300"
elif [ $count -gt 300 ]
then
echo "Count is greater than 300"
else
echo "Count is less than 300"
fi
4. Bash If..then..else..if..then..fi..fi.. syntax
If [ conditional expression1 ]
then
statement1
statement2
.
else
if [ conditional expression2 ]
then
statement3
.
fi
fi
Bash If..then..else..if..then..fi..fi.. Example
#!/bin/bash
count=299
if [ $count -eq 300 ]
then
echo " The Count is 300"
else
if [ $count -gt 300 ]
then
echo "The Count is greater than 300"
else
echo "The Count is less than 300"
fi
fi
Test for numbers
-eq -------> equal to---------> x==y
-ge--------> greater than or equal to---> x>=y
-gt --------> greater than -----> x>y
-le ---------> less than or equal to ------> x<=y
-lt ---------> less than------> x<y
-ne ---------> not equal to ----> x!=y
AND and OR operator
&& ------> This stand for AND condition( if both the conditions are true then whole Condition will be true)
[[ $1 == yes && -r $1.txt ]]
|| --------> This stand for OR condition (if only one Condition is true then whole Condition will be true
[[ $1 == yes || -r $1.txt ]]
Note : Info on Shell Script Basics it may be differ in your environment like production,testing,development and naming conventions etc
THANKS FOR VIEWING MYBLOG FOR MORE UPDATES FOLLOW ME OR SUBSCRIBE ME
No comments:
Post a Comment