Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
03-01-2014, 09:03 PM
|
#1
|
Member
Registered: Dec 2007
Distribution: Fedora, Oracle Linux & Centos
Posts: 197
Rep:
|
Bash Script to register different host to Spacewalk
Trying to make this script register host, two different spacewalk servers based on the distro and the last character of it's name.
The first if and elif condition work but after that it doesn't seem to honor the next if conditions. Anyone see what I'm doing wrong?
Code:
server=`/bin/hostname`
hostn=`/bin/hostname|awk -F. '{ print $1 }'| egrep -o .$`
type=`cat /etc/redhat-release|awk '{ print $NF }'`
centos=`cat /etc/redhat-release|awk '{ print $1 }'`
if [ $type = "(Tikanga)" ] && [ $hostn = "t" ] || [ $hostn = "d" ]
then
rhnreg_ks --serverUrl=http://servername/XMLRPC --activationkey=1-OEL5 --force
elif [ $type = "(Tikanga)" ] && [ $hostn = "p" ]
then
rhnreg_ks --serverUrl=http://servername/XMLRPC --activationkey=1-OEL5 --force
elif [ $type = "(Santiago)" ] && [ $hostn = "t" ] || [ $hostn = "d" ]
then
rhnreg_ks --serverUrl=http://servername/XMLRPC --activationkey=1-OEL6 --force
elif [ $type = "(Santiago)" ] && [ $hostn = "p" ]
then
rhnreg_ks --serverUrl=http://servername/XMLRPC --activationkey=1-OEL6 --force
elif [ $centos = "CentOS" ] && [ $hostn = "t" ] || [ $hostn = "d" ]
then
rhnreg_ks --force --serverUrl=http://servername/XMLRPC --activationkey=1-CENT6
elif [ $centos = "CentOS" ] && [ $hostn = "p" ]
then
rhnreg_ks --force --serverUrl=http://servername/XMLRPC --activationkey=1-CENT6
else
echo "The host $server doesn't meet the requirements to register!"
fi
Last edited by vwtech; 03-04-2014 at 11:53 AM.
|
|
|
03-02-2014, 02:55 AM
|
#3
|
Member
Registered: Dec 2007
Distribution: Fedora, Oracle Linux & Centos
Posts: 197
Original Poster
Rep:
|
I'm stumped right now.
Even with double quotes it doesn't honor, it will do the systems that hostname ends with "t" but not the "d".
Crazy part is if I make it the first if statement it will work but then another condition a few lines down won't.
Code:
[[ $type = "(Tikanga)" && $hostn = "t" || $hostn = "d" ]]
|
|
|
03-03-2014, 05:59 AM
|
#4
|
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,418
|
Like I said, try adding 'set -xv' at the top; it shows you what the parser is actually doing.
|
|
|
03-03-2014, 04:29 PM
|
#5
|
Member
Registered: Dec 2007
Distribution: Fedora, Oracle Linux & Centos
Posts: 197
Original Poster
Rep:
|
Put "set -vx" at the top of my script and it does show what gets executed:
Code:
+ [[ (Santiago) = \(\T\i\k\a\n\g\a\) ]]
+ [[ d = \d ]]
+ rhnreg_ks --serverUrl=http://servername01t/XMLRPC --activationkey=1-OEL5 --force
But since the out put from the three conditions I'm using are (Santiago) and d ,it should have run
Code:
rhnreg_ks --serverUrl=http://servername/XMLRPC --activationkey=1-OEL6 --force
Per my condition.
Last edited by vwtech; 03-04-2014 at 11:52 AM.
|
|
|
03-03-2014, 07:37 PM
|
#6
|
Member
Registered: Sep 2009
Location: Los Angeles, CA
Posts: 33
Rep:
|
Looks like it just gets answered by the first right away. Which shell are you using? Or better yet can you hard code the shell in your script?
e.g.
#!/bin/bash
set -vx
server=`/bin/hostname`
etc..etc..
I ran a test version of your script through #!/bin/bash and #!/bin/sh and worked fine for me. So the only thing I can think of is your shell is not handling your "=" comparison the way you think it is.
In programming, sometimes for example for "=" you need to use "eq" or "==" because "=" is setting a variable, not comparing it.
So depending on what shell you're using, read man page for that shell and see what it expects as a comparison operator for "equals"
Thanks,
|
|
|
03-03-2014, 09:16 PM
|
#7
|
Member
Registered: Dec 2007
Distribution: Fedora, Oracle Linux & Centos
Posts: 197
Original Poster
Rep:
|
I'm using the bash shell
The top of the script is has:
Code:
#!/bin/bash
set -vx
server=`/bin/hostname`
etc..etc..
It's a trip.
|
|
|
03-04-2014, 02:26 PM
|
#8
|
Member
Registered: Dec 2007
Distribution: Fedora, Oracle Linux & Centos
Posts: 197
Original Poster
Rep:
|
Trial and Error = Success
Must have been with how the comparison works inside double brackets vs single brackets.
Even after figuring that part out it, didn't like to many if-statements with the double brackets (sounds crazy, I know).
The code below ended up working for me.
Code:
server=`/bin/hostname`
hostn=`/bin/hostname|awk -F. '{ print $1 }'| egrep -o .$`
type=`cat /etc/redhat-release|awk '{ print $NF }'`
centos=`cat /etc/redhat-release|awk '{ print $1 }'`
rhel5="(Tikanga)"
rhel6="(Santiago)"
test="t"
dev="d"
if [ $type == "$rhel5" ] && [ $hostn == "$test" ]
then
rhnreg_ks --serverUrl=http://servername01t/XMLRPC --activationkey=1-OEL5 --force
elif [ $type == "$rhel5" ] && [ $hostn == "$dev" ]
then
rhnreg_ks --serverUrl=http://servername01t/XMLRPC --activationkey=1-OEL5 --force
elif [[ $type = $rhel6 && $hostn = "t" || $hostn = "d" ]]
then
rhnreg_ks --serverUrl=http://servername01t/XMLRPC --activationkey=1-OEL6 --force
elif [[ $centos = "CentOS" && $hostn = "t" || $hostn = "d" ]]
then
rhnreg_ks --force --serverUrl=http://servername01t/XMLRPC --activationkey=1-CENT6
elif [[ $type = "(Tikanga)" && $hostn = "p" ]]
then
rhnreg_ks --serverUrl=http://servername01p/XMLRPC --activationkey=1-OEL5 --force
elif [[ $type = "(Santiago)" && $hostn = "p" ]]
then
rhnreg_ks --serverUrl=http://servername01p/XMLRPC --activationkey=1-OEL6 --force
elif [[ $centos = "CentOS" && $hostn = "p" ]]
then
rhnreg_ks --force --serverUrl=http://servername01p/XMLRPC --activationkey=1-CENT6
else
echo "The host $server doesn't meet the requirements to register!"
|
|
|
All times are GMT -5. The time now is 11:33 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|