Program based on hostname | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Program based on hostname

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 08-15-2013
darling darling is offline
Registered User
 
Join Date: Jun 2011
Last Activity: 11 July 2014, 6:07 AM EDT
Posts: 90
Thanks: 0
Thanked 2 Times in 2 Posts
Program based on hostname

Hi
Can you help me get the script for below requirement.
when i run below script

Code:
./script QM hostname

script should be working in below
here inputs are QM and hostname
by taking inputs it should work like below

Code:
QM=$1
if QM name ends with  1 $4 should be 51431 
                ends with  2 $4 should be 51432
                               3 $4 should be 51433
                               4 $4 should be 51434

and hostname should be converted into IP adress. i think it should be hostname -i to convert hostname into IP (IP address is $3)
then it should perform below command

Code:
 
echo $1 >&2 \ export MQSERVER=SYSTEM.DEF.SVRCONN/TCP/'$3($4)' \ ./qload -ISYSTEM.DEAD.LETTER.QUEUE -lmqic32 -m$1 >&2

Sponsored Links
    #2  
Old 08-15-2013
Scott's Avatar
Scott Scott is online now Forum Staff  
Administrator
 
Join Date: Jun 2009
Last Activity: 18 September 2014, 1:29 PM EDT
Location: Zürich
Posts: 8,478
Thanks: 331
Thanked 1,046 Times in 913 Posts
You can use if-then-else

Code:
  if [[ $1 =~ 1$ ]] && [[ $4 != 51431 ]]; then
    ...
  elif ...
    ...
  fi

Or a case-statement might be simpler.

[/code]
Code:
  case "$QM" in
    *1) NEED=51431;;
    *2) NEED=51432;;
    *3) NEED=51433 ;;
    *4) NEED=51434;;
    ?) NEED=-1;;
  esac

  if [ "$4" -ne "$NEED" ]; then
    echo "\$4 should be $NEED"
    : ...
  fi

Maybe you can use the host command to get the IP address (but I think you're using Solaris, and I'm not sure if the host command is available there), otherwise nslookup if it's in DNS.
Sponsored Links
    #3  
Old 08-15-2013
darling darling is offline
Registered User
 
Join Date: Jun 2011
Last Activity: 11 July 2014, 6:07 AM EDT
Posts: 90
Thanks: 0
Thanked 2 Times in 2 Posts
Command giving as

Code:
./sco.sh
Enter QM
Enter hostname

Script is
Code:
#!/bin/ksh
QM=$1
echo "enter QM"
read QM
hostname=$2
echo "enter hostname"
read hostname
case "$QM" in
    *1) NEED=51431;;
    *2) NEED=51432;;
    *3) NEED=51433 ;;
    *4) NEED=51434;;
    ?) NEED=-1;;
  esac
  if [ "$4" -ne "$NEED" ]; then
    echo "\$4 should be $NEED"
    : ...
  fi

but it is printing output as below

Code:
$4 should be 51431

what i want is 51431 should be assigned to $4
till here it is first part
second part is converting hostname into IP adress

Code:
$3=hostname -i

then $1 (QM) , $3 (Converted IPAdress) and $4 (PORT) should be assigned in below code


Code:
echo $1 >&2 \ export MQSERVER=SYSTEM.DEF.SVRCONN/TCP/'$3($4)' \ ./qload -ISYSTEM.DEAD.LETTER.QUEUE -lmqic32 -m$1 >&2

---------- Post updated at 10:03 AM ---------- Previous update was at 09:13 AM ----------

I tried below code, but it is not giving any output


Code:
#!/bin/ksh
QM=$1
echo "enter QM"
read QM
hostname=$2
echo "enter hostname"
read hostname
case "$QM" in
    *1) NEED=51431;;
    *2) NEED=51432;;
    *3) NEED=51433;;
    *4) NEED=51434;;
    ?) NEED=-1;;
  esac
  if [ "$PORT" -ne "$NEED" ]; then
 
PORT=$4
 
  fi
$3=$hostname -i
echo $1 >&2 \ export MQSERVER=SYSTEM.DEF.SVRCONN/TCP/'$3($4)' \ ./qload -ISYSTEM.DEAD.LETTER.QUEUE -lmqic32 -m$1 >&2

    #4  
Old 08-15-2013
RudiC RudiC is offline Forum Advisor  
Registered User
 
Join Date: Jul 2012
Last Activity: 17 September 2014, 4:41 PM EDT
Location: Aachen, Germany
Posts: 4,265
Thanks: 72
Thanked 1,030 Times in 976 Posts
A little effort in transfering proposals to your own solution should be invested, don't you think, too?
Assigning single positional parameters is not that easy. Try (this bashism):

Code:
case "${QM:${#QM}-1:1}" in
     1) NEED=51431;;
     2) NEED=51432;;
     3) NEED=51433;;
     4) NEED=51434;;
     ?) NEED=-1;;
   esac
TMP=($@)
TMP[3]="$NEED"
set ${TMP[@]}

Sponsored Links
    #5  
Old 08-16-2013
darling darling is offline
Registered User
 
Join Date: Jun 2011
Last Activity: 11 July 2014, 6:07 AM EDT
Posts: 90
Thanks: 0
Thanked 2 Times in 2 Posts
I am trying from my end and then only asking for your help. That is why i am posting replies late.

throwing below error
Code:
./jj.sh[9]: "${QM:${#QM}-1:1}": bad substitution

till below code it is fine


Code:
#!/bin/ksh
QM=$1
echo "enter QM"
read QM
hostname=$2
echo "enter hostname"
read hostname
case "$QM" in
    *1) NEED=51431;;
    *2) NEED=51432;;
    *3) NEED=51433;;
    *4) NEED=51434;;
    ?) NEED=-1;;
  esac

here how to store that value of 51431 or smnthg?

then need to get IP address from hostname by using hostname -i

and keep in below command


Code:
echo $1 >&2 \ export MQSERVER=SYSTEM.DEF.SVRCONN/TCP/'$3($4)' \ ./qload -ISYSTEM.DEAD.LETTER.QUEUE -lmqic32 -m$1 >&2

Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Sleep timer based on hostname deunan Shell Programming and Scripting 3 08-28-2012 05:13 AM
C program - convert values based on the position rocker_me2002 Programming 1 12-07-2011 03:32 PM
HP UX - ILO Console hostname different than Machine Hostname... zixzix01 Emergency UNIX and Linux Support 4 08-25-2011 07:25 PM
proceed through a menu-based program with a script? nichola$ UNIX for Dummies Questions & Answers 4 10-03-2008 05:01 PM
Solaris - unknown hostname - how can I change hostname? XNOR UNIX for Dummies Questions & Answers 1 03-29-2007 10:52 PM



All times are GMT -4. The time now is 01:44 PM.