'how can i use grep result from a variable in if/else statment
i am trying to create a condition statement that checks if the grep found if a package is installed. if it is true , then the package should not be installed , and if it is false , then the package should be installed. i am always getting the same result package is not installed no matter which value i put please help (in my case the all packages are installed and grep finds a match. here is code:
chk1=$(yum list installed | grep rpmdevtools)
chk2=$(yum list installed | grep rpmbuild)
chk3=$(yum list installed | grep rpmdev)
if [[ $chk1 -ne 0 && "$chk2" -ne 0 && "$chk3" -ne 0 ]];then
echo "package exists"
sleep 5
else
echo "package doesn't exists installing .."
sleep 5
sudo yum install wget -y
wget http://mirror.centos.org/centos/7/os/x86_64/Packages/rpmdevtools-8.3-5.el7.noarch.rpm
sudo yum install rpmdevtools-8.3-5.el7.noarch.rpm -y
fi
Solution 1:[1]
I'm not familiar with yum, there may be a better way to check, but this will make your if statement work:
if
yum list installed | grep -q rpmdevtools &&
yum list installed | grep -q rpmbuild &&
yum list installed | grep -q rpmdev
then
I also recommend adding -w to grep, to match a whole word.
If yum list installed is a slow command, consider saving the output in a variable, and grepping that (instead of running it three times):
list=$(yum list installed)
if
echo "$list" | grep -q rpmdevtools &&
echo "$list" | grep -q rpmbuild &&
echo "$list" | grep -q rpmdev
then
Solution 2:[2]
The immediate problem is that -ne checks for numeric equality; of course the output from grep is not going to be the number 0, so all of your checks fail.
You seem to be overcomplicating things significantly anyway. yum list installed optionally accepts a list of packages to check.
if yum list installed rpmdevtools rpmbuild rpmdev >/dev/null 2>&1
then
echo "$0: package exists" >&1
else
echo "$0: package doesn't exist; installing ..." >&2
sudo yum install -y wget
wget http://mirror.centos.org/centos/7/os/x86_64/Packages/rpmdevtools-8.3-5.el7.noarch.rpm
sudo yum install -y rpmdevtools-8.3-5.el7.noarch.rpm
fi
Notice the convention for diagnostic messages to include the script name $0 and print to standard error >&2, and how we avoid the absolutely user-hostile abomination of sleeping in the middle. If you want to give users a chance to decide whether they really want to proceed with the planned action, display a separate prompt (and ideally make it possible to override from the command line, just like yum install -y lets you say you don't want a prompt).
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 |
