-
Notifications
You must be signed in to change notification settings - Fork 0
/
loops.sh
118 lines (94 loc) · 1.31 KB
/
loops.sh
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/usr/bin/bash
# loops
# 1. while loop
# Syntax:
# while [ condition ]
# do
# commands
# done
# Example:
n=1
while [ "$n" -le 10 ]
do
echo "$n"
n=$(( n+1 ))
done
echo "After using Sleep command"
#using sleep and open command with while loop
n=1
while [ "$n" -le 5 ]
do
sleep 1
echo "$n"
n=$(( n+1 ))
done
#Read content in Bash
while read p
do
echo $p
done <basics.sh
# second way to read
cat basics.sh | while read p
do
echo $p
done
######### UNTIL LOOP ########
# Syntax:
# until [ condition ]
# do
# commands
# done
echo "Until loop"
read -p "Enter a number:" num
until [ $num -le 10 ]
do
echo "$num is greater than 10"
num=$(( num-1 ))
done
####### FOR LOOP ########
# Syntax
# 1.
# for variable in {1,2,....}
# do
# Conditions
# done
# 2.
# for variable in file1 file2..
# do
# commands
# done
# 3.
# for OUTPUT in $(linux-or-unix-command-here)
# do
# command1 on $OUTPUT
# command2 on $OUTPUT
# done
# 4.
# for (( exp1; exp2; exp3))
# do
# commands
# done
echo "FOR LOOP"
# example
for command in ls pwd date
do
echo "------- $command ---------"
$command
done
# 2.
for (( i=0; i<=5 ; i++))
do
echo $i
done
########## SELECT LOOP ########
# Syntax:
# select varname in list
# do
# commands
# done
# Example
echo " SELECT LOOP"
select name in rachana rachel rohan ben tom
do
echo "$name Selected"
done