0

I am trying to create a script that checks for two parameters. So far this is the code I have:

#!/bin/bash 

portFile=$1
file=$2

if [ -z “$portFile” ] || [ -z “$file” ]
then
echo “Não tem parâmetros suficientes”
else

The user input should be something like this:

portscanner.sh -n 80 -h hostfile.txt
portscanner.sh -f portfile.txt -h hostfile.txt

My questions is how can I make sure that the first parameter is either -f or -n and make sure that the second one is -h with the file? thank you.


  • The linked article shows some ways of parsing parameters like this (I'd suggest "Method #2: Using bash with getopts"). By doing it in such a standard way, it automatically supports e.g. `portscanner.sh -h hostfile.txt -n 80` as well, just like how `ls -l -a` and `ls -a -l` are the same. Give it a try, and feel free to post about any problems you have with it! – that other guy Nov 20 '19 at 20:22
  • 1
    This might just be a copy-paste issue, but notice that you're using "smart quotes" (`“`) – it should always be straight double quotes (`"`) – Benjamin W. Nov 20 '19 at 20:34

0 Answers0