Assigning a variable within a while loop in shell script












0















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)

strqmgrs=(${qmgrs})

i=$numqmgrs
arrayindex=0

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

qmgrname=${strqmgrs[$arrayindex]}


echo "

this is the $port for the $qmgrname”


i=$((i-1))
arrayindex=$((arrayindex+1))
done
done


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”









share|improve this question

























  • What ksh are you using? ksh93?

    – Kusalananda
    4 hours ago






  • 1





    Is that while read -r port 60720 60721 60722 60723 60724 supposed to be for port in 60720 60721 60722 60723 60724?

    – roaima
    3 hours ago











  • @Kusalananda yes thats right ksh93

    – MO12
    3 hours ago











  • @roaima you are right. script hangs. for loop didnt work either. Its not setting the port value using for as you suggested

    – MO12
    3 hours ago
















0















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)

strqmgrs=(${qmgrs})

i=$numqmgrs
arrayindex=0

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

qmgrname=${strqmgrs[$arrayindex]}


echo "

this is the $port for the $qmgrname”


i=$((i-1))
arrayindex=$((arrayindex+1))
done
done


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”









share|improve this question

























  • What ksh are you using? ksh93?

    – Kusalananda
    4 hours ago






  • 1





    Is that while read -r port 60720 60721 60722 60723 60724 supposed to be for port in 60720 60721 60722 60723 60724?

    – roaima
    3 hours ago











  • @Kusalananda yes thats right ksh93

    – MO12
    3 hours ago











  • @roaima you are right. script hangs. for loop didnt work either. Its not setting the port value using for as you suggested

    – MO12
    3 hours ago














0












0








0








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)

strqmgrs=(${qmgrs})

i=$numqmgrs
arrayindex=0

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

qmgrname=${strqmgrs[$arrayindex]}


echo "

this is the $port for the $qmgrname”


i=$((i-1))
arrayindex=$((arrayindex+1))
done
done


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”









share|improve this question
















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)

strqmgrs=(${qmgrs})

i=$numqmgrs
arrayindex=0

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

qmgrname=${strqmgrs[$arrayindex]}


echo "

this is the $port for the $qmgrname”


i=$((i-1))
arrayindex=$((arrayindex+1))
done
done


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”






linux shell-script ksh






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 3 hours ago







MO12

















asked 4 hours ago









MO12MO12

81210




81210













  • What ksh are you using? ksh93?

    – Kusalananda
    4 hours ago






  • 1





    Is that while read -r port 60720 60721 60722 60723 60724 supposed to be for port in 60720 60721 60722 60723 60724?

    – roaima
    3 hours ago











  • @Kusalananda yes thats right ksh93

    – MO12
    3 hours ago











  • @roaima you are right. script hangs. for loop didnt work either. Its not setting the port value using for as you suggested

    – MO12
    3 hours ago



















  • What ksh are you using? ksh93?

    – Kusalananda
    4 hours ago






  • 1





    Is that while read -r port 60720 60721 60722 60723 60724 supposed to be for port in 60720 60721 60722 60723 60724?

    – roaima
    3 hours ago











  • @Kusalananda yes thats right ksh93

    – MO12
    3 hours ago











  • @roaima you are right. script hangs. for loop didnt work either. Its not setting the port value using for as you suggested

    – MO12
    3 hours ago

















What ksh are you using? ksh93?

– Kusalananda
4 hours ago





What ksh are you using? ksh93?

– Kusalananda
4 hours ago




1




1





Is that while read -r port 60720 60721 60722 60723 60724 supposed to be for port in 60720 60721 60722 60723 60724?

– roaima
3 hours ago





Is that while read -r port 60720 60721 60722 60723 60724 supposed to be for port in 60720 60721 60722 60723 60724?

– roaima
3 hours ago













@Kusalananda yes thats right ksh93

– MO12
3 hours ago





@Kusalananda yes thats right ksh93

– MO12
3 hours ago













@roaima you are right. script hangs. for loop didnt work either. Its not setting the port value using for as you suggested

– MO12
3 hours ago





@roaima you are right. script hangs. for loop didnt work either. Its not setting the port value using for as you suggested

– MO12
3 hours ago










1 Answer
1






active

oldest

votes


















0














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:



PATH=$MQ_INSTALL_PATH/bin:$PATH

ports=( 60720 60721 60722 60723 60724 )
i=0

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


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



PATH=$MQ_INSTALL_PATH/bin:$PATH

ports=( 60720 60721 60722 60723 60724 )
i=${#ports[@]}

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


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; }'





share|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
    3 hours ago













  • @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
    3 hours ago








  • 1





    updated my question. I am deriving the qmgrname and numqmgr from output of another command

    – MO12
    3 hours ago











  • @MO12 See update answer.

    – Kusalananda
    3 hours ago











Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f505694%2fassigning-a-variable-within-a-while-loop-in-shell-script%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














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:



PATH=$MQ_INSTALL_PATH/bin:$PATH

ports=( 60720 60721 60722 60723 60724 )
i=0

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


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



PATH=$MQ_INSTALL_PATH/bin:$PATH

ports=( 60720 60721 60722 60723 60724 )
i=${#ports[@]}

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


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; }'





share|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
    3 hours ago













  • @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
    3 hours ago








  • 1





    updated my question. I am deriving the qmgrname and numqmgr from output of another command

    – MO12
    3 hours ago











  • @MO12 See update answer.

    – Kusalananda
    3 hours ago
















0














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:



PATH=$MQ_INSTALL_PATH/bin:$PATH

ports=( 60720 60721 60722 60723 60724 )
i=0

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


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



PATH=$MQ_INSTALL_PATH/bin:$PATH

ports=( 60720 60721 60722 60723 60724 )
i=${#ports[@]}

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


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; }'





share|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
    3 hours ago













  • @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
    3 hours ago








  • 1





    updated my question. I am deriving the qmgrname and numqmgr from output of another command

    – MO12
    3 hours ago











  • @MO12 See update answer.

    – Kusalananda
    3 hours ago














0












0








0







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:



PATH=$MQ_INSTALL_PATH/bin:$PATH

ports=( 60720 60721 60722 60723 60724 )
i=0

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


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



PATH=$MQ_INSTALL_PATH/bin:$PATH

ports=( 60720 60721 60722 60723 60724 )
i=${#ports[@]}

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


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; }'





share|improve this answer















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:



PATH=$MQ_INSTALL_PATH/bin:$PATH

ports=( 60720 60721 60722 60723 60724 )
i=0

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


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



PATH=$MQ_INSTALL_PATH/bin:$PATH

ports=( 60720 60721 60722 60723 60724 )
i=${#ports[@]}

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


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; }'






share|improve this answer














share|improve this answer



share|improve this answer








edited 2 hours ago

























answered 3 hours ago









KusalanandaKusalananda

135k17255420




135k17255420













  • 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
    3 hours ago













  • @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
    3 hours ago








  • 1





    updated my question. I am deriving the qmgrname and numqmgr from output of another command

    – MO12
    3 hours ago











  • @MO12 See update answer.

    – Kusalananda
    3 hours ago



















  • 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
    3 hours ago













  • @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
    3 hours ago








  • 1





    updated my question. I am deriving the qmgrname and numqmgr from output of another command

    – MO12
    3 hours ago











  • @MO12 See update answer.

    – Kusalananda
    3 hours ago

















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
3 hours ago







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
3 hours ago















@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
3 hours ago







@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
3 hours ago






1




1





updated my question. I am deriving the qmgrname and numqmgr from output of another command

– MO12
3 hours ago





updated my question. I am deriving the qmgrname and numqmgr from output of another command

– MO12
3 hours ago













@MO12 See update answer.

– Kusalananda
3 hours ago





@MO12 See update answer.

– Kusalananda
3 hours ago


















draft saved

draft discarded




















































Thanks for contributing an answer to Unix & Linux Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f505694%2fassigning-a-variable-within-a-while-loop-in-shell-script%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Histoire des bourses de valeurs

Why is there Russian traffic in my log files?

Mouloudia Club d'Alger (football)