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.










share|improve this question
























  • 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















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.










share|improve this question
























  • 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













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.










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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
















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










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.






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: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    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%2faskubuntu.com%2fquestions%2f1094602%2fproblem-with-script-in-ubuntu%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








    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.






    share|improve this answer

























      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.






      share|improve this answer























        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.






        share|improve this answer












        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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 20 at 19:49









        PerlDuck

        4,83111130




        4,83111130






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            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





















































            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

            Quarter-circle Tiles

            build a pushdown automaton that recognizes the reverse language of a given pushdown automaton?

            Mont Emei