Invalid output and loop is not breaking in this bash code
![Creative The name of the picture](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgO9GURib1T8z7lCwjOGLQaGtrueEthgQ8LO42ZX8cOfTqDK4jvDDpKkLFwf2J49kYCMNW7d4ABih_XCb_2UXdq5fPJDkoyg7-8g_YfRUot-XnaXkNYycsNp7lA5_TW9td0FFpLQ2APzKcZ/s1600/1.jpg)
![Creative The name of the picture](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhYQ0N5W1qAOxLP7t7iOM6O6AzbZnkXUy16s7P_CWfOb5UbTQY_aDsc727chyphenhyphen5W4IppVNernMMQeaUFTB_rFzAd95_CDt-tnwN-nBx6JyUp2duGjPaL5-VgNO41AVsA_vu30EJcipdDG409/s400/Clash+Royale+CLAN+TAG%2523URR8PPP.png)
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
command-line bash
add a comment |Â
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
command-line bash
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
add a comment |Â
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
command-line bash
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
command-line bash
asked Feb 7 at 18:20
![](https://i.stack.imgur.com/F8mcb.jpg?s=32&g=1)
![](https://i.stack.imgur.com/F8mcb.jpg?s=32&g=1)
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
add a comment |Â
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
add a comment |Â
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
add a comment |Â
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
add a comment |Â
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
add a comment |Â
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
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
edited Feb 7 at 20:34
answered Feb 7 at 18:27
![](https://i.stack.imgur.com/9L8vd.png?s=32&g=1)
![](https://i.stack.imgur.com/9L8vd.png?s=32&g=1)
dessert
20k55795
20k55795
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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