← Back to list

Day 10 of DevOps: Python and Linux Syntax Basics

Shreya Sinha · 2026-08-11 02:56 · 0 claps · 6.8 min read
#python #linux #bash #coding #hackerrank
Open on Medium ↗
Wiki topics: PFI · Personal Finance LNG · Linguistics & Language 💻 · Programming ☁️ · DevOps & Cloud 🔓 · Open Source

Day 10 of DevOps: Python, Linux and

Photo by Eugene Ga on Unsplash

Photo by Eugene Ga on Unsplash

Pic from Hackerrank by the author

Pic from Hackerrank by the author

Let’s learn some syntax…

Linux/Bash for DevOps

1. while loop

A while loop keeps executing as long as a condition is true.

Syntax

while [ condition ]
do
    commands
done

Example: print numbers 1–5

count=1
while [ $count -le 5 ]
do
    echo $count
    count=$((count + 1))
done

Output:

1
2
3
4
5

How it works

count=1
   ↓
Is count <= 5?
   ↓ Yes
echo count
   ↓
count = count + 1
   ↓
Check again

Common numerical operators:

OperatorMeaning-eqequal-nenot equal-ltless than-leless than or equal-gtgreater than-gegreater than or equal

Example:

while [ $count -lt 10 ]
do
    echo $count
    count=$((count + 1))
done

2. for loop

A for loop is generally used when you want to iterate over a list of values.

Syntax

for variable in list
do
    commands
done

Example

for name in Alice Bob Charlie
do
    echo $name
done

Output:

Alice
Bob
Charlie

Very common DevOps example

Suppose you want to check several servers:

for server in server1 server2 server3
do
    echo "Checking $server"
done

Output:

Checking server1
Checking server2
Checking server3

Using a range

for i in {1..5}
do
    echo $i
done

Output:

1
2
3
4
5

You can also specify an increment:

for i in {1..10..2}
do
    echo $i
done

Output:

1
3
5
7
9

3. if statement

if is used when you want Bash to make a decision.

Syntax

if [ condition ]
then
    commands
fi

Example:

age=25
if [ $age -ge 18 ]
then
    echo "Adult"
fi

Output:

Adult

if-else

if [ condition ]
then
    commands
else
    commands
fi

Example:

age=16
if [ $age -ge 18 ]
then
    echo "Adult"
else
    echo "Minor"
fi

Output:

Minor

if-elif-else

if [ condition1 ]
then
    commands
elif [ condition2 ]
then
    commands
else
    commands
fi

Example:

marks=75
if [ $marks -ge 90 ]
then
    echo "A"
elif [ $marks -ge 60 ]
then
    echo "B"
else
    echo "C"
fi

Output:

B

4. Comparing numbers

This is very important in Bash.

Do not normally use <, > or = for numerical comparisons inside [ ].

Use:

-eq
-ne
-lt
-le
-gt
-ge

Examples

a=10
b=20

Equal

if [ $a -eq $b ]
then
    echo "Equal"
fi

Not equal

if [ $a -ne $b ]
then
    echo "Not equal"
fi

Less than

if [ $a -lt $b ]
then
    echo "$a is smaller"
fi

Less than or equal

if [ $a -le $b ]
then
    echo "a <= b"
fi

Greater than

if [ $a -gt $b ]
then
    echo "a is greater"
fi

Greater than or equal

if [ $a -ge $b ]
then
    echo "a >= b"
fi

5. Comparing characters/strings

For strings, you use different operators.

=
!=

Example:

name="Shreya"
if [ "$name" = "Shreya" ]
then
    echo "Name matches"
fi

Output:

Name matches

Not equal

name="Shreya"
if [ "$name" != "Rahul" ]
then
    echo "Names are different"
fi

Why do we put quotes around variables?

Prefer:

if [ "$name" = "Shreya" ]

rather than:

if [ $name = "Shreya" ]

Because if the variable is empty or contains spaces, quoting prevents many Bash parsing problems.

For example:

name=""

This is safer:

