[ SYNTAX ] Control Structures in Shell

Unix C++ :: UNIX CPP :: UNIX :: SYNTAX

Go down

[ SYNTAX ] Control Structures in Shell

Post by Christopher on Tue Mar 09, 2010 10:59 am

IF_ELSE_FI

Pgm : Find whether the given number is odd or even

Code:
read n
if [ `expr $n % 2` = 0 ]
then
echo Even
else
echo Odd
fi

You can also use ‘test’ keyword instead of ‘[ ]’.


FOR LOOP

Pgm : To print Natural Numbers

Code:
read n
for((i=1;i<=n;i++))
do
echo $i
done

There is another way to use for loop.

Code:
for i in 1 2 3 4 5
do
echo $i
done


WHILE LOOP

Pgm : Print Natural Numbers

Code:
read n
i=1
while [ $i –le $n ]
do
echo $i
i=`expr $i + 1`
done

CASE

Print Number in words

Pgm :

Code:
read n
case $n in
1) echo "One";;
2) echo "Two";;
3) echo "Three";;
4) echo "Enter number between one and three";;
esac


Last edited by Christopher on Tue Mar 09, 2010 1:46 pm; edited 1 time in total
avatar
Christopher
Admin

Posts : 240
Points : 429
Join date : 2010-02-26
Age : 30

View user profile http://unixcpp.forumotion.com

Back to top Go down

Re: [ SYNTAX ] Control Structures in Shell

Post by jeeva on Tue Mar 09, 2010 12:55 pm

also this works


read n
case $n in

hi) echo "One";;
hello) echo "Two";;
you) echo "Three";;
me) echo "Enter number between one and three";;
esac

jeeva

Posts : 50
Points : 93
Join date : 2010-03-04

View user profile

Back to top Go down

if -else - fi

Post by jennyinhere on Tue Mar 09, 2010 1:05 pm

Am gettin the o/p for if-else-fi only if I give either of these formats

if [ 'expr $n % 2 = 0' ]
or
if [ 'expr $n % 2 eq 0' ]

jennyinhere

Posts : 24
Points : 30
Join date : 2010-02-26
Age : 30

View user profile

Back to top Go down

Re: [ SYNTAX ] Control Structures in Shell

Post by Christopher on Tue Mar 09, 2010 1:12 pm

dont use quote ( ' ) to enclose expr. use acute ( ` ).
avatar
Christopher
Admin

Posts : 240
Points : 429
Join date : 2010-02-26
Age : 30

View user profile http://unixcpp.forumotion.com

Back to top Go down

Re: [ SYNTAX ] Control Structures in Shell

Post by jennyinhere on Tue Mar 09, 2010 1:14 pm

Okay . I get the o/p now

jennyinhere

Posts : 24
Points : 30
Join date : 2010-02-26
Age : 30

View user profile

Back to top Go down

Re: [ SYNTAX ] Control Structures in Shell

Post by jennyinhere on Tue Mar 09, 2010 1:25 pm

for((i=1;1<=n;1++))

The above gives an error ./forloop.sh: line 2: ((: 1++ : syntax error: operand expected (error token is " ")

for (( i = 1 ; i <= n ; i++ )) Whereas this gives me the o/p

What is the problem with the previous for loop?

jennyinhere

Posts : 24
Points : 30
Join date : 2010-02-26
Age : 30

View user profile

Back to top Go down

Re: [ SYNTAX ] Control Structures in Shell

Post by Christopher on Tue Mar 09, 2010 1:48 pm

sorry Rolling Eyes .. it was i++ only.. typed wrongly.
avatar
Christopher
Admin

Posts : 240
Points : 429
Join date : 2010-02-26
Age : 30

View user profile http://unixcpp.forumotion.com

Back to top Go down

Re: [ SYNTAX ] Control Structures in Shell

Post by Nazneennazim on Wed Mar 10, 2010 11:46 am

why are two semi colons ( ; ; ) used in CASE control structure

Nazneennazim

Posts : 10
Points : 16
Join date : 2010-03-01

View user profile

Back to top Go down

Re: [ SYNTAX ] Control Structures in Shell

Post by Maithreyi on Wed Mar 10, 2010 11:58 am

The two semicolons are used to denote the end of that particular case .

Maithreyi

Posts : 76
Points : 142
Join date : 2010-03-03
Age : 30
Location : Haldia

View user profile

Back to top Go down

Re: [ SYNTAX ] Control Structures in Shell

Post by Navitha on Wed Mar 10, 2010 2:00 pm

why two brackets are used in for loop.

Navitha

Posts : 14
Points : 24
Join date : 2010-02-26

View user profile

Back to top Go down

Re: [ SYNTAX ] Control Structures in Shell

Post by Christopher on Wed Mar 10, 2010 3:14 pm

because it is the syntax... lol!

single bracket has some other significance. so to differentiate between that and for loop arguments, double brackets are used here.
avatar
Christopher
Admin

Posts : 240
Points : 429
Join date : 2010-02-26
Age : 30

View user profile http://unixcpp.forumotion.com

Back to top Go down

Re: [ SYNTAX ] Control Structures in Shell

Post by Sponsored content


Sponsored content


Back to top Go down

Back to top

- Similar topics

Unix C++ :: UNIX CPP :: UNIX :: SYNTAX

 
Permissions in this forum:
You cannot reply to topics in this forum