How to write if loop in unix | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

How to write if loop in unix

UNIX for Dummies Questions & Answers


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 07-25-2007
pinky pinky is offline
Registered User
 
Join Date: Jul 2007
Last Activity: 25 November 2010, 5:22 AM EST
Posts: 33
Thanks: 0
Thanked 0 Times in 0 Posts
How to write if loop in unix

Hi Thanks to one who helped me to create a new thread
I am so new to unix and trying to learn the stuff
Thanks for all the help I am getting here
I wanted to execute if loop like

if( x=y || (x=z && x=q))
{
some statements
}

Can I get help how to do write the if loop in unix shell scripting
I know if it is for two like
if [[ x eq y || x eq z ]] but couldn't do it with the above requirement

Thanks in advance
PINKY
Sponsored Links
    #2  
Old 07-25-2007
Shell_Life's Avatar
Shell_Life Shell_Life is offline
Registered User
 
Join Date: Mar 2007
Last Activity: 21 June 2012, 3:19 PM EDT
Location: Bahia, Brazil
Posts: 1,203
Thanks: 1
Thanked 103 Times in 100 Posts
Here is one form of looping:

Code:
#!/bin/ksh
typeset -i mCnt=0
while [ ${mCnt} -le 10 ]
do
  echo "Count = "${mCnt}
  mCnt=${mCnt}+1
done

Sponsored Links
    #3  
Old 07-25-2007
DukeNuke2's Avatar
DukeNuke2 DukeNuke2 is offline Forum Staff  
Soulman
 
Join Date: Jul 2006
Last Activity: 24 November 2014, 12:02 PM EST
Location: Berlin, Germany
Posts: 5,425
Thanks: 55
Thanked 231 Times in 224 Posts
the syntax depends on the shell you are using...

in the korn shell an if statement looks like:

if [ $a = $b]
then
.....
else
.....
fi
    #4  
Old 07-25-2007
pinky pinky is offline
Registered User
 
Join Date: Jul 2007
Last Activity: 25 November 2010, 5:22 AM EST
Posts: 33
Thanks: 0
Thanked 0 Times in 0 Posts
Data

Thanks for the answers
That didn't help me
I want if statement for exactly this condition
if(x=y || (y=z && a=b))

I was looking to write ksh script

Thanks again
Pinky
Sponsored Links
    #5  
Old 07-25-2007
Shell_Life's Avatar
Shell_Life Shell_Life is offline
Registered User
 
Join Date: Mar 2007
Last Activity: 21 June 2012, 3:19 PM EDT
Location: Bahia, Brazil
Posts: 1,203
Thanks: 1
Thanked 103 Times in 100 Posts
Pinky,
Your 'if' statement is not clear on to what type the variables x,y,z,a,b are.

It makes a difference if they are string or numeric.

Numeric:

Code:
x=1
y=1
z=3
a=9
b=8
if [ $x -eq $y -o \( $y -eq $z -a $a -eq $b \) ]; then
 echo "FOUND NUMERIC"
else
 echo "NOT FOUND NUMERIC"
fi

Alphanumeric:

Code:
X='A'
Y='A'
Z='C'
A='M'
B='N'
if [ "$X" = "$Y" -o \( "$Y" = "$Z" -a "$A" = "$B" \) ]; then
 echo "FOUND CAPITAL ALPHA"
else
 echo "NOT FOUND CAPITAL ALPHA"
fi

Sponsored Links
    #6  
Old 07-25-2007
pinky pinky is offline
Registered User
 
Join Date: Jul 2007
Last Activity: 25 November 2010, 5:22 AM EST
Posts: 33
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks I am sorry not to be clear that's for numeric
It worked perfect

Thanks for all your help
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
how to write for loop pdathu Shell Programming and Scripting 4 10-13-2011 08:46 PM
Unable to write to a file within a loop janardhanamk UNIX for Dummies Questions & Answers 1 11-15-2010 07:54 AM
Push records to array during implicit loop and write to file jospan Shell Programming and Scripting 0 01-12-2010 09:06 AM
How to write a If loop.. suri.tyson Shell Programming and Scripting 2 10-15-2008 11:50 AM
trying to write a script to loop through a port info file rcon1 Shell Programming and Scripting 5 01-09-2008 06:59 AM



All times are GMT -4. The time now is 01:47 PM.