- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Re: 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
Discussions
Discussions
Forums
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
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-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.
- « Previous
-
- 1
- 2
- Next »