I am trying to assign a variable within a while loop and the script hangs during the read statement.

while read -r port  60720 60721 60722 60723 60724 

here is the code:

 qmgrs=$($MQ_INSTALL_PATH/bin/dspmq | grep QMNAME | awk -F\( '{ print $2}'| awk -F\) '{ print $1}')

numqmgrs=$($MQ_INSTALL_PATH/bin/dspmq | grep QMNAME | wc -l)



while ((i != 0))
while read -r port  60720 60721 60722 60723 60724 ;do


echo "

this is the $port for the $qmgrname”


desired output:

this is the 60720 for the apple”
this is the 60721 for the pear”
this is the 60722 for the mango”
this is the 60723 for the grape”
this is the 60724 for the blueberry”
  • What ksh are you using? ksh93? – Kusalananda Mar 11 '19 at 18:11
  • 1
    Is that while read -r port 60720 60721 60722 60723 60724 supposed to be for port in 60720 60721 60722 60723 60724? – roaima Mar 11 '19 at 18:14
  • @Kusalananda yes thats right ksh93 – MO12 Mar 11 '19 at 18:14
  • @roaima you are right. script hangs. for loop didnt work either. Its not setting the port value using for as you suggested – MO12 Mar 11 '19 at 18:22

It looks as if you'd like to pair a static list of port numbers with server names that you get from a command.

Do this instead:


ports=( 60720 60721 60722 60723 60724 )

dspmq | sed -n '/QMNAME/{ s/.*(\([^)]*\)).*/\1/p; }' |
while read -r server && [ "$i" -lt "${#ports[@]}" ]; do
    printf 'Port on %s is %d\n' "$server" "${ports[i]}"
    i=$(( i+1 ))

This is essentially what you were trying to do, but you used two nested loops rather than a single loop with a combined condition. The code is also reading the server names directly from the command pipeline that produces these without storing them in an intermediate array.

If you want to hand out the port numbers in the opposite order, use


ports=( 60720 60721 60722 60723 60724 )

dspmq | sed -n '/QMNAME/{ s/.*(\([^)]*\)).*/\1/p; }' |
while read -r server && [ "$i" -gt 0 ]; do
    i=$(( i-1 ))
    printf 'Port on %s is %d\n' "$server" "${ports[i]}"

The ${#ports[@]} expression will in all cases above expand to the number of elements in the ports array.

The sed command

sed -n '/QMNAME/{ s/.*(\([^)]*\)).*/\1/p; }'

will extract the string within the first parenthesis on the lines that contain the string QMNAME. It could also be written

sed -n '/QMNAME/{ s/.*(//; s/).*//p; }'
| improve this answer | |
  • port list is static, but not the server list. I comverted to static list for sake of simplicity..Thats the reason to use the numqmgrs as a counter to end the loop when the count is 0. – MO12 Mar 11 '19 at 18:28
  • @MO12 Hmm... You will have to update your question with some actual code to show the problem. We could possibly help you get that current code working, but it would be assuming a static list of servers. If that is not what you have, then the corrected code will still be wrong for you. Are you, for example reading the server names from a file? – Kusalananda Mar 11 '19 at 18:33
  • 1
    updated my question. I am deriving the qmgrname and numqmgr from output of another command – MO12 Mar 11 '19 at 18:40
  • @MO12 See update answer. – Kusalananda Mar 11 '19 at 19:01
  • @MO12 The $server value in the while loop is being read from the sed command on the line previous. Note the | at the end of the sed command that pipes it into the loop. – Kusalananda Jun 19 '19 at 16:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.