combination between || and && in IF condition with ksh


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting combination between || and && in IF condition with ksh
# 1  
Old 02-15-2010
combination between || and && in IF condition with ksh

Dear All,
Please advice about this issue.
when i run this line in a script
Code:
if [ "${x}" = "test3" ] && [ "${y}" != "test3" ] || [ "${x}" = "test2" ] && [ "${y}" != "test2" ] || [ "${x}" = "test1" ] && [ "${y}" != "test1" ]

if i enter $x = test3 and $y = test1 the If condition apply while it should not
Best Regards

Last edited by islam.said; 02-15-2010 at 07:56 AM.. Reason: code tags, please...
# 2  
Old 02-15-2010
I don't think that "&&" and "||" are suitable for nested conditions. Paraphrasing the condition into nested "ands" and "ors" we get.

Code:
#!/bin/ksh
x="test3"
y="test1"
if [ \( "${x}" = "test3" -a "${y}" != "test3" \) -o \( "${x}" = "test2" -a "${y}" != "test2" \) -o \( "${x}" = "test1" -a "${y}" != "test1" \) ]
then
        echo "true"
else
        echo "false"
fi

true

Was this what you meant?
# 3  
Old 02-15-2010
thank you so much
it works gr8
Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

SFTP Shell Script Get & Delete && Upload & Delete

Hi All, Do you have any sample script, - auto get file from SFTP remote server and delete file in remove server after downloaded. - only download specify filename - auto upload file from local to SFTP remote server and delete local folder file after uploaded - only upload specify filename ... (3 Replies)
Discussion started by: weesiong
3 Replies

2. AIX

Elseif & for condition in AIX

I'm using the below statements in my script if && then sqlplus sysadm/abcdefgh12@${dbarr} @/u1/scripts/ResetPswd.sql elif then for idx in 0 1 2 3 4 5 6 7 do sqlplus sysadm/abcdefgh12@${dbarr} @/u1/scripts/ResetPswd.sql done else exit fi It give me... (5 Replies)
Discussion started by: Pandee
5 Replies

3. Shell Programming and Scripting

awk question: How to print condition of NR & NF together.

Experts: LINE1 :This is line one The FIRST line of the file. LINE2 :This is line two LINE3 :This is line three with 8 fileds LINE4 :This is line four LINE5 :This is line five LINE6 :This is line six with 8 fileds I want to delete line 1, and then process the file and want to print lines... (2 Replies)
Discussion started by: rveri
2 Replies

4. Shell Programming and Scripting

Replace & sign to &amp word

Hi, I have text file abc.txt. In this file, I have the following data. Input: Mr Smith &amp Mrs Smith Mr Smith &apos Mrs Smith Mr Smith & Mrs Smith Mr Smith& Mrs Smith Mr Smith &Mrs Smith Output: Mr Smith &amp Mrs Smith Mr Smith &apos Mrs Smith Mr Smith &amp Mrs Smith Mr Smith&amp... (4 Replies)
Discussion started by: naveed
4 Replies

5. Shell Programming and Scripting

Advice using cut & echo combination commands

Hi, I am cutting data from a fixed length test file and then writing out a new record using the echo command, the problem I have is how to stop multiple spaces from being written to the output file as a single space. Example: cat filea | while read line do field1=`echo $line | cut -c1-2` ... (6 Replies)
Discussion started by: dc18
6 Replies

6. Shell Programming and Scripting

replace & with & xml file

Hello All I have a xml file with many sets of records like this <mytag>mydata</mytag> <tag2>data&</tag2> also same file can be like this <mytag>mydata</mytag> <tag2>data&</tag2> <tag3>data2&amp;data3</tag3> Now i can grep & and replace with &amp; for whole file but it will replace all... (4 Replies)
Discussion started by: lokaish23
4 Replies

7. Shell Programming and Scripting

PHP read large string & split in multidimensional arrays & assign fieldnames & write into MYSQL

Hi, I hope the title does not scare people to look into this thread but it describes roughly what I'm trying to do. I need a solution in PHP. I'm a programming beginner, so it might be that the approach to solve this, might be easier to solve with an other approach of someone else, so if you... (0 Replies)
Discussion started by: lowmaster
0 Replies

8. Shell Programming and Scripting

Bash & Ksh

I am using a Linux machine and it's default shell is BASH . I have korn shell script and inside it's setting environment variables. But after execuitng the script those env values are not holding the values . If I run the script outside of that ksh script with source a.sh , it's able to hold to... (1 Reply)
Discussion started by: talashil
1 Replies

9. UNIX for Dummies Questions & Answers

Combination of find -xargs & wc -l

Dear all, I have to calculate sum of record count of files of the specified directory. First I tried the following way which prints one or more outputs. How can I sum of this output? find /home/work/tmp/1/O/ -type f -print0 | xargs -0 wc -l | grep total 1666288 total 1073908 total ... (4 Replies)
Discussion started by: mr_bold
4 Replies
Login or Register to Ask a Question