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

Issues related to applications and software problems
Post Reply
lightman47
Posts: 913
Joined: 2014/05/21 20:16:00
Location: Central New York, USA

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

Post by lightman47 » 2019/05/24 18:37:48

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.
Last edited by lightman47 on 2019/05/25 16:20:31, edited 1 time in total.
The more I learn the 'behinder' I get.

aks
Posts: 2791
Joined: 2014/09/20 11:22:14

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

Post by aks » 2019/05/24 18:51:07

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!

User avatar
TrevorH
Forum Moderator
Posts: 26098
Joined: 2009/09/24 10:40:56
Location: Brighton, UK

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

Post by TrevorH » 2019/05/24 20:19:11

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
CentOS 5 died in March 2017 - migrate NOW!
Full time Geek, part time moderator. Use the FAQ Luke

lightman47
Posts: 913
Joined: 2014/05/21 20:16:00
Location: Central New York, USA

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

Post by lightman47 » 2019/05/25 16:04:15

Thank you both. Got working with TrevorH's adjustment. Want to fiddle/learn the other (aks) when I get the chance.
;)
The more I learn the 'behinder' I get.

User avatar
TrevorH
Forum Moderator
Posts: 26098
Joined: 2009/09/24 10:40:56
Location: Brighton, UK

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

Post by TrevorH » 2019/05/25 16:10:30

I nicked the syntax for mine by running grep " if " /etc/sysconfig/network-scripts/* | grep grep
CentOS 5 died in March 2017 - migrate NOW!
Full time Geek, part time moderator. Use the FAQ Luke

lightman47
Posts: 913
Joined: 2014/05/21 20:16:00
Location: Central New York, USA

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

Post by lightman47 » 2019/05/25 16:19:43

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:
The more I learn the 'behinder' I get.

Post Reply