script question

General support questions
unix1adm
Posts: 153
Joined: 2010/02/23 13:27:06

script question

Post by unix1adm » 2018/04/19 11:51:03

So I am trying to grab the oslevel from a command in a script and pass it to a variable.
I tried various ` " but it does not get set. Or it tells me command not found.

#!/bin/sh
set -x

# Validate OS level
rhlevel= `cut -c 41 /etc/redhat-release`
echo $rhlevel
# Make logical volume
vg_space= 'vgdisplay |grep Free |cut -c 30`
echo "$vg_space"

This is just a portion of the script but the variables never get set.

[root tmp]# ./os_level.sh
++ cut -c 41 /etc/redhat-release
+ rhlevel=
+ 6
./os_level.sh: line 5: 6: command not found
+ echo

++ vgdisplay
++ grep Free
++ cut -c 30
+ vg_space=
+ 1
./os_level.sh: line 9: 1: command not found
+ echo ''
-------------------------------------

If I remove the ` ` around the commands i get this.
[root tmp]# ./os_level.sh
+ rhlevel=
+ cut -c 41 /etc/redhat-release
6
+ echo

+ vg_space=
+ vgdisplay
+ cut -c 30
+ grep Free
1
+ echo ''


Notice the echo statements show nothing for a value But the commands do run. However I need to pass the variable to another portion of my script.
I need to check if the OS is 5/6/7 and if the vg has less than 2G in root then do some other stuff.
I am sure it is just a simple syntax I am missing.

Thank you in advance for looking.

stevemowbray
Posts: 519
Joined: 2012/06/26 14:20:47

Re: script question

Post by stevemowbray » 2018/04/19 12:07:57

You have a space after rhlevel= and other assignment(s).

tunk
Posts: 1206
Joined: 2017/02/22 15:08:17

Re: script question

Post by tunk » 2018/04/19 12:40:24

And 41 probably is too high as the string is 36 characters:
CentOS Linux release 7.4.1708 (Core)

unix1adm
Posts: 153
Joined: 2010/02/23 13:27:06

Re: script question

Post by unix1adm » 2018/04/19 12:47:36

41 works fine to cut the field i want you can see it shows fine.

I did try moving the spacing around but that did not change anything.

I appreciate your input into this.

i added spaces and subtracts spaces. Here is the outputs.


[root tmp]# ./os_level.sh
+ rhlevel=cut
+ -c 41 /etc/redhat-release
./os_level.sh: line 5: -c: command not found
+ echo

+ vg_space=vgdisplay
+ cut -c 30
+ grep Free
+ echo ''

[root tmp]# ./os_level.sh
+ rhlevel = cut -c 41 /etc/redhat-release
./os_level.sh: line 5: rhlevel: command not found
+ echo

+ vg_space = vgdisplay
+ grep Free
./os_level.sh: line 9: vg_space: command not found
+ cut -c 30
+ echo ''


edit:

I see i forgot the `` on that last output. put them back in and see this now.
tmp]# ./os_level.sh
++ cut -c 41 /etc/redhat-release
+ rhlevel=6
+ echo 6
6
++ cut -c 30
++ vgdisplay
++ grep Free
+ vg_space=1
+ echo 1
1


I need coffee. Ill put it in the main script and see if it runs now.

more in a bit.

tunk
Posts: 1206
Joined: 2017/02/22 15:08:17

Re: script question

Post by tunk » 2018/04/19 13:10:20

Try to remove set -x.

pjsr2
Posts: 614
Joined: 2014/03/27 20:11:07

Re: script question

Post by pjsr2 » 2018/04/19 21:16:06

The redhat-lsb-core package provides the script /usr/bin/lsb_release

When running

Code: Select all

lsb_release -r
the output on my system is Release: 7.4.1708

The release version string is effectively obtained by doing:

Code: Select all

head -n 1 /etc/centos-release | sed -e 's/.*release[[:blank:]]*\([[:digit:]][[:graph:]]*\).*/\1/'

kt53
Posts: 48
Joined: 2016/05/12 05:12:02

Re: script question

Post by kt53 » 2018/04/20 02:20:49

This is in bash

Code: Select all

$ cat os-level.sh 
#!/bin/bash
oslvl="$(cut -c1-41 /etc/centos-release)"
echo $oslvl
$ ./os-level.sh 
CentOS Linux release 7.4.1708 (Core)
$

unix1adm
Posts: 153
Joined: 2010/02/23 13:27:06

Re: script question

Post by unix1adm » 2018/04/20 11:18:03

Thank you all for the response back.

This is what I ended up with.

This is just a part of the script. But the script now works.

# Variables

# Validate OS level
rhlevel=`cut -c 41 /etc/redhat-release`
#typeset -i rhlevel
echo "$rhlevel"
# Make logical volume
vg_space=`vgdisplay |grep Free |cut -c 30`
echo "$vg_space"
if [[ $vg_space -le 3 ]]; then
lvcreate -n taniumlv -L 512M /dev/mapper/rootvg
else
lvcreate -n taniumlv -L 2G /dev/mapper/rootvg
fi

User avatar
TrevorH
Site Admin
Posts: 33215
Joined: 2009/09/24 10:40:56
Location: Brighton, UK

Re: script question

Post by TrevorH » 2018/04/20 12:37:59

Parsing /etc/redhat-release is a bad idea. It's a text file and can be edited to contain anything.

What information are you trying to gather? Perhaps we can suggest a better way to get it than that.
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

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

Re: script question

Post by owl102 » 2018/04/20 13:12:50

Code: Select all

#!/bin/bash

source /etc/os-release
echo "ID is $ID"
echo "VERSION_ID is ${VERSION_ID}"

if [[ $ID == "centos" ]]
then
  echo "It's CentOS Version ${VERSION_ID}"
fi
BTW: Please don't use backticks: http://mywiki.wooledge.org/BashFAQ/082
German speaking forum for Fedora and CentOS: https://www.fedoraforum.de/

Post Reply