[ SYNTAX ] Control Structures in Shell
+2
jeeva
Christopher
6 posters
[ SYNTAX ] Control Structures in Shell
IF_ELSE_FI
Pgm : Find whether the given number is odd or even
You can also use ‘test’ keyword instead of ‘[ ]’.
FOR LOOP
Pgm : To print Natural Numbers
There is another way to use for loop.
WHILE LOOP
Pgm : Print Natural Numbers
CASE
Print Number in words
Pgm :
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
Re: [ SYNTAX ] Control Structures in Shell
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
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
if -else - fi
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' ]
if [ 'expr $n % 2 = 0' ]
or
if [ 'expr $n % 2 eq 0' ]
jennyinhere- Posts : 24
Points : 30
Join date : 2010-02-26
Age : 35
Re: [ SYNTAX ] Control Structures in Shell
Okay . I get the o/p now
jennyinhere- Posts : 24
Points : 30
Join date : 2010-02-26
Age : 35
Re: [ SYNTAX ] Control Structures in Shell
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?
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
Re: [ SYNTAX ] Control Structures in Shell
why are two semi colons ( ; ; ) used in CASE control structure
Nazneennazim- Posts : 10
Points : 16
Join date : 2010-03-01
Re: [ SYNTAX ] Control Structures in Shell
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
Re: [ SYNTAX ] Control Structures in Shell
why two brackets are used in for loop.
Navitha- Posts : 14
Points : 24
Join date : 2010-02-26
Re: [ SYNTAX ] Control Structures in Shell
because it is the syntax...
single bracket has some other significance. so to differentiate between that and for loop arguments, double brackets are used here.
single bracket has some other significance. so to differentiate between that and for loop arguments, double brackets are used here.
Permissions in this forum:
You cannot reply to topics in this forum
|
|