LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
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


Reply
  Search this Thread
Old 03-01-2014, 09:03 PM   #1
vwtech
Member
 
Registered: Dec 2007
Distribution: Fedora, Oracle Linux & Centos
Posts: 197

Rep: Reputation: 26
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.
 
Old 03-02-2014, 12:52 AM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 7.7 (?), Centos 8.1
Posts: 17,814

Rep: Reputation: 2553Reputation: 2553Reputation: 2553Reputation: 2553Reputation: 2553Reputation: 2553Reputation: 2553Reputation: 2553Reputation: 2553Reputation: 2553Reputation: 2553
Have your tried adding 'set xv' at the top to see what its actually doing?
I also recommend double [[ ]] http://tldp.org/LDP/abs/html/testcon...ml#DBLBRACKETS
 
Old 03-02-2014, 02:55 AM   #3
vwtech
Member
 
Registered: Dec 2007
Distribution: Fedora, Oracle Linux & Centos
Posts: 197

Original Poster
Rep: Reputation: 26
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" ]]
 
Old 03-03-2014, 05:59 AM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 7.7 (?), Centos 8.1
Posts: 17,814

Rep: Reputation: 2553Reputation: 2553Reputation: 2553Reputation: 2553Reputation: 2553Reputation: 2553Reputation: 2553Reputation: 2553Reputation: 2553Reputation: 2553Reputation: 2553
Like I said, try adding 'set -xv' at the top; it shows you what the parser is actually doing.
 
Old 03-03-2014, 04:29 PM   #5
vwtech
Member
 
Registered: Dec 2007
Distribution: Fedora, Oracle Linux & Centos
Posts: 197

Original Poster
Rep: Reputation: 26
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.
 
Old 03-03-2014, 07:37 PM   #6
byau
Member
 
Registered: Sep 2009
Location: Los Angeles, CA
Posts: 33

Rep: Reputation: 5
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,
 
Old 03-03-2014, 09:16 PM   #7
vwtech
Member
 
Registered: Dec 2007
Distribution: Fedora, Oracle Linux & Centos
Posts: 197

Original Poster
Rep: Reputation: 26
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.
 
Old 03-04-2014, 02:26 PM   #8
vwtech
Member
 
Registered: Dec 2007
Distribution: Fedora, Oracle Linux & Centos
Posts: 197

Original Poster
Rep: Reputation: 26
Smile 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!"
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash script that read from file and ping remote host zunder1990 Linux - Newbie 4 12-19-2012 11:15 AM
[SOLVED] Check if host is a live bash script edwardcode Programming 10 06-06-2012 12:22 PM
bash script recording remote host output? ieatbunnies Linux - Software 4 07-21-2010 01:23 PM
Writing a bash script to find and register vmware vms juchestyle Programming 6 01-07-2007 01:03 PM
bash script connect from one host to another and to another weird aim nakkaya Linux - Networking 1 06-30-2003 02:28 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 11:19 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration