Problem with script in Ubuntu
up vote
1
down vote
favorite
Am trying to write a script that tests to see if my vpn is connected. When run, the script currently only echos the vpn status. Eventually, I want the script to do much more, but for now, I am trying to work out the testing bit and it's driving me up the wall. No matter what status the VPN is in, the script always reports as "Not Connected".
The command I am using to check status is nordvpn status
but that spits out more lines of information than I am looking for, so I grep out the line that reports its connection status. Here is the raw output from nordvpn status
when connected.
You are connected to NordVPN.
Current server: us1681.nordvpn.com
Your new IP: xxx.xxx.200.1xx
Current protocol: UDP
Transfer: 1.7 MB received, 500.5 KB sent
This is the output from nordvpn status
when I am not connected.
You are not connected to NordVPN.
Here is the script:
#!/usr/bin/env bash
NORDSTAT1="$(nordvpn status | grep connected)"
if [ "$NORDSTAT1 = 'You are not connected to NordVPN.'" ]; then
echo Not Connected
else
echo Connected
fi
If I add a line to the script that says echo $NORDSTAT1
, it shows that the connection status is correctly stored in the variable. Also, I have confirmed that the string that I am testing against is an exact match of the grep'd line from nordvpn status
.
Any help would be greatly appreciated.
command-line bash scripts
add a comment |
up vote
1
down vote
favorite
Am trying to write a script that tests to see if my vpn is connected. When run, the script currently only echos the vpn status. Eventually, I want the script to do much more, but for now, I am trying to work out the testing bit and it's driving me up the wall. No matter what status the VPN is in, the script always reports as "Not Connected".
The command I am using to check status is nordvpn status
but that spits out more lines of information than I am looking for, so I grep out the line that reports its connection status. Here is the raw output from nordvpn status
when connected.
You are connected to NordVPN.
Current server: us1681.nordvpn.com
Your new IP: xxx.xxx.200.1xx
Current protocol: UDP
Transfer: 1.7 MB received, 500.5 KB sent
This is the output from nordvpn status
when I am not connected.
You are not connected to NordVPN.
Here is the script:
#!/usr/bin/env bash
NORDSTAT1="$(nordvpn status | grep connected)"
if [ "$NORDSTAT1 = 'You are not connected to NordVPN.'" ]; then
echo Not Connected
else
echo Connected
fi
If I add a line to the script that says echo $NORDSTAT1
, it shows that the connection status is correctly stored in the variable. Also, I have confirmed that the string that I am testing against is an exact match of the grep'd line from nordvpn status
.
Any help would be greatly appreciated.
command-line bash scripts
I'm not familiar withnordvpn status
however I did something similar to this (I was just checking for internet connectionnetctl status interface
). You might try checking the return value of the command. It is likely (assuming they're sane people at nordvpn) different if you are connected vs unconnected
– j-money
Nov 20 at 18:45
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
Am trying to write a script that tests to see if my vpn is connected. When run, the script currently only echos the vpn status. Eventually, I want the script to do much more, but for now, I am trying to work out the testing bit and it's driving me up the wall. No matter what status the VPN is in, the script always reports as "Not Connected".
The command I am using to check status is nordvpn status
but that spits out more lines of information than I am looking for, so I grep out the line that reports its connection status. Here is the raw output from nordvpn status
when connected.
You are connected to NordVPN.
Current server: us1681.nordvpn.com
Your new IP: xxx.xxx.200.1xx
Current protocol: UDP
Transfer: 1.7 MB received, 500.5 KB sent
This is the output from nordvpn status
when I am not connected.
You are not connected to NordVPN.
Here is the script:
#!/usr/bin/env bash
NORDSTAT1="$(nordvpn status | grep connected)"
if [ "$NORDSTAT1 = 'You are not connected to NordVPN.'" ]; then
echo Not Connected
else
echo Connected
fi
If I add a line to the script that says echo $NORDSTAT1
, it shows that the connection status is correctly stored in the variable. Also, I have confirmed that the string that I am testing against is an exact match of the grep'd line from nordvpn status
.
Any help would be greatly appreciated.
command-line bash scripts
Am trying to write a script that tests to see if my vpn is connected. When run, the script currently only echos the vpn status. Eventually, I want the script to do much more, but for now, I am trying to work out the testing bit and it's driving me up the wall. No matter what status the VPN is in, the script always reports as "Not Connected".
The command I am using to check status is nordvpn status
but that spits out more lines of information than I am looking for, so I grep out the line that reports its connection status. Here is the raw output from nordvpn status
when connected.
You are connected to NordVPN.
Current server: us1681.nordvpn.com
Your new IP: xxx.xxx.200.1xx
Current protocol: UDP
Transfer: 1.7 MB received, 500.5 KB sent
This is the output from nordvpn status
when I am not connected.
You are not connected to NordVPN.
Here is the script:
#!/usr/bin/env bash
NORDSTAT1="$(nordvpn status | grep connected)"
if [ "$NORDSTAT1 = 'You are not connected to NordVPN.'" ]; then
echo Not Connected
else
echo Connected
fi
If I add a line to the script that says echo $NORDSTAT1
, it shows that the connection status is correctly stored in the variable. Also, I have confirmed that the string that I am testing against is an exact match of the grep'd line from nordvpn status
.
Any help would be greatly appreciated.
command-line bash scripts
command-line bash scripts
edited Nov 20 at 19:42
George Udosen
18.5k94265
18.5k94265
asked Nov 20 at 18:42
Robert Baker
669
669
I'm not familiar withnordvpn status
however I did something similar to this (I was just checking for internet connectionnetctl status interface
). You might try checking the return value of the command. It is likely (assuming they're sane people at nordvpn) different if you are connected vs unconnected
– j-money
Nov 20 at 18:45
add a comment |
I'm not familiar withnordvpn status
however I did something similar to this (I was just checking for internet connectionnetctl status interface
). You might try checking the return value of the command. It is likely (assuming they're sane people at nordvpn) different if you are connected vs unconnected
– j-money
Nov 20 at 18:45
I'm not familiar with
nordvpn status
however I did something similar to this (I was just checking for internet connection netctl status interface
). You might try checking the return value of the command. It is likely (assuming they're sane people at nordvpn) different if you are connected vs unconnected– j-money
Nov 20 at 18:45
I'm not familiar with
nordvpn status
however I did something similar to this (I was just checking for internet connection netctl status interface
). You might try checking the return value of the command. It is likely (assuming they're sane people at nordvpn) different if you are connected vs unconnected– j-money
Nov 20 at 18:45
add a comment |
1 Answer
1
active
oldest
votes
up vote
3
down vote
accepted
The quoting in the script is wrong. The expression in the line
if [ "$NORDSTAT1 = 'You are not connected to NordVPN.'" ]; then
is one complete string that gets interpolated (variable resolved) to
if [ "You are not connected to NordVPN. = 'You are not connected to NordVPN.'" ]; then
This simply checks whether that string is non-empty. You want
if [ "$NORDSTAT1" = "You are not connected to NordVPN." ]; then
instead to test for equality of the two strings.
The test could also be rewritten with grep -q
. It tests for the existence
of a pattern without printing. Instead, the result can be determined from
grep
's returncode (which is 0
for found and !=0
for not found).
In that case the test must changed a bit:
#!/usr/bin/env bash
if nordvpn status | grep -q "not connected"; then
echo "Not Connected" # the string "not connected" is contained in the output
else
echo "Connected" # ... is not contained
fi
I think it's just a matter of taste in this case, though.
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
accepted
The quoting in the script is wrong. The expression in the line
if [ "$NORDSTAT1 = 'You are not connected to NordVPN.'" ]; then
is one complete string that gets interpolated (variable resolved) to
if [ "You are not connected to NordVPN. = 'You are not connected to NordVPN.'" ]; then
This simply checks whether that string is non-empty. You want
if [ "$NORDSTAT1" = "You are not connected to NordVPN." ]; then
instead to test for equality of the two strings.
The test could also be rewritten with grep -q
. It tests for the existence
of a pattern without printing. Instead, the result can be determined from
grep
's returncode (which is 0
for found and !=0
for not found).
In that case the test must changed a bit:
#!/usr/bin/env bash
if nordvpn status | grep -q "not connected"; then
echo "Not Connected" # the string "not connected" is contained in the output
else
echo "Connected" # ... is not contained
fi
I think it's just a matter of taste in this case, though.
add a comment |
up vote
3
down vote
accepted
The quoting in the script is wrong. The expression in the line
if [ "$NORDSTAT1 = 'You are not connected to NordVPN.'" ]; then
is one complete string that gets interpolated (variable resolved) to
if [ "You are not connected to NordVPN. = 'You are not connected to NordVPN.'" ]; then
This simply checks whether that string is non-empty. You want
if [ "$NORDSTAT1" = "You are not connected to NordVPN." ]; then
instead to test for equality of the two strings.
The test could also be rewritten with grep -q
. It tests for the existence
of a pattern without printing. Instead, the result can be determined from
grep
's returncode (which is 0
for found and !=0
for not found).
In that case the test must changed a bit:
#!/usr/bin/env bash
if nordvpn status | grep -q "not connected"; then
echo "Not Connected" # the string "not connected" is contained in the output
else
echo "Connected" # ... is not contained
fi
I think it's just a matter of taste in this case, though.
add a comment |
up vote
3
down vote
accepted
up vote
3
down vote
accepted
The quoting in the script is wrong. The expression in the line
if [ "$NORDSTAT1 = 'You are not connected to NordVPN.'" ]; then
is one complete string that gets interpolated (variable resolved) to
if [ "You are not connected to NordVPN. = 'You are not connected to NordVPN.'" ]; then
This simply checks whether that string is non-empty. You want
if [ "$NORDSTAT1" = "You are not connected to NordVPN." ]; then
instead to test for equality of the two strings.
The test could also be rewritten with grep -q
. It tests for the existence
of a pattern without printing. Instead, the result can be determined from
grep
's returncode (which is 0
for found and !=0
for not found).
In that case the test must changed a bit:
#!/usr/bin/env bash
if nordvpn status | grep -q "not connected"; then
echo "Not Connected" # the string "not connected" is contained in the output
else
echo "Connected" # ... is not contained
fi
I think it's just a matter of taste in this case, though.
The quoting in the script is wrong. The expression in the line
if [ "$NORDSTAT1 = 'You are not connected to NordVPN.'" ]; then
is one complete string that gets interpolated (variable resolved) to
if [ "You are not connected to NordVPN. = 'You are not connected to NordVPN.'" ]; then
This simply checks whether that string is non-empty. You want
if [ "$NORDSTAT1" = "You are not connected to NordVPN." ]; then
instead to test for equality of the two strings.
The test could also be rewritten with grep -q
. It tests for the existence
of a pattern without printing. Instead, the result can be determined from
grep
's returncode (which is 0
for found and !=0
for not found).
In that case the test must changed a bit:
#!/usr/bin/env bash
if nordvpn status | grep -q "not connected"; then
echo "Not Connected" # the string "not connected" is contained in the output
else
echo "Connected" # ... is not contained
fi
I think it's just a matter of taste in this case, though.
answered Nov 20 at 19:49
PerlDuck
4,83111130
4,83111130
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1094602%2fproblem-with-script-in-ubuntu%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
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
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
I'm not familiar with
nordvpn status
however I did something similar to this (I was just checking for internet connectionnetctl status interface
). You might try checking the return value of the command. It is likely (assuming they're sane people at nordvpn) different if you are connected vs unconnected– j-money
Nov 20 at 18:45