how to set and unset proxy dynamically through shell script?
Clash 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
linux shell-script shell
add a comment |Â
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
linux shell-script shell
add a comment |Â
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
linux shell-script shell
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
linux shell-script shell
asked Jan 23 at 13:34
yogesh agrawal
1011
1011
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
4
down vote
There wasn't too much wrong with your script:
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.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
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
 |Â
show 3 more comments
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:
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.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
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
 |Â
show 3 more comments
up vote
4
down vote
There wasn't too much wrong with your script:
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.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
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
 |Â
show 3 more comments
up vote
4
down vote
up vote
4
down vote
There wasn't too much wrong with your script:
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.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
There wasn't too much wrong with your script:
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.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
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
 |Â
show 3 more comments
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
 |Â
show 3 more comments
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
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
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
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
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