Pranaam to all bhai ji _/\_
in this tutorial we will learn , how to use if condition when we are comparing strings.
string comparison is useful when we are checking for existence of a string have specific keywords .
like installation/update of packages on a system.
how???
lets start ;)
but first of all let me tell you operations that can be done in string comparison
string operations
string1 = string2 it checks string1 is equal to string2
string1 != string2 it checks string1 is not equal to string2
string string is NOT NULL or not defined
-n string string is NOT NULL and exist
-z string string is NULL and exist
lets start :)
Syntax for using string with if condition
if condition with test command
if test "string1" string operation "string2"
then
statement 1
statement 2
....
....
statement n
fi
Note:- dont forget to enclose string variable in double quotes. in case, if your string has space in between keywords, test command will consider them different-different variable and in such condition test command will through error , so its batter to enclose string variable in double quotes
if condition with [ EXPR ]
if [[ string1 string operation string2 ]]
then
statement 1
statement 2
....
....
statement n
fi
Note:- in last tutorial we used [ EXPR ] where as in case of string with if condition we will use [[ EXPR ]]
its because of avoiding error if string variable has more then one words and has space in between then .
lets have an example for both the cases
code for script
with test command
st="Team Indishell"
st2="Team Indishell"
if test "$st" = "$st2"
then
echo -e "both strings are equal and string is \n $st "
else
echo -e "strings are not equal and string values are following \n string 1 is $st \n string 2 is $st2 \n"
fi
lets write a script that will check for user defined package installation on system
i am writing a script that will ask for package name and then will check whether package is installed or not on system.
logic is simple
we execute command "rpm -q package_name" (for centos/redhat/fedora)
here package_name=name of the package like httpd(apache web server)
-q stands for query
if command output is "package httpd is not installed" , means package httpd is not installed on your system else it will print package name along with package version.
so we will check whether command output is "package package_name is not installed" , if yes then print package is not installed else print that package is installed on system
we need to take package name(r) as input from user, store that name in a string(string1) having words "package package_name is not installed" here package_name will be replaced with user input
store that package name in a string(string2) having words "rpm -q package_name"
again, we will replace package_name with variable r and then use string s3 as command and match output of this command with string 1 value
if string1 value is same as command output, it means package is not installed on system
:)
lets code it........
echo -e "\n enter the service name which you want to check for availability \n"
read r
echo -e "i am checking for service $r \n please wait ... \n"
string1="package $r is not installed"
string2=`rpm -q $r`
if [[ $string2 = $string1 ]]
then
echo -e "package $r is not installed on your system \n"
else
echo -e "package $r is installed on your system >:D< \n"
fi
save this code in a file and run that file :)
script is working fine :). you can modify it for further processing like if service is not installed, you can prompt user for installation using yum utility ;)
this is how we can use string comparison with if condition :)
stay in touch for further tutorials :)
Thank you
Greetz to :- Guru ji Zero , code breaker ica, Aasim shaikh,Reborn, Raman kumar rana,INX_r0ot,Darkwolf indishell, Chinmay Pandya,L0rd Crus4d3r,Hackuin ,Silent poison India,Magnum sniper,Atul Dwivedi,ethicalnoob Indishell,Local root indishell,Irfninja indishell Hardeep bhai,Mannu,Viki and AR AR bhai ji <3
in this tutorial we will learn , how to use if condition when we are comparing strings.
string comparison is useful when we are checking for existence of a string have specific keywords .
like installation/update of packages on a system.
how???
lets start ;)
but first of all let me tell you operations that can be done in string comparison
string operations
string1 = string2 it checks string1 is equal to string2
string1 != string2 it checks string1 is not equal to string2
string string is NOT NULL or not defined
-n string string is NOT NULL and exist
-z string string is NULL and exist
lets start :)
Syntax for using string with if condition
if condition with test command
if test "string1" string operation "string2"
then
statement 1
statement 2
....
....
statement n
fi
Note:- dont forget to enclose string variable in double quotes. in case, if your string has space in between keywords, test command will consider them different-different variable and in such condition test command will through error , so its batter to enclose string variable in double quotes
if condition with [ EXPR ]
if [[ string1 string operation string2 ]]
then
statement 1
statement 2
....
....
statement n
fi
Note:- in last tutorial we used [ EXPR ] where as in case of string with if condition we will use [[ EXPR ]]
its because of avoiding error if string variable has more then one words and has space in between then .
lets have an example for both the cases
code for script
with test command
st="Team Indishell"
st2="Team Indishell"
if test "$st" = "$st2"
then
echo -e "both strings are equal and string is \n $st "
else
echo -e "strings are not equal and string values are following \n string 1 is $st \n string 2 is $st2 \n"
fi
with [[ EXPR ]] command
st="Team Indishell"
st2="Team Indishell"
if [[ $st = $st2 ]]
then
echo -e "both strings are equal and string is \n $st "
else
echo -e "strings are not equal and string values are following \n string 1 is $st \n string 2 is $st2 \n"
fi
st2="Team Indishell"
if [[ $st = $st2 ]]
then
echo -e "both strings are equal and string is \n $st "
else
echo -e "strings are not equal and string values are following \n string 1 is $st \n string 2 is $st2 \n"
fi
i am writing a script that will ask for package name and then will check whether package is installed or not on system.
logic is simple
we execute command "rpm -q package_name" (for centos/redhat/fedora)
here package_name=name of the package like httpd(apache web server)
-q stands for query
if command output is "package httpd is not installed" , means package httpd is not installed on your system else it will print package name along with package version.
so we will check whether command output is "package package_name is not installed" , if yes then print package is not installed else print that package is installed on system
we need to take package name(r) as input from user, store that name in a string(string1) having words "package package_name is not installed" here package_name will be replaced with user input
store that package name in a string(string2) having words "rpm -q package_name"
again, we will replace package_name with variable r and then use string s3 as command and match output of this command with string 1 value
if string1 value is same as command output, it means package is not installed on system
:)
lets code it........
echo -e "\n enter the service name which you want to check for availability \n"
read r
echo -e "i am checking for service $r \n please wait ... \n"
string1="package $r is not installed"
string2=`rpm -q $r`
if [[ $string2 = $string1 ]]
then
echo -e "package $r is not installed on your system \n"
else
echo -e "package $r is installed on your system >:D< \n"
fi
save this code in a file and run that file :)
script is working fine :). you can modify it for further processing like if service is not installed, you can prompt user for installation using yum utility ;)
this is how we can use string comparison with if condition :)
stay in touch for further tutorials :)
Thank you
Greetz to :- Guru ji Zero , code breaker ica, Aasim shaikh,Reborn, Raman kumar rana,INX_r0ot,Darkwolf indishell, Chinmay Pandya,L0rd Crus4d3r,Hackuin ,Silent poison India,Magnum sniper,Atul Dwivedi,ethicalnoob Indishell,Local root indishell,Irfninja indishell Hardeep bhai,Mannu,Viki and AR AR bhai ji <3
0 comments