bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: help with combining expressions in an if statement


From: Jim
Subject: Re: help with combining expressions in an if statement
Date: Sun, 02 Jan 2005 16:41:44 +0000
User-agent: Mozilla/5.0 (X11; U; Linux i686; en-GB; rv:1.4) Gecko/20030630

Chris F.A. Johnson wrote:
On Sat, 1 Jan 2005, Jim wrote:

Hi,

I am new to bash and am looking for some help. Specifically, I would like to ensure the user supplies two arguments to the shell script - month and year. How should I combine multiple expressions in an if statement?


So far I have the following script :

month=$1
year=$2

if [ -z$month -o -z$year ]; then
echo "Usage: $0 month year";
echo "example: date.sh 01 2005";
exit 1
fi

In other words I want to say if A is true OR B is true then echo output.


The number of arguments is in the variable $#, so you can check
              that:

[ $# -ne 2 ] && {
            echo "Usage: $0 month year"
            echo "example: date.sh 01 2005"
}

              Or you can test each variable:

[ -n "$1" ] && [ -n "$2" ] || {
            echo "Usage: $0 month year"
            echo "example: date.sh 01 2005"
}


Thanks Chris.

As a matter of interest what was wrong with 'if [ -z$month -o -z$year ]' ?

-z means string has a length of zero
-o is the "or" operator which can be used to combine two expressions

If I wanted to combine two expressions in an 'if' test, how would I do it?

Jim






reply via email to

[Prev in Thread] Current Thread [Next in Thread]