#!/bin/bash
if [ X`cat /proc/cpuinfo |grep vmx` != X ] && [ X`zcat /proc/config.gz |grep CONFIG_KVM=y` != X ];then
echo "xxxx"
fi
give below error
line 2: [: too many arguments
#!/bin/bash
if [ X`cat /proc/cpuinfo |grep vmx` != X ] && [ X`zcat /proc/config.gz |grep CONFIG_KVM=y` != X ];then
echo "xxxx"
fi
give below error
line 2: [: too many arguments
You've got at least one useless use of cat in there. Don't need to put X before your strings, you can test with -z. And should be quoting stuff (probably why you were getting the error).
But you don't need [ ] at all. Just test whether grep returns success or failure directly. You can suppress its output with -q on linux, or redirect its output with >/dev/null other places.
if grep -q vmx /proc/cpuinfo && zcat /proc/config.gz | grep -q CONFIG_KVM=y
then
echo "something"
fi
You need double brackets.
(00:10:48\[root@DeCoBoxOmega)
[~]$ cat search
xaaax
xxxxx
cbbbc
(00:10:58\[root@DeCoBoxOmega)
[~]$ cat help
if [[ x`grep q search` = x ]] && [[ x`grep i search` = x ]]; then
echo help;
fi
(00:11:16\[root@DeCoBoxOmega)
[~]$ bash help
help
if [[ `grep used file` = "" ]]
then
echo "pass"
fi
[[..]] construct is not at all required here. The general syntax of if
is:
if statement
then
<something>
fi
Something like this could be used instead (as Corona688 pointed out):
if ! grep -q used file 2>/dev/null