Unix C++
Would you like to react to this message? Create an account in a few clicks or log in to continue.

[ SYNTAX ] Control Structures in Shell

+2
jeeva
Christopher
6 posters

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

Go down

[ SYNTAX ] Control Structures in Shell Empty [ SYNTAX ] Control Structures in Shell

Post by Christopher 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
Christopher
Christopher
Admin

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

https://unixcpp.forumotion.com

Back to top Go down

[ SYNTAX ] Control Structures in Shell Empty Re: [ SYNTAX ] Control Structures in Shell

Post by jeeva 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

Back to top Go down

[ SYNTAX ] Control Structures in Shell Empty if -else - fi

Post by jennyinhere 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 : 35

Back to top Go down

[ SYNTAX ] Control Structures in Shell Empty Re: [ SYNTAX ] Control Structures in Shell

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

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

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

https://unixcpp.forumotion.com

Back to top Go down

[ SYNTAX ] Control Structures in Shell Empty Re: [ SYNTAX ] Control Structures in Shell

Post by jennyinhere 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 : 35

Back to top Go down

[ SYNTAX ] Control Structures in Shell Empty Re: [ SYNTAX ] Control Structures in Shell

Post by jennyinhere 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 : 35

Back to top Go down

[ SYNTAX ] Control Structures in Shell Empty Re: [ SYNTAX ] Control Structures in Shell

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

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

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

https://unixcpp.forumotion.com

Back to top Go down

[ SYNTAX ] Control Structures in Shell Empty Re: [ SYNTAX ] Control Structures in Shell

Post by Nazneennazim 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

Back to top Go down

[ SYNTAX ] Control Structures in Shell Empty Re: [ SYNTAX ] Control Structures in Shell

Post by Maithreyi 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 : 36
Location : Haldia

Back to top Go down

[ SYNTAX ] Control Structures in Shell Empty Re: [ SYNTAX ] Control Structures in Shell

Post by Navitha 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

Back to top Go down

[ SYNTAX ] Control Structures in Shell Empty Re: [ SYNTAX ] Control Structures in Shell

Post by Christopher 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.
Christopher
Christopher
Admin

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

https://unixcpp.forumotion.com

Back to top Go down

[ SYNTAX ] Control Structures in Shell Empty Re: [ SYNTAX ] Control Structures in Shell

Post by Sponsored content


Sponsored content


Back to top Go down

Back to top


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

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