Multiple condition checking in bash

Hi All,
I am trying to check if two variables have value assigned to it.
i am doing it like

if [[ -n "$host_name" && -n "$host_file" ]]
then
    echo "Please specify either single hostname or host file for the report"
    usage
    exit
fi

But its not working for it.Even i specify values for both variables it dont go inside if.
what i am doing wrong in this?
Appreciate your help.

check here,

thanks latika

---------- Post updated at 05:48 AM ---------- Previous update was at 05:24 AM ----------

Question:

Why i cant use and( && ) as i did above?

Anyone can please explain to me?

Your code is right for at least ksh93, bash and zsh. Give the full script and version of your shell.

Hi,
I am using bash :

here's the code:

#!/bin/bash

usage()
{
    cat << EOF
usage: $0 options

This script genrates the report in xls format.

OPTIONS:
   -h      Show this message
   -s      Start date for the report(format: yyyy/mm/dd).
   -e      End date for the report(format: yyyy/mm/dd).
   -m      hostname fot which report is generated
   -f      hostfile(each host in new line)

Don't specify option(m,f) if you need to generate for all hosts.
EOF
}
start_date=
end_sate=
host_name=
host_file=

while getopts "hs:e:m:f" OPTION
do
    case $OPTION in
        h)
            usage
            exit 1
            ;;
        s)
            start_date=$OPTARG
            ;;
        e)
            end_date=$OPTARG
            ;;
        m)
            host_name=$OPTARG
            ;;
        f)
            host_file=$OPTARG
            ;;
        ?)
            usage
            exit
            ;;
    esac
done
if [ -z $start_date ]
then
    echo "Please specify start date for the report"
    usage
    exit
elif [ -z $end_date ]
then
    echo "Please specify end date for the report"
    usage
    exit
fi
if [[ -n "$host_name" && -n "$host_file" ]]
then
    echo "Please specify either single hostname or host file for the report"
    usage
    exit
fi

echo $start_date
echo $end_date
echo $host_name
echo $host_file

Cant figure out why its not working.

---------- Post updated 08-17-11 at 12:30 AM ---------- Previous update was 08-16-11 at 04:30 AM ----------

can anyone please help?

Your var "host_file" gets the previous value of $OPTARG. Just add a colon.

   -f      hostfile(each host in new line)

...

while getopts "hs:e:m:f:" OPTION

...

        f)
            host_file=$OPTARG

With "set -x" or bash debugger you could see what happens.

1 Like

Thanks a lot yazu.... wonder why i was not able to detect it.

Humans(like me) are so stupid sometimes :smiley: