Language? #!/bin/bash first line, then ls -l | grep ".txt"
Bash. The first line names the interpreter that runs the file, and | hands one command's output to the next. Hypothetical: a note left on a Linux jump box that lists only the .txt files in a folder.
PowerShell. Verb-Noun commands (cmdlets) pass whole objects through |, and $_ stands for the current object. Hypothetical: on a Windows laptop it lists processes that have used over 100 seconds of CPU time.
3
New cards
Language? import socket then def check(host):
Python. import loads a library and def starts a function whose body is indented. Hypothetical: the opening of a short script that checks whether a hostname resolves.
4
New cards
Not equal for numbers: Bash, Python, PowerShell
Bash -ne, Python !=, PowerShell -ne. Hypothetical: [ $code -ne 200 ] in Bash is true for any response code other than 200.
5
New cards
Greater than for numbers: Bash, Python, PowerShell
Bash -gt, Python >, PowerShell -gt. Hypothetical: $count -gt 3 in PowerShell is true for 4 and false for 3.
6
New cards
Setting a variable to 5: Bash, Python, PowerShell
Bash x=5 (no spaces around =, read back with $x), Python x = 5, PowerShell $x = 5.
7
New cards
AND, OR, NOT: Bash, Python, PowerShell
Bash && || !, Python and or not, PowerShell -and -or -not. Hypothetical: "port is 80 and host is up" reads $p -eq 80 -and $up in PowerShell.
8
New cards
Joining the strings "web" and "01": Bash, Python, PowerShell
Bash "ab", Python a + b, PowerShell $a + $b. All three give web01.
9
New cards
Prints? for i in range(1, 4): print(i) (Python)
1, 2, 3, each on its own line. range stops before the end number. Hypothetical: range(1, 255) covers hosts .1 through .254 of a subnet.
10
New cards
Prints? foreach ($h in "web","mail") { Write-Host $h } (PowerShell)
web, then mail, each on its own line. The braces run once per item, with $h holding the current one. Hypothetical: a two-server list to loop through.
11
New cards
Prints? for h in web mail vpn; do echo $h; done (Bash)
web, mail, vpn, each on its own line. The body sits between do and done. Hypothetical: the same pattern with real hostnames runs one check per server.
12
New cards
Prints? while read line; do echo "$line"; done < hosts.txt (Bash)
Every line of hosts.txt, one at a time. The < hosts.txt at the end feeds the file into the loop. Hypothetical: a 3-line file prints 3 lines.
13
New cards
Python: score is 75. if score >= 90 prints A, elif score >= 70 prints B, else prints C. What prints?
B. Branches are checked top to bottom and the first true one runs; the rest are skipped. 75 fails the first test and passes the second.
14
New cards
Bash: if [ -f hosts.txt ]; then echo found; fi
Prints found only when a regular file named hosts.txt exists. -f tests for a file and fi closes the if. Hypothetical: run in an empty folder, nothing prints.
15
New cards
PowerShell: $ports = 22,80,443 then $ports -contains 80
True. The list goes on the left, and the operator is true when the list includes the value on the right.
16
New cards
Bug? if [$port -eq 22]; then echo ssh; fi (Bash)
Bash needs spaces inside the brackets: [ port−eq22].Withoutthem,[port is read as one unknown command name and the line errors out.
17
New cards
Bug? x = 5 (Bash)
Spaces around = make Bash read x as a command, giving "command not found". Write x=5.
18
New cards
Bug? if x = 5: (Python)
A single = assigns, which is a syntax error inside an if. Comparing takes ==, as in if x == 5:
19
New cards
Bug? if ($x > 5) (PowerShell)
> is a redirect in PowerShell, so this does not compare numbers. It sends $x into a file named 5. Comparing takes -gt.
20
New cards
Bug? Python for loop whose body line is flush against the for line
IndentationError. Python marks blocks by indentation (commonly 4 spaces), so the body must be indented under the for.
21
New cards
Python: import socket then socket.gethostbyname("example.com")
import loads a library that ships with Python, and the call returns the IP address for that name as text.
22
New cards
Python: import requests then requests.get(url).status_code
Third-party library (installed with pip) that fetches a web page. The status code is 200 when the page came back fine. Hypothetical: a missing page returns 404.
23
New cards
Python: def add(a, b): return a + b, then add(2, 3)
5. def defines the function, the parameters receive the arguments, and return hands the result back to the caller.
24
New cards
Python: class Host: with def __init__(self, ip): self.ip = ip, then h = Host("10.0.0.5") and h.ip
"10.0.0.5". A class is a blueprint, h is one object built from it, and __init__ runs at creation and stores the address inside that object.
25
New cards
Bash: what does the pipe in ls | wc -l do?
Sends the file list from ls into wc -l, which counts lines, so the result is the number of files in the folder.
26
New cards
Bash: echo hi > out.txt versus echo hi >> out.txt
> overwrites the file and >> appends to the end. Run twice, the first leaves one line and the second leaves two.
27
New cards
Bash: cut -d"," -f2 data.csv
Prints the second comma-separated column. -d sets the separator and -f picks the field number. Hypothetical: the line bob,22,tx gives 22.
28
New cards
Bash: why write sort names.txt | uniq
uniq only removes duplicates that sit next to each other, so sort first puts matching lines together. Hypothetical: a, b, a stays a, b, a through uniq alone and becomes a, b after sorting.
29
New cards
PowerShell: Get-Process | Export-Csv procs.csv
Writes the objects to a file named procs.csv, one row per process. Hypothetical: it opens in a spreadsheet as a table.
30
New cards
Pick the language: domain-joined Windows server, nothing extra installed, need a list of Active Directory users
PowerShell. It is built into Windows and has Active Directory commands such as Get-ADUser.
31
New cards
Pick the language: Linux box, join the output of two existing command-line tools
Bash. Pipes connect existing commands directly with no libraries needed.
32
New cards
Pick the language: you need a library to parse DNS answers and write the results with a function
Python. import brings in libraries, and functions and classes organize the code. Hypothetical: a script that reads DNS answers with a library and saves them to a file.