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
-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 '18 at 14:10npm config get proxy
returns the stringnull
rather than an empty string, then test for[ "$p" == 'null' ]
rather than[ -z "$p" ]
- is that somehow not completely obvious? – cas Jan 24 '18 at 05:00