[SOLVED] scriptng in CentOS

General support questions
Post Reply
lightman47
Posts: 1203
Joined: 2014/05/21 20:16:00
Location: Central New York, USA

[SOLVED] scriptng in CentOS

Post by lightman47 » 2020/10/14 18:01:54

I know this isn't actually a CentOS issue and I apologize - am just hoping someone knowledgeable ...

For about a week I have been researching and trying to get a (nested) backup script to acquire the current major CentOS version - which it will use as part of the path to the backup server location. I've found that grep -oP '.*\K(?<=\ )[0-9](?=\.)' /etc/redhat-release returns the precise digit (albeit colored red) that I need to use to form this path directory.

Further googling for bash scripting seems to indicate that if I equate a script variable to this command within single quotes then that variable will contain the "7" or "8" that I can then concatenate to my specific path-to-proper-server-directory variable. Except I am doing something wrong with each permutation I try in my scripts. The best I come up with is a variable containing the grep command. I am sure it has everything to do with the 'escaping' (or not) of those single enclosing quotes (sometimes double quotes).
release='grep -oP '.*\K(?<=\ )[0-9](?=\.)' /etc/redhat-release'
echo "$release"
I've tried many with different variable names (currently 'release') - some attempts are:
release=\'grep -oP '.*\K(?<=\ )[0-9](?=\.)' /etc/redhat-release\'
release='grep -oP \'.*\K(?<=\ )[0-9](?=\.)\' /etc/redhat-release'
release="grep -oP '.*\K(?<=\ )[0-9](?=\.)' /etc/redhat-release"
release='/bin/bash grep -oP '.*\K(?<=\ )[0-9](?=\.)' /etc/redhat-release'
release="/bin/bash grep -oP '.*\K(?<=\ )[0-9](?=\.)' /etc/redhat-release"
and many, many more.

What am I 'just not getting'??
"release" (or whatever the variable name) never returns the text digit, but rather the command, or portions of it. I've also played with 'sed' and it's "&" - but I can't seem to get my backup script to actually acquire the result.

Thank you.
Last edited by lightman47 on 2020/10/15 11:53:15, edited 1 time in total.
Remember - importing/building packages will likely "byte you in the butt" come update time, long after you'd forgotten you did that! Use repos whenever possible.

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

Re: scriptng in CentOS

Post by TrevorH » 2020/10/14 18:07:52

They're not quotes, they're backticks - on a UK keyboard, key on the top lefthand side just below the Esc key. But it's usually recommended to use $() instead as it's marginally more efficient - so RELEASE=$(grep thing where)
CentOS 6 will die in November 2020 - migrate sooner rather than later!
Info for USB installs on http://wiki.centos.org/HowTos/InstallFromUSBkey
CentOS 5 is dead, do not use it.
Full time Geek, part time moderator. Use the FAQ Luke

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

Re: scriptng in CentOS

Post by lightman47 » 2020/10/14 19:30:25

I've never been known as "observant" and therefore didn't notice they were backticks. Good grief! Thank you for the re-direction.

I will however try your $() suggestion first.

Many thanks!!
Remember - importing/building packages will likely "byte you in the butt" come update time, long after you'd forgotten you did that! Use repos whenever possible.

owl102
Posts: 385
Joined: 2014/06/10 19:13:41

Re: scriptng in CentOS

Post by owl102 » 2020/10/15 07:30:34

lightman47 wrote:
2020/10/14 19:30:25
I will however try your $() suggestion first.
Regarding $() vs. backticks: https://mywiki.wooledge.org/BashFAQ/082
German speaking forum for Fedora and CentOS: https://www.fedoraforum.de/

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

Re: [SOLVED] scriptng in CentOS

Post by lightman47 » 2020/10/15 11:53:37

PERFECT - Thank you !

I can now get rid of that big block of if, then, elif's hard coded into the script!!
Remember - importing/building packages will likely "byte you in the butt" come update time, long after you'd forgotten you did that! Use repos whenever possible.

Post Reply

Return to “CentOS 7 - General Support”