Program based on hostname

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

./script QM hostname

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

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

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

You can use if-then-else

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

Or a case-statement might be simpler.

[/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.

Command giving as

./sco.sh
Enter QM
Enter hostname

Script is

#!/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

$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

$3=hostname -i

then $1 (QM) , $3 (Converted IPAdress) and $4 (PORT) should be assigned in below 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

#!/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
 

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):

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[@]}

I am trying from my end and then only asking for your help. That is why i am posting replies late.

throwing below error

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

till below code it is fine

#!/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

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