if [ "$name" = "Shreya" ]

6. String comparison with [[ ]]

You’ll often see modern Bash scripts use:

if [[ "$name" == "Shreya" ]]
then
    echo "Matched"
fi

Notice:

[ "$name" = "Shreya" ]

versus:

[[ "$name" == "Shreya" ]]

For Bash scripting, I recommend becoming comfortable with [[ ]] because it is more robust and gives you more capabilities.

7. Character/string operators

OperatorMeaning= or ==equal!=not equal-zstring is empty-nstring is not empty

Example:

name="Shreya"
if [ -n "$name" ]
then
    echo "Name exists"
fi

Check if empty

name=""
if [ -z "$name" ]
then
    echo "Name is empty"
fi

8. Combining conditions

You can combine conditions with:

&&
||

Example:

age=25
country="India"
if [[ $age -ge 18 && $country == "India" ]]
then
    echo "Condition satisfied"
fi

&& means AND.

|| means OR.

Example:

if [[ $age -lt 18 || $age -gt 60 ]]
then
    echo "Special category"
fi

9. A very useful DevOps example

Suppose you have a script that checks whether a service is running:

service="nginx"
if systemctl is-active --quiet "$service"
then
    echo "$service is running"
else
    echo "$service is not running"
fi

This is extremely common in Bash automation.

Another example using a loop:

for service in nginx docker ssh
do
    if systemctl is-active --quiet "$service"
    then
        echo "$service is running"
    else
        echo "$service is down"
    fi
done

This combines **for + if** — exactly the kind of pattern you'll use in DevOps scripts.

Quick cheat sheet

# WHILE
while [ condition ]
do
    commands
done
# FOR
for variable in list
do
    commands
done
# IF
if [ condition ]
then
    commands
fi
# IF / ELSE
if [ condition ]
then
    commands
else
    commands
fi
# NUMBER COMPARISON
-eq   # ==
-ne   # !=
-lt   # <
-le   # <=
-gt   # >
-ge   # >=
# STRING COMPARISON
= / == # equal
!=     # not equal
-z     # empty
-n     # not empty
# LOGICAL
&&     # AND
||     # OR
$(()) -> for doing arithmetic operations

One important distinction to remember

Think of it this way:

Numbers       → -eq -ne -lt -le -gt -ge
Strings       → = == != -z -n
Files         → -f -d -e -r -w -x

For DevOps, the file operators are the next important piece because you’ll frequently write scripts like “if this file exists, do X; if this directory doesn’t exist, create it.”

The Python equivalent, keeping the same structure as the Bash notes.

1. while loop

A while loop keeps running as long as a condition is True.

Syntax

while condition:
    commands

Example: print 1–5

count = 1
while count <= 5:
    print(count)
    count = count + 1

Output:

1
2
3
4
5

You can also write:

count += 1

instead of:

count = count + 1

2. for loop

Python’s for loop is used to iterate over a sequence/iterable.

Syntax

for variable in sequence:
    commands

Example

for name in ["Alice", "Bob", "Charlie"]:
    print(name)

Output:

Alice
Bob
Charlie

Very common DevOps example

servers = ["server1", "server2", "server3"]
for server in servers:
    print(f"Checking {server}")

Output:

Checking server1
Checking server2
Checking server3

Using range()

for i in range(1, 6):
    print(i)

Output:

1
2
3
4
5

Remember: the end value is excluded.

range(1, 6)

means:

1, 2, 3, 4, 5

Increment by 2

for i in range(1, 10, 2):
    print(i)

Output:

1
3
5
7
9

3. if statement

Python uses if to make decisions.

Syntax

if condition:
    commands

Example:

age = 25
if age >= 18:
    print("Adult")

Output:

Adult

Important difference from Bash

Python uses indentation instead of then and fi.

Bash:

if [ $age -ge 18 ]
then
    echo "Adult"
fi

Python:

if age >= 18:
    print("Adult")

4. if-else

Syntax

if condition:
    commands
else:
    commands

Example:

age = 16
if age >= 18:
    print("Adult")
else:
    print("Minor")

Output:

Minor

5. if-elif-else

Python uses elif instead of Bash's elif syntax.

marks = 75
if marks >= 90:
    print("A")
elif marks >= 60:
    print("B")
else:
    print("C")

Output:

B

6. Comparing numbers

Python is much simpler than Bash here.

You can directly use:

==    equal
!=    not equal
<     less than
<=    less than or equal
>     greater than
>=    greater than or equal

Examples

a = 10
b = 20

Equal

if a == b:
    print("Equal")

Not equal

if a != b:
    print("Not equal")

Less than

if a < b:
    print("a is smaller")

Less than or equal

if a <= b:
    print("a <= b")

Greater than

if a > b:
    print("a is greater")

Greater than or equal

if a >= b:
    print("a >= b")

7. Comparing characters / strings

Python uses:

==
!=

Example

name = "Shreya"
if name == "Shreya":
    print("Name matches")

Output:

Name matches

Not equal

name = "Shreya"
if name != "Rahul":
    print("Names are different")

8. Empty strings

Python doesn’t have Bash’s -z and -n.

Instead, Python commonly uses:

if not name:

to check whether a string is empty.

Example:

name = ""
if not name:
    print("Name is empty")

You can also explicitly check:

if name == "":
    print("Name is empty")

For non-empty:

name = "Shreya"
if name:
    print("Name exists")

9. Comparing characters

A character is simply a string of length 1 in Python.

character = "A"
if character == "A":
    print("Character is A")

You can also compare characters alphabetically because strings have an ordering:

character = "B"
if character > "A":
    print("B comes after A")

For example:

if "b" > "a":
    print("b is greater")

This is based on the characters’ Unicode values.

Be careful with uppercase/lowercase:

print("A" < "a")

This is True because uppercase and lowercase letters have different Unicode values.

10. Combining conditions

Python uses:

and
or
not

AND

age = 25
country = "India"
if age >= 18 and country == "India":
    print("Condition satisfied")

Both conditions must be True.

OR

if age < 18 or age > 60:
    print("Special category")

At least one condition must be True.

NOT

is_running = False
if not is_running:
    print("Service is not running")

11. Very useful DevOps example

Suppose you want to check whether a service is running.

With Python, you can execute a Linux command using subprocess:

import subprocess
service = "nginx"
result = subprocess.run(
    ["systemctl", "is-active", "--quiet", service]
)
if result.returncode == 0:
    print(f"{service} is running")
else:
    print(f"{service} is not running")

And you can combine this with a for loop:

import subprocess
services = ["nginx", "docker", "ssh"]
for service in services:
    result = subprocess.run(
        ["systemctl", "is-active", "--quiet", service]
    )
    if result.returncode == 0:
        print(f"{service} is running")
    else:
        print(f"{service} is down")

That’s a very realistic DevOps Python scripting pattern.

12. Bash vs Python cheat sheet

ConceptBashPythonEqual number-eq==Not equal-ne!=Less than-lt<Less/equal-le<=Greater than-gt>Greater/equal-ge>=AND&&andORNOT!notEqual string===Empty string-znot stringwhilewhile [ condition ]while condition:forfor x in listfor x in list:Block endingfi,done`indentation

The biggest thing to remember

Bash:

if [ $age -ge 18 ]
then
    echo "Adult"
fi

Python:

if age >= 18:
    print("Adult")

So Python is generally much cleaner for conditions because you don’t have Bash’s -eq, -gt, [ ], then, fi, etc.


메타데이터
post_id
f47a754065bb
slug
day-10-of-devops-python-and-linux-syntax-basics-f47a754065bb
url
https://medium.com/@shreyajung/day-10-of-devops-python-and-linux-syntax-basics-f47a754065bb
canonical_url
https://medium.com/@shreyajung/day-10-of-devops-python-and-linux-syntax-basics-f47a754065bb
author_url
https://medium.com/@shreyajung
status
ok
fetched_at
2026-08-27 22:07:21