-
Notifications
You must be signed in to change notification settings - Fork 0
/
16.break-continue.txt
113 lines (78 loc) · 1.64 KB
/
16.break-continue.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
break
------
Based on some condition, if we want to beak loop execution (i.e to come out of loop) then we should go for break statement.
#! /bin/bash
i=1
while [ $i -le 10 ]
do
if [ $i -eq 5 ];then
break
fi
echo $i
let i++
done
______________________________________
continue statement:
------------------
We can use coninue statement to skip current itereation and continue for the next iteration.
# to print odd numbers.
#! /bin/bash
i=0
while [ $i -le 10 ]
do
let i++
if [ $[i%2] -eq 0 ];then
continue
fi
echo $i
done
******Output*********
[abhijit@localhost ~]$ ./test36.sh
1
3
5
7
9
11
***********************
Q. write a script to read file name and display its content.
with extra masala to this script.
#! /bin/bash
while [ true ]
do
read -p " enter file name to display its content: " fname
#check whether file exist/regular file or not.
if [ -f $fname ];then
echo "the content of $fname: "
cat $fname
else
echo "$fname may not exist/regular file"
fi
read -p "do you want to display content of another file[yes/no]: " option
case $option in
[yY]|[yY][eE][sS])
continue
;;
[nN]|[nN][oO])
break
;;
*)
echo "please select one of Yes|No only."
;;
esac
done
echo "thanks for using application"
________________________________________________________
Q. Write a script that reads a astring as input and print its reverse?
#! /bin/bash
read -p "enter any string to find reverse: " str
len=$(echo -n $str | wc -c)
output=""
while [ $len -ge 1 ]
do
ch=$(echo -n $str | cut -c $len)
output=$output$ch
let len--
done
echo "the original string: $str"
echo "the reversed string: $output"