how to set and unset proxy dynamically through shell script?

Multi tool use
Multi tool use

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











up vote
0
down vote

favorite












p=sudo npm config get proxy;
echo "$p";
if [ -z $p ]
then
echo "delete";
sudo npm config delete proxy http://xxx.xx.xxx.xxx:8085
else
echo "set";
sudo npm config set proxy http://xxx.xx.xxx.xxx:8085
fi


I have tried this but didn't get result







share|improve this question
























    up vote
    0
    down vote

    favorite












    p=sudo npm config get proxy;
    echo "$p";
    if [ -z $p ]
    then
    echo "delete";
    sudo npm config delete proxy http://xxx.xx.xxx.xxx:8085
    else
    echo "set";
    sudo npm config set proxy http://xxx.xx.xxx.xxx:8085
    fi


    I have tried this but didn't get result







    share|improve this question






















      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      p=sudo npm config get proxy;
      echo "$p";
      if [ -z $p ]
      then
      echo "delete";
      sudo npm config delete proxy http://xxx.xx.xxx.xxx:8085
      else
      echo "set";
      sudo npm config set proxy http://xxx.xx.xxx.xxx:8085
      fi


      I have tried this but didn't get result







      share|improve this question












      p=sudo npm config get proxy;
      echo "$p";
      if [ -z $p ]
      then
      echo "delete";
      sudo npm config delete proxy http://xxx.xx.xxx.xxx:8085
      else
      echo "set";
      sudo npm config set proxy http://xxx.xx.xxx.xxx:8085
      fi


      I have tried this but didn't get result









      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 23 at 13:34









      yogesh agrawal

      1011




      1011




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          4
          down vote













          There wasn't too much wrong with your script:



          1. setting a variable to the output of a command (i.e. Command Substitution) needs $() around the command. I am deliberately ignoring the existence of obsolete backticks for the same purpose, they are broken in several ways.



          2. quote your variables when you use them.



            e.g. if [ -z $p ] without quotes is guaranteed to be a syntax error if $p is actually empty because -z requires an argument. if [ -z "$p" ] will never cause an error because even an empty string is an argument.



          Here's a minimally fixed version (also with superfluous semicolons removed):



          p="$(sudo npm config get proxy)"
          echo "$p"
          if [ -z "$p" ] ; then
          echo "delete"
          sudo npm config delete proxy http://xxx.xx.xxx.xxx:8085
          else
          echo "set"
          sudo npm config set proxy http://xxx.xx.xxx.xxx:8085
          fi





          share|improve this answer




















          • BTW, the -z probably needs to be a -n. The way it's written now, it attempts to delete the proxy setting if "$p" is empty (i.e. no proxy has been configured) which effectively does nothing, and sets a proxy if one is already set. But that's what I meant by "minimally fixed" - I fixed the obvious errors, without any consideration of whether your script actually does what you want/expect or not....I'll leave that up to you.
            – cas
            Jan 23 at 14:10










          • Mind expanding on your comment on why backticks are "broken in several ways"?
            – CyberJacob
            Jan 23 at 16:01










          • quoting issues. you can't nest backticks. difficult to distinguish from single-quotes. general ugliness. some (unverified) reports of demonic manifestations. see, e.g., unix.stackexchange.com/questions/126927/… and mywiki.wooledge.org/BashFAQ/082 and wiki.bash-hackers.org/syntax/expansion/cmdsubst. backticks still work (as well...badly...as they ever did) but don't use them. backticks are evil, and they will cause your hamster to spontaneously explode.
            – cas
            Jan 23 at 16:14










          • But ... I don't have a hamster. Now I'm even more worried.
            – CyberJacob
            Jan 23 at 16:50










          • i accidentally deleted the chilling end to that sentence - "even if you don't have a hamster".
            – cas
            Jan 23 at 16:58











          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',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          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%2funix.stackexchange.com%2fquestions%2f419080%2fhow-to-set-and-unset-proxy-dynamically-through-shell-script%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
          4
          down vote













          There wasn't too much wrong with your script:



          1. setting a variable to the output of a command (i.e. Command Substitution) needs $() around the command. I am deliberately ignoring the existence of obsolete backticks for the same purpose, they are broken in several ways.



          2. quote your variables when you use them.



            e.g. if [ -z $p ] without quotes is guaranteed to be a syntax error if $p is actually empty because -z requires an argument. if [ -z "$p" ] will never cause an error because even an empty string is an argument.



          Here's a minimally fixed version (also with superfluous semicolons removed):



          p="$(sudo npm config get proxy)"
          echo "$p"
          if [ -z "$p" ] ; then
          echo "delete"
          sudo npm config delete proxy http://xxx.xx.xxx.xxx:8085
          else
          echo "set"
          sudo npm config set proxy http://xxx.xx.xxx.xxx:8085
          fi





          share|improve this answer




















          • BTW, the -z probably needs to be a -n. The way it's written now, it attempts to delete the proxy setting if "$p" is empty (i.e. no proxy has been configured) which effectively does nothing, and sets a proxy if one is already set. But that's what I meant by "minimally fixed" - I fixed the obvious errors, without any consideration of whether your script actually does what you want/expect or not....I'll leave that up to you.
            – cas
            Jan 23 at 14:10










          • Mind expanding on your comment on why backticks are "broken in several ways"?
            – CyberJacob
            Jan 23 at 16:01










          • quoting issues. you can't nest backticks. difficult to distinguish from single-quotes. general ugliness. some (unverified) reports of demonic manifestations. see, e.g., unix.stackexchange.com/questions/126927/… and mywiki.wooledge.org/BashFAQ/082 and wiki.bash-hackers.org/syntax/expansion/cmdsubst. backticks still work (as well...badly...as they ever did) but don't use them. backticks are evil, and they will cause your hamster to spontaneously explode.
            – cas
            Jan 23 at 16:14










          • But ... I don't have a hamster. Now I'm even more worried.
            – CyberJacob
            Jan 23 at 16:50










          • i accidentally deleted the chilling end to that sentence - "even if you don't have a hamster".
            – cas
            Jan 23 at 16:58















          up vote
          4
          down vote













          There wasn't too much wrong with your script:



          1. setting a variable to the output of a command (i.e. Command Substitution) needs $() around the command. I am deliberately ignoring the existence of obsolete backticks for the same purpose, they are broken in several ways.



          2. quote your variables when you use them.



            e.g. if [ -z $p ] without quotes is guaranteed to be a syntax error if $p is actually empty because -z requires an argument. if [ -z "$p" ] will never cause an error because even an empty string is an argument.



          Here's a minimally fixed version (also with superfluous semicolons removed):



          p="$(sudo npm config get proxy)"
          echo "$p"
          if [ -z "$p" ] ; then
          echo "delete"
          sudo npm config delete proxy http://xxx.xx.xxx.xxx:8085
          else
          echo "set"
          sudo npm config set proxy http://xxx.xx.xxx.xxx:8085
          fi





          share|improve this answer




















          • BTW, the -z probably needs to be a -n. The way it's written now, it attempts to delete the proxy setting if "$p" is empty (i.e. no proxy has been configured) which effectively does nothing, and sets a proxy if one is already set. But that's what I meant by "minimally fixed" - I fixed the obvious errors, without any consideration of whether your script actually does what you want/expect or not....I'll leave that up to you.
            – cas
            Jan 23 at 14:10










          • Mind expanding on your comment on why backticks are "broken in several ways"?
            – CyberJacob
            Jan 23 at 16:01










          • quoting issues. you can't nest backticks. difficult to distinguish from single-quotes. general ugliness. some (unverified) reports of demonic manifestations. see, e.g., unix.stackexchange.com/questions/126927/… and mywiki.wooledge.org/BashFAQ/082 and wiki.bash-hackers.org/syntax/expansion/cmdsubst. backticks still work (as well...badly...as they ever did) but don't use them. backticks are evil, and they will cause your hamster to spontaneously explode.
            – cas
            Jan 23 at 16:14










          • But ... I don't have a hamster. Now I'm even more worried.
            – CyberJacob
            Jan 23 at 16:50










          • i accidentally deleted the chilling end to that sentence - "even if you don't have a hamster".
            – cas
            Jan 23 at 16:58













          up vote
          4
          down vote










          up vote
          4
          down vote









          There wasn't too much wrong with your script:



          1. setting a variable to the output of a command (i.e. Command Substitution) needs $() around the command. I am deliberately ignoring the existence of obsolete backticks for the same purpose, they are broken in several ways.



          2. quote your variables when you use them.



            e.g. if [ -z $p ] without quotes is guaranteed to be a syntax error if $p is actually empty because -z requires an argument. if [ -z "$p" ] will never cause an error because even an empty string is an argument.



          Here's a minimally fixed version (also with superfluous semicolons removed):



          p="$(sudo npm config get proxy)"
          echo "$p"
          if [ -z "$p" ] ; then
          echo "delete"
          sudo npm config delete proxy http://xxx.xx.xxx.xxx:8085
          else
          echo "set"
          sudo npm config set proxy http://xxx.xx.xxx.xxx:8085
          fi





          share|improve this answer












          There wasn't too much wrong with your script:



          1. setting a variable to the output of a command (i.e. Command Substitution) needs $() around the command. I am deliberately ignoring the existence of obsolete backticks for the same purpose, they are broken in several ways.



          2. quote your variables when you use them.



            e.g. if [ -z $p ] without quotes is guaranteed to be a syntax error if $p is actually empty because -z requires an argument. if [ -z "$p" ] will never cause an error because even an empty string is an argument.



          Here's a minimally fixed version (also with superfluous semicolons removed):



          p="$(sudo npm config get proxy)"
          echo "$p"
          if [ -z "$p" ] ; then
          echo "delete"
          sudo npm config delete proxy http://xxx.xx.xxx.xxx:8085
          else
          echo "set"
          sudo npm config set proxy http://xxx.xx.xxx.xxx:8085
          fi






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 23 at 13:43









          cas

          37.7k44393




          37.7k44393











          • BTW, the -z probably needs to be a -n. The way it's written now, it attempts to delete the proxy setting if "$p" is empty (i.e. no proxy has been configured) which effectively does nothing, and sets a proxy if one is already set. But that's what I meant by "minimally fixed" - I fixed the obvious errors, without any consideration of whether your script actually does what you want/expect or not....I'll leave that up to you.
            – cas
            Jan 23 at 14:10










          • Mind expanding on your comment on why backticks are "broken in several ways"?
            – CyberJacob
            Jan 23 at 16:01










          • quoting issues. you can't nest backticks. difficult to distinguish from single-quotes. general ugliness. some (unverified) reports of demonic manifestations. see, e.g., unix.stackexchange.com/questions/126927/… and mywiki.wooledge.org/BashFAQ/082 and wiki.bash-hackers.org/syntax/expansion/cmdsubst. backticks still work (as well...badly...as they ever did) but don't use them. backticks are evil, and they will cause your hamster to spontaneously explode.
            – cas
            Jan 23 at 16:14










          • But ... I don't have a hamster. Now I'm even more worried.
            – CyberJacob
            Jan 23 at 16:50










          • i accidentally deleted the chilling end to that sentence - "even if you don't have a hamster".
            – cas
            Jan 23 at 16:58

















          • BTW, the -z probably needs to be a -n. The way it's written now, it attempts to delete the proxy setting if "$p" is empty (i.e. no proxy has been configured) which effectively does nothing, and sets a proxy if one is already set. But that's what I meant by "minimally fixed" - I fixed the obvious errors, without any consideration of whether your script actually does what you want/expect or not....I'll leave that up to you.
            – cas
            Jan 23 at 14:10










          • Mind expanding on your comment on why backticks are "broken in several ways"?
            – CyberJacob
            Jan 23 at 16:01










          • quoting issues. you can't nest backticks. difficult to distinguish from single-quotes. general ugliness. some (unverified) reports of demonic manifestations. see, e.g., unix.stackexchange.com/questions/126927/… and mywiki.wooledge.org/BashFAQ/082 and wiki.bash-hackers.org/syntax/expansion/cmdsubst. backticks still work (as well...badly...as they ever did) but don't use them. backticks are evil, and they will cause your hamster to spontaneously explode.
            – cas
            Jan 23 at 16:14










          • But ... I don't have a hamster. Now I'm even more worried.
            – CyberJacob
            Jan 23 at 16:50










          • i accidentally deleted the chilling end to that sentence - "even if you don't have a hamster".
            – cas
            Jan 23 at 16:58
















          BTW, the -z probably needs to be a -n. The way it's written now, it attempts to delete the proxy setting if "$p" is empty (i.e. no proxy has been configured) which effectively does nothing, and sets a proxy if one is already set. But that's what I meant by "minimally fixed" - I fixed the obvious errors, without any consideration of whether your script actually does what you want/expect or not....I'll leave that up to you.
          – cas
          Jan 23 at 14:10




          BTW, the -z probably needs to be a -n. The way it's written now, it attempts to delete the proxy setting if "$p" is empty (i.e. no proxy has been configured) which effectively does nothing, and sets a proxy if one is already set. But that's what I meant by "minimally fixed" - I fixed the obvious errors, without any consideration of whether your script actually does what you want/expect or not....I'll leave that up to you.
          – cas
          Jan 23 at 14:10












          Mind expanding on your comment on why backticks are "broken in several ways"?
          – CyberJacob
          Jan 23 at 16:01




          Mind expanding on your comment on why backticks are "broken in several ways"?
          – CyberJacob
          Jan 23 at 16:01












          quoting issues. you can't nest backticks. difficult to distinguish from single-quotes. general ugliness. some (unverified) reports of demonic manifestations. see, e.g., unix.stackexchange.com/questions/126927/… and mywiki.wooledge.org/BashFAQ/082 and wiki.bash-hackers.org/syntax/expansion/cmdsubst. backticks still work (as well...badly...as they ever did) but don't use them. backticks are evil, and they will cause your hamster to spontaneously explode.
          – cas
          Jan 23 at 16:14




          quoting issues. you can't nest backticks. difficult to distinguish from single-quotes. general ugliness. some (unverified) reports of demonic manifestations. see, e.g., unix.stackexchange.com/questions/126927/… and mywiki.wooledge.org/BashFAQ/082 and wiki.bash-hackers.org/syntax/expansion/cmdsubst. backticks still work (as well...badly...as they ever did) but don't use them. backticks are evil, and they will cause your hamster to spontaneously explode.
          – cas
          Jan 23 at 16:14












          But ... I don't have a hamster. Now I'm even more worried.
          – CyberJacob
          Jan 23 at 16:50




          But ... I don't have a hamster. Now I'm even more worried.
          – CyberJacob
          Jan 23 at 16:50












          i accidentally deleted the chilling end to that sentence - "even if you don't have a hamster".
          – cas
          Jan 23 at 16:58





          i accidentally deleted the chilling end to that sentence - "even if you don't have a hamster".
          – cas
          Jan 23 at 16:58













           

          draft saved


          draft discarded


























           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f419080%2fhow-to-set-and-unset-proxy-dynamically-through-shell-script%23new-answer', 'question_page');

          );

          Post as a guest













































































          J6,dq,QPxDiDUFyu0FL3RQ a0NoM,lCvqlRaIHAuK0SICjSkz,JFd mSomem,l2LcZ,6NB
          NQIT Wofy26KRN,F9,uz8Zt mFr56ba Jy arYsaFMsMcgfMtIJaJJ5TvLjtCkJAM

          Popular posts from this blog

          How to check contact read email or not when send email to Individual?

          How many registers does an x86_64 CPU actually have?

          Displaying single band from multi-band raster using QGIS