script question
script question
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.
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.
-
- Posts: 519
- Joined: 2012/06/26 14:20:47
Re: script question
You have a space after rhlevel= and other assignment(s).
Re: script question
And 41 probably is too high as the string is 36 characters:
CentOS Linux release 7.4.1708 (Core)
CentOS Linux release 7.4.1708 (Core)
Re: script question
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.
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.
Re: script question
The redhat-lsb-core package provides the script /usr/bin/lsb_release
When running
the output on my system is Release: 7.4.1708
The release version string is effectively obtained by doing:
When running
Code: Select all
lsb_release -r
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/'
Re: script question
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)
$
Re: script question
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
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
Re: script question
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.
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
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
Re: script question
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
German speaking forum for Fedora and CentOS: https://www.fedoraforum.de/