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

Issues related to applications and software problems
Post Reply
lightman47
Posts: 1522
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.

aks
Posts: 3073
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
Site Admin
Posts: 33211
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
The future appears to be RHEL or Debian. I think I'm going Debian.
Info for USB installs on http://wiki.centos.org/HowTos/InstallFromUSBkey
CentOS 5 and 6 are deadest, do not use them.
Use the FAQ Luke

lightman47
Posts: 1522
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.
;)

User avatar
TrevorH
Site Admin
Posts: 33211
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
The future appears to be RHEL or Debian. I think I'm going Debian.
Info for USB installs on http://wiki.centos.org/HowTos/InstallFromUSBkey
CentOS 5 and 6 are deadest, do not use them.
Use the FAQ Luke

lightman47
Posts: 1522
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:

Post Reply