I have a question on how I'm adding arguments when executing a shell script.
I have a simple script which helps me block ip ranges:
~/block_ip.sh:
if [ ! $3 ]
then
echo "usage ~/block_ip.sh (DROP/ACCEPT) '0.0.0.0' 'amsterdam'"
exit 1
fi
echo "adding $3"
sudo iptables -I INPUT -s $2 -j $1 -m comment --comment "$3"
If I execute this without arguments the output is as expected:
~$ ./block_ip.sh
usage ~/block_ip.sh (DROP/ACCEPT) '0.0.0.0' 'amsterdam'
However the spaces seem to be the cause of the unexpected output of "binary operator expected":
~$ ./block_ip.sh DROP '1.0.0.0/8' 'south brisbane qld'
./block_ip.sh: line 1: [: brisbane: binary operator expected
adding south brisbane au
But then it adds it despite the unexpected output:
Chain INPUT (policy ACCEPT)
num target prot opt source destination
1 DROP all -- 1.0.0.0/8 anywhere /* south brisbane au */
If this is a quoting issue, how do I form the arguments (without using backslashes to escape the spaces)? Of course, I expect I may need a change to the script, that is an acceptable solution, too.
$3
is just to be sure that there are three parameters, then you can use the simpler$#
shell variable, which holds the number of parameters, eg.if [ $# -ne 3 ]
. – user1404316 Feb 08 '18 at 17:45