Invalid output and loop is not breaking in this bash code

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP








up vote
0
down vote

favorite












I wrote this simple script to check whether some of the specific ports are open or not.



#!/bin/bash 
prt=(8080,22,53)
for i in "$prt[@]"
do
nc -vz 127.0.0.1 $i
if [ $? -eq 0 ]; then
echo "succeeded"
echo $i
break
else
echo "refused"
fi
done


It should loop through the list of ports and print the first open port and then break from the loop. Except 8080 all other ports are open in my system and should in theory output succeeded 22. But instead of doing that this is the output its giving



nc: port number invalid: 8080,22,53
refused









share|improve this question

















  • 2




    When you have a shell problem, a good first step is to cut and paste your code into shellcheck.net. In this case, as in many cases, it would have solved your problem quite quickly.
    – John1024
    Feb 7 at 18:36














up vote
0
down vote

favorite












I wrote this simple script to check whether some of the specific ports are open or not.



#!/bin/bash 
prt=(8080,22,53)
for i in "$prt[@]"
do
nc -vz 127.0.0.1 $i
if [ $? -eq 0 ]; then
echo "succeeded"
echo $i
break
else
echo "refused"
fi
done


It should loop through the list of ports and print the first open port and then break from the loop. Except 8080 all other ports are open in my system and should in theory output succeeded 22. But instead of doing that this is the output its giving



nc: port number invalid: 8080,22,53
refused









share|improve this question

















  • 2




    When you have a shell problem, a good first step is to cut and paste your code into shellcheck.net. In this case, as in many cases, it would have solved your problem quite quickly.
    – John1024
    Feb 7 at 18:36












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I wrote this simple script to check whether some of the specific ports are open or not.



#!/bin/bash 
prt=(8080,22,53)
for i in "$prt[@]"
do
nc -vz 127.0.0.1 $i
if [ $? -eq 0 ]; then
echo "succeeded"
echo $i
break
else
echo "refused"
fi
done


It should loop through the list of ports and print the first open port and then break from the loop. Except 8080 all other ports are open in my system and should in theory output succeeded 22. But instead of doing that this is the output its giving



nc: port number invalid: 8080,22,53
refused









share|improve this question













I wrote this simple script to check whether some of the specific ports are open or not.



#!/bin/bash 
prt=(8080,22,53)
for i in "$prt[@]"
do
nc -vz 127.0.0.1 $i
if [ $? -eq 0 ]; then
echo "succeeded"
echo $i
break
else
echo "refused"
fi
done


It should loop through the list of ports and print the first open port and then break from the loop. Except 8080 all other ports are open in my system and should in theory output succeeded 22. But instead of doing that this is the output its giving



nc: port number invalid: 8080,22,53
refused






command-line bash






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Feb 7 at 18:20









Eka

92661735




92661735







  • 2




    When you have a shell problem, a good first step is to cut and paste your code into shellcheck.net. In this case, as in many cases, it would have solved your problem quite quickly.
    – John1024
    Feb 7 at 18:36












  • 2




    When you have a shell problem, a good first step is to cut and paste your code into shellcheck.net. In this case, as in many cases, it would have solved your problem quite quickly.
    – John1024
    Feb 7 at 18:36







2




2




When you have a shell problem, a good first step is to cut and paste your code into shellcheck.net. In this case, as in many cases, it would have solved your problem quite quickly.
– John1024
Feb 7 at 18:36




When you have a shell problem, a good first step is to cut and paste your code into shellcheck.net. In this case, as in many cases, it would have solved your problem quite quickly.
– John1024
Feb 7 at 18:36










1 Answer
1






active

oldest

votes

















up vote
3
down vote













man bash says:




Arrays are assigned to using compound assignments of the form name=(value1 ... valuen), where each value is of the form [subscript]=string.




So to populate your array, you should rather do:





prt=(8080 22 53)


I would do the task as follows:



#!/bin/bash
prt=(8080 22 53)
for i in "$prt[@]"; do
if nc -vz 127.0.0.1 $i; then
echo -e succeeded\n$i
break
else
echo refused
fi
done





share|improve this answer






















    Your Answer







    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "89"
    ;
    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',
    convertImagesToLinks: true,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1003962%2finvalid-output-and-loop-is-not-breaking-in-this-bash-code%23new-answer', 'question_page');

    );

    Post as a guest






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    3
    down vote













    man bash says:




    Arrays are assigned to using compound assignments of the form name=(value1 ... valuen), where each value is of the form [subscript]=string.




    So to populate your array, you should rather do:





    prt=(8080 22 53)


    I would do the task as follows:



    #!/bin/bash
    prt=(8080 22 53)
    for i in "$prt[@]"; do
    if nc -vz 127.0.0.1 $i; then
    echo -e succeeded\n$i
    break
    else
    echo refused
    fi
    done





    share|improve this answer


























      up vote
      3
      down vote













      man bash says:




      Arrays are assigned to using compound assignments of the form name=(value1 ... valuen), where each value is of the form [subscript]=string.




      So to populate your array, you should rather do:





      prt=(8080 22 53)


      I would do the task as follows:



      #!/bin/bash
      prt=(8080 22 53)
      for i in "$prt[@]"; do
      if nc -vz 127.0.0.1 $i; then
      echo -e succeeded\n$i
      break
      else
      echo refused
      fi
      done





      share|improve this answer
























        up vote
        3
        down vote










        up vote
        3
        down vote









        man bash says:




        Arrays are assigned to using compound assignments of the form name=(value1 ... valuen), where each value is of the form [subscript]=string.




        So to populate your array, you should rather do:





        prt=(8080 22 53)


        I would do the task as follows:



        #!/bin/bash
        prt=(8080 22 53)
        for i in "$prt[@]"; do
        if nc -vz 127.0.0.1 $i; then
        echo -e succeeded\n$i
        break
        else
        echo refused
        fi
        done





        share|improve this answer














        man bash says:




        Arrays are assigned to using compound assignments of the form name=(value1 ... valuen), where each value is of the form [subscript]=string.




        So to populate your array, you should rather do:





        prt=(8080 22 53)


        I would do the task as follows:



        #!/bin/bash
        prt=(8080 22 53)
        for i in "$prt[@]"; do
        if nc -vz 127.0.0.1 $i; then
        echo -e succeeded\n$i
        break
        else
        echo refused
        fi
        done






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Feb 7 at 20:34

























        answered Feb 7 at 18:27









        dessert

        20k55795




        20k55795



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1003962%2finvalid-output-and-loop-is-not-breaking-in-this-bash-code%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            pylint3 and pip3 broken

            Missing snmpget and snmpwalk

            How to enroll fingerprints to Ubuntu 17.10 with VFS491