-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComparing Numbers.sh
More file actions
119 lines (78 loc) · 1.93 KB
/
Copy pathComparing Numbers.sh
File metadata and controls
119 lines (78 loc) · 1.93 KB
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
119
Given two integers, and , identify whether or or .
Exactly one of the following lines:
- X is less than Y
- X is greater than Y
- X is equal to Y
Input Format
Two lines containing one integer each ( and , respectively).
Constraints
-
Output Format
Exactly one of the following lines:
- X is less than Y
- X is greater than Y
- X is equal to Y
Sample Input
Sample Input 1
5
2
Sample Input 2
2
2
Sample Input 3
2
3
Sample Output
Sample Output 1
X is greater than Y
Sample Output 2
X is equal to Y
Sample Output 3
X is less than Y
Explanation
Answer: #!/bin/bash
read X
read Y
if [ "$X" -lt "$Y" ]; then
echo "X is less than Y"
elif [ "$X" -gt "$Y" ]; then
echo "X is greater than Y"
else
echo "X is equal to Y"
fi
explanation: How It Works
read X and read Y
Reads two integers from input (one per line).
if [ condition ]
Bash uses square brackets [ ] for testing conditions.
Comparison operators:
Operator Meaning
-lt less than
-gt greater than
-eq equal to
-ne not equal to
-le less than or equal
-ge greater than or equal
elif / else
elif = “else if”
else = catch-all for remaining case
echo "..."
Prints the appropriate comparison result.
Interview / Job Tips (Bash Conditionals)
Use [ ] for integer comparisons; (( )) can also be used for arithmetic comparisons.
Always quote variables ("$X") to avoid errors if the variable is empty.
Conditional statements are heavily tested in Linux shell scripting, DevOps, and beginner HackerRank problems.
Practice: loops + conditionals + arithmetic together.
What fi Means
In Bash, every if statement must be closed.
The word fi is literally if backwards.
It tells Bash:
“End of this if block.”
Interview Tip:
Always remember: in Bash:
if → fi
case → esac
for/while/until → done
This is a very common question in Linux shell scripting interviews.
fi here closes the if-elif-else statement.
Without it, Bash would throw an “unexpected end of file” error.