On 4/15/2022 10:50 AM, Greg Wooledge wrote:
On Fri, Apr 15, 2022 at 10:34:25AM -0400, Chuck Zmudzinski wrote:
user@debian:~$ cat ipv6
#!/bin/bash
if [ $1 == "on" ]
then
ip -6 route add default via <redacted> dev <redacted>
elif [ $1 == "off" ]
then
ip -6 route delete default
fi
Quotes are in the wrong place. The [ builtin command follows the
ordinary parsing rules, which means an unquoted $1 argument will be
subject to word splitting and filename expansions.
In simpler terms, it will blow up if $1 is empty or contains whitespace
characters or globbing characters.
The quotes need to go around "$1", not around string constants.
if [ "$1" == on ]
You are right, with no arguments I get this:
./ipv6: line 2: [: ==: unary operator expected
./ipv6: line 5: [: ==: unary operator expected
I did not write the script with unexpected values or whitespace for the
argument in mind. It is a simple script that works as expected if the
argument is what the script is designed for, either 'on' or 'off'. Of
course the script is improved as you suggest to not dump the error
message when there is no argument and is also further improved with
error and help messages to handle the cases when the argument is not as
designed or contains whitespace, as you point out. I have updated my
script with your suggestion. Thanks!
Chuck