Page 1 of 1

[Solved] Bash issue - I know I am doing it wrong - help?

Posted: 2019/05/24 18:37:48
by lightman47
So, I have a script setupPrograms.sh that I run after the first boot of a new install of CentOS 7 that maps network drives, sets up repos, installs commonly used apps on my network, and more - it works great. Well, almost. I had to comment out the 'test' for CentOS 7 because it ALWAYS failed from within the script, but returned a result from the command line.

My test for CentOS 7 was:

Code: Select all

 if [ grep VERSION_ID=\"7\" /etc/os-release ] 
	then
If I copy the text between the brackets to the command line and execute it, I always get the (red) version info. If I create (I did) a separate bash script with that condition, it ALWAYS evaluates to false (executing the ELSE statements in the script) and complains
/scripts/versionTest.sh: line 1: [: VERSION_ID="7": binary operator expected
Script (tried multiple times escaping/un-escaping non-alpha characters to no avail):

Code: Select all

if [ grep VERSION_ID=\"7\" /etc/os-release ] 
 	then
    echo Version found!
else
    echo Failed for some reason!
fi
I know I've done something stupid - the binary thing is throwing me - I even tried escaping the "-". What does BASH want in my script(s) that's OK from the command line?

Thank you.

Re: Bash issue - I know I am doing it wrong - help?

Posted: 2019/05/24 18:51:07
by aks
You need to execute the comparison in a subshell.

if [ $(grep the thing) <matches> "this thing" ]

But don't do that. Use uname - as in what am I running really!

Re: Bash issue - I know I am doing it wrong - help?

Posted: 2019/05/24 20:19:11
by TrevorH

Code: Select all

[trevor@trevor4 ~]$ if grep -q 'VERSION_ID="8"' /etc/os-release; then echo found; else echo "not found";fi
not found
[trevor@trevor4 ~]$ if grep -q 'VERSION_ID="7"' /etc/os-release; then echo found; else echo "not found";fi
found

Re: Bash issue - I know I am doing it wrong - help?

Posted: 2019/05/25 16:04:15
by lightman47
Thank you both. Got working with TrevorH's adjustment. Want to fiddle/learn the other (aks) when I get the chance.
;)

Re: Bash issue - I know I am doing it wrong - help?

Posted: 2019/05/25 16:10:30
by TrevorH
I nicked the syntax for mine by running grep " if " /etc/sysconfig/network-scripts/* | grep grep

Re: Bash issue - I know I am doing it wrong - help?

Posted: 2019/05/25 16:19:43
by lightman47
Nice - saving the post ! Trying to figure out what it's doing makes my brain hurt - best saved for another time as well. Heh.
:lol: