Need to have brackets within brackets in a shell script

Thaidog
Thaidog used Ask the Experts™
on
I have the following function set up in a shell script:

function configaccess {
# Setup access for root, sysadmins and monman user
cp -fp /etc/pam.d/system-auth /etc/pam.d/system-auth.orig
if [ $RELEASE == 5.7 ]; then
cat >>/etc/pam.d/system-auth<<EOF
#%PAM-1.0
# This file is auto-generated.
# User changes will be destroyed the next time authconfig is run.
auth        required      pam_env.so
auth        sufficient    pam_unix.so nullok try_first_pass
auth        requisite     pam_succeed_if.so uid >= 500 quiet
auth        required      pam_deny.so

account     sufficient    pam_localuser.so
account     required      pam_unix.so
account     sufficient    pam_succeed_if.so uid < 500 quiet
account     required      pam_permit.so
account     required      pam_access.so

password    requisite     pam_cracklib.so try_first_pass retry=3
password    sufficient    pam_unix.so md5 shadow nis nullok try_first_pass use_authtok
password    required      pam_deny.so

session     optional      pam_keyinit.so revoke
session     required      pam_limits.so
session     [success=1 default=ignore] pam_succeed_if.so service in crond quiet use_uid
session     required      pam_unix.so
EOF
elif [ $RELEASE == 6.2 ]; then
cat >>/etc/pam.d/system-auth<<EOF
#%PAM-1.0
# This file is auto-generated.
# User changes will be destroyed the next time authconfig is run.
auth        required      pam_env.so
auth        sufficient    pam_fprintd.so
auth        sufficient    pam_unix.so nullok try_first_pass
auth        requisite     pam_succeed_if.so uid >= 500 quiet
auth        required      pam_deny.so

account     sufficient    pam_localuser.so
account     required      pam_unix.so
account     sufficient    pam_localuser.so
account     sufficient    pam_succeed_if.so uid < 500 quiet
account     required      pam_permit.so
account     required      pam_access.so

password    requisite     pam_cracklib.so try_first_pass retry=3 type=
password    sufficient    pam_unix.so md5 shadow nis nullok try_first_pass use_authtok
password    required      pam_deny.so

session     optional      pam_keyinit.so revoke
session     required      pam_limits.so
session     [success=1 default=ignore] pam_succeed_if.so service in crond quiet use_uid
session     required      pam_unix.so
EOF
fi
}

Open in new window


I would like to have the part where the function check for the release [ $RELEASE == 6.2 ] to be something more like [ $RELEASE == 6[0-9] ] so that it can check for multiple versions instead of just 6.2 but I am not sure how to change the syntax so that it works correctly...
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
The '[' (i.e. 'test') function is limited in what it can test - for strings, it is equality or inequality - for integers you can use -eq, -ne (not equal) -gt (greater than) and so on.  Neither strings nor integers do what you want.

Easiest might be to specify allowed values as a regular expression, e.g:

if echo $RELEASE | grep -q '6\.[0-4]'; then

If you can't specify a single pattern (e.g. if you want to allow 5.6 to 5.9 and 6.0. to 6.2) you could use:

 if echo $RELEASE | grep -qE '5\.[6-9]|6\.[0-2]'; then
Most Valuable Expert 2013
Top Expert 2013
Commented:
Just use double brackets.

 [[ $RELEASE == 6[0-9] ]]

Author

Commented:
I'm getting errors with that:

[root@server ~]# sh new_nis2.sh
CentOS 5.7
new_nis2.sh: line 26: 5.7: command not found
new_nis2.sh: line 51: 5.7: command not found

#!/bin/bash -x

## Script exit codes
## 1 - Problem configuring NIS with authconfig
## 2 - Problem getting patch file(s) with wget
## 3 - Problem applying patch(es)
## 4 - Invalid command-line option
## 5 - Problem setting up automounter

PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin

LOGFILE=/var/log/kickstart/setup-nis.log
LOGCMD="tee -a $LOGFILE"

function getosversion {
    # Figure out what distro and release we are on
    DISTRO=`rpm -qf --queryformat "%{VENDOR}\n" /etc/redhat-release | awk '{print $1}'`
    RELEASE=`rpm -qf --queryformat "%{VERSION}.%.1{RELEASE}\n" /etc/redhat-release | sed -e 's/[AEW]S//g'`
    echo $DISTRO $RELEASE | $LOGCMD
}

function configaccess {
# Setup access for root, sysadmins and monman user
cp -fp /etc/pam.d/system-auth /etc/pam.d/system-auth.orig
if $RELEASE | grep -q '5\.[0-4]'; then
cat >>/etc/pam.d/system-auth<<EOF
#%PAM-1.0
# This file is auto-generated.
# User changes will be destroyed the next time authconfig is run.
auth        required      pam_env.so
auth        sufficient    pam_unix.so nullok try_first_pass
auth        requisite     pam_succeed_if.so uid >= 500 quiet
auth        required      pam_deny.so

account     sufficient    pam_localuser.so
account     required      pam_unix.so
account     sufficient    pam_succeed_if.so uid < 500 quiet
account     required      pam_permit.so
account     required      pam_access.so

password    requisite     pam_cracklib.so try_first_pass retry=3
password    sufficient    pam_unix.so md5 shadow nis nullok try_first_pass use_authtok
password    required      pam_deny.so

session     optional      pam_keyinit.so revoke
session     required      pam_limits.so
session     [success=1 default=ignore] pam_succeed_if.so service in crond quiet use_uid
session     required      pam_unix.so
EOF
elif $RELEASE | grep -q '6\.[0-4]'; then
cat >>/etc/pam.d/system-auth<<EOF
#%PAM-1.0
# This file is auto-generated.
# User changes will be destroyed the next time authconfig is run.
auth        required      pam_env.so
auth        sufficient    pam_fprintd.so
auth        sufficient    pam_unix.so nullok try_first_pass
auth        requisite     pam_succeed_if.so uid >= 500 quiet
auth        required      pam_deny.so

account     sufficient    pam_localuser.so
account     required      pam_unix.so
account     sufficient    pam_localuser.so
account     sufficient    pam_succeed_if.so uid < 500 quiet
account     required      pam_permit.so
account     required      pam_access.so

password    requisite     pam_cracklib.so try_first_pass retry=3 type=
password    sufficient    pam_unix.so md5 shadow nis nullok try_first_pass use_authtok
password    required      pam_deny.so

session     optional      pam_keyinit.so revoke
session     required      pam_limits.so
session     [success=1 default=ignore] pam_succeed_if.so service in crond quiet use_uid
session     required      pam_unix.so
EOF
fi
}

getosversion
configaccess

Open in new window

You missed the "echo" - if echo $RELEASE | .....

WMP's option is simpler, and also allows more complex expressions using =~ rather than ==, for example:

if [[ $RELEASE =~ 5.[6-9]|6.[0-2] ]]; then

Author

Commented:
Multiple solutions given since the second comment helped compress the script down

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial