- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- if with multiple conditions
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-13-2007 12:07 AM
04-13-2007 12:07 AM
if with multiple conditions
if [[ $smonth -le $temp ] && [ $syear -le $year ]] || [[ $smonth -gt $temp ]
&& [ $syear -le $year ]]; then
Thanking you in advance,
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-13-2007 12:31 AM
04-13-2007 12:31 AM
Re: if with multiple conditions
this depends on the logic you want to achieve !
But I would add another set of brackets around the whole statement, as at the moment you have :
if a || b ;then
Please also read:
http://forums1.itrc.hp.com/service/forums/helptips.do?#33 on how to reward any useful answers given to your questions.
So far you have only awarded points to 3 of 11 answers !
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-13-2007 12:31 AM
04-13-2007 12:31 AM
Re: if with multiple conditions
You'll have to explain in words what you would like to accomplish.
As written the statement looks wrong both in actual writing and in intent.
Effectively the statement now says
if $syear le $year
It 'looks' like you can to compare for a date range. Now consider $temp=3 $year=2007.
And $smonth=12, $syear=2006.
Do you want that combination to pass or fail.
Yo may need to glue year and (zero-leading)month together before comparing (as string) or convert both to time-in-seconds.
Hope this helps some,
Hein van den Heuvel (at gmail dot com)
HvdH Performance Consulting
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-13-2007 12:33 AM
04-13-2007 12:33 AM
Re: if with multiple conditions
ksh:
if [ $smonth -le $temp -a $syear -le $year -o $smonth -gt $temp -o $syear -le $year ]
then
...
fi
Regards
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-13-2007 07:48 AM - edited 10-07-2011 01:17 AM
04-13-2007 07:48 AM - edited 10-07-2011 01:17 AM
Re: if with multiple conditions
If you need something understandable, you can write it in C, assuming the above variables are integers:
if (( smonth <= temp && syear <= year || smonth > temp && syear <= year )); then
And as Hein say, this can be simplified to:
if [ (( syear <= year )) ]; then
>Oviwan: ... -a ... -o ... -o
Did you mean to have two -o?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-13-2007 08:10 AM
04-13-2007 08:10 AM
Re: if with multiple conditions
if [ \( $smonth -le $temp -o $smonth -gt $temp \) -a $syear -le $year ]; then
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-13-2007 08:34 AM
04-13-2007 08:34 AM
Re: if with multiple conditions
if [ $syear -le $year ]; then
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-13-2007 10:27 AM
04-13-2007 10:27 AM
Re: if with multiple conditions
That's what Hein said and I agreed.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-14-2007 12:54 AM
04-14-2007 12:54 AM
Re: if with multiple conditions
if [
thanks..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-14-2007 01:35 AM - edited 10-07-2011 01:18 AM
04-14-2007 01:35 AM - edited 10-07-2011 01:18 AM
Re: if with multiple conditions
>Please correct me to the right syntax.
I did, if these are numbers and not strings:
if (( smonth <= temp && syear <= year || smonth > temp && syear <= year )); then
Otherwise you have to use:
if [[ X && Y || Z && W ]]; then
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-14-2007 03:54 AM
04-14-2007 03:54 AM
Re: if with multiple conditions
# if [ \( condition1 -a condition2 \) -o \( condition3 -a condition4 \) ]; then
...
The '-a' means "and"; the '-o' means "or". The parentheses are escaped because they are special to the shell. See the manpages for 'test(1)'.
Regards!
...JRF...
- Tags:
- Test
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-15-2007 01:21 AM
04-15-2007 01:21 AM
Re: if with multiple conditions
I have 4 conditions. I want either 1&2 or 3&4 condition should match. All are string values.
[ condition 1 AND condition 2 ] OR [ Condition 3 AND condition 4 ]
Please help me.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-15-2007 06:34 AM
04-15-2007 06:34 AM
Re: if with multiple conditions
if [[ "$str1" = "$str2" && "$str3" ]] || [[ -z "$str4" && -n "$str5" ]]; then
statements
fi
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-15-2007 07:15 AM
04-15-2007 07:15 AM
Re: if with multiple conditions
> [ condition 1 AND condition 2 ] OR [ Condition 3 AND condition 4 ]
As I wrote before, this is a valid shell syntax for this:
# if [ \( condition1 -a condition2 \) -o \( condition3 -a condition4 \) ]; then
...
For example:
# if [ \( "${A}" = "${B}" -a "${C}" = "${D}" \) -o \( "${X}" != "${Y}" -a "${X}" != "${Z}" \) ]; then
...
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-15-2007 04:11 PM
04-15-2007 04:11 PM
Re: if with multiple conditions
its giving syntax error again. FYI, my shell is ksh.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-15-2007 07:49 PM - edited 10-07-2011 01:22 AM
04-15-2007 07:49 PM - edited 10-07-2011 01:22 AM
Re: if with multiple conditions
All of these syntaxes are valid:
if (( smonth <= temp && syear <= year || smonth > temp && syear <= year )); then
if [[ X && Y || Z && W ]]; then
if [ \( condition1 -a condition2 \) -o \( condition3 -a condition4 \) ]; then
(In the above case, the use of () is redundant but may make it clearer.)
>I believe you guys not understood my question. I want either 1&2 or 3&4 condition should match. All are string values.
Sure we have. But this is the first time you said they were strings. This makes my (( )) solution invalid. Though in most cases if your variables have numeric values, they can be treated as numbers.
>Sandman: if [[ "$str1" = "$str2" && "$str3" ]] || [[ -z "$str4" && -n "$str5" ]]; then
This isn't valid. You only need one set of [[ ]]:
if [[ "$str1" = "$str2" && "$str9" = "$str3" || -z "$str4" && -n "$str5" ]]; then
>JRF:
if [ \( "${A}" = "${B}" -a "${C}" = "${D}" \) -o \( "${X}" != "${Y}" -a "${X}" != "${Z}" \) ]; then
>its giving syntax error again. FYI, my shell is ksh.
That syntax is perfectly valid. You need that exact spacing.
Using your original case:
if [[ ! ( $smonth > $temp ) && ! ( $syear > $year ) || $smonth > $temp && ! ( $syear > $year ) ]]; then
If these variables can be empty, you would need to quote them. If the variables contain a leading "-", you would have to prefix a "X".
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-15-2007 07:51 PM
04-15-2007 07:51 PM
Re: if with multiple conditions
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-16-2007 02:03 PM
04-16-2007 02:03 PM
Re: if with multiple conditions
if [[ ! ( $smonth > $temp ) && ! ( $syear > $year ) || $smonth > $temp && ! ( $syear > $year ) ]]; then
The "!" is needed because there are no <= or >= string compares.