This here’s a machine-translated text that might contain errors!
If ya done the same task fer the third time, it’s high time to automate it. Scriptin’ is all about lettin’ the machine do the dull work fer ya, faster and without forgettin’ a thing.
Why Automate?
Some tasks in IT operations are done often:
- Updatin’ servers
- Creatin’ users
- Backin’ things up
- Restartin’ services after an update
- Checkin’ if services are runnin’
Dooin’ these by hand every time takes time, and it’s easy to forget a step. A script does the same thing every time, without complainin’.
Bash: Scriptin’ in Linux
Bash is the shell (terminal) in most Linux distributions, and it can also run scripts. A Bash script is just a text file with commands that are run in order.
Yer First Script
Make a file called oppdater.sh:
#!/bin/bash
# Update the package list and install updates
echo "Updating the system..."
sudo apt update
sudo apt upgrade -y
echo "Done!"
Make the file runnable and run ‘er:
chmod +x oppdater.sh
./oppdater.sh
# Gjør skriptet kjørbart
# Kjør skriptet
#!/bin/bash
That first line there’s called a shebang. It tells the operatin’ system to run the file with Bash. Without it, the system ain’t got a clue which program’s supposed to read the script.
Variables and Input
Now, listen up. When wranglin’ code, ya gotta have places to store things – that’s what variables are for. Think of ‘em as branded saddlebags for yer data.
Input is how ya get information into yer program. It’s like askin’ a fella what he needs before ya start fixin’ his wagon.
Here’s how it generally works:
- Variables: These hold values. Ya give ‘em a name, like “number_of_cattle” or “brandin_iron_temp”.
- Input: Yer program asks the user (or gets it from a file, or another source) for some information.
- Assignment: Ya take that input and put it into a variable. Like fillin’ those saddlebags.
Let’s say ya want to know how many head of cattle a rancher has. Yer code might look somethin’ like this:
number_of_cattle = input("How many cattle ya got, partner? ")
print("Well now, ya got", number_of_cattle, "head o' cattle!")
See? We asked for the number, and then we stored it in the number_of_cattle variable. Simple as ropin’ a calf.
Keep in mind, input usually comes in as text. If ya need a number, ya gotta convert it. Otherwise, yer program might get mighty confused. We’ll cover that in a bit.
#!/bin/bash
BRUKERNAVN=$1
if [ -z "$BRUKERNAVN" ]; then
echo "Usage: ./lag_bruker.sh <username>"
exit 1
fi
sudo adduser "$BRUKERNAVN"
sudo usermod -aG sudo "$BRUKERNAVN"
echo "User $BRUKERNAVN created with sudo access."
Now, this here script takes a username as an argument ($1 is the first argument, ya see). If ya run ./lag_bruker.sh ola, it’ll go and create the user ola and give ‘em sudo access.
Handy Bash Concepts
| Concept | Example | What it does |
|---|---|---|
| Variable | NAME="Ola" | Stores a value |
| Argument | $1, $2 | Input from the command line |
| If-check | if [ -f "file.txt" ]; then | Checks if a file exists |
| For-loop | for f in *.log; do | Does somethin’ for each file that matches |
| Pipe | cat log.txt | grep "ERROR" | Sends output from one command to another |
PowerShell: Scriptin’ in Windows
PowerShell’s Windows’ answer to Bash, but it’s more object-oriented. It works with objects instead of just text, makin’ it powerful for managin’ Windows systems.
Example: Check Disk Space
Now, listen up, partner. Checkin’ yer disk space is mighty important. If yer hard drive’s full, things ain’t gonna run smooth. Here’s how ya do it:
- Windows: Right-click on yer drive in File Explorer and select “Properties.” It’ll tell ya how much space ya got used and how much is free.
- macOS: Click the Apple menu, then “About This Mac,” then “Storage.” That’ll show ya what’s takin’ up space.
- Linux: Open a terminal and type
df -h. That’ll give ya a list of yer disks and how full they are.
Keep an eye on that space, and ya won’t run into trouble.
# Check available disk space
Get-PSDrive -PSProvider FileSystem | Select-Object Name,
@{Name="Used (GB)"; Expression={[math]::Round($_.Used / 1GB, 2)}},
@{Name="Free (GB)"; Expression={[math]::Round($_.Free / 1GB, 2)}}
Example: Installin’ Software with Winget
Now, listen up, ‘cause we’re gonna talk ‘bout gettin’ software installed usin’ Winget. It’s a mighty fine tool fer doin’ just that, straight from yer command line.
Winget is a package manager fer Windows, kinda like how folks use apt on Linux or brew on macOS. It lets ya search, install, upgrade, and uninstall software without havin’ to click around a bunch.
Slik bruker du Winget:
- Søk etter programvare:
winget search <søkestreng>– This here’ll find software matchin’ what yer lookin’ fer. - Installer programvare:
winget install <pakkenavn>– This’ll get the software installed, quick as a wink. - Oppgrader programvare:
winget upgrade– Keeps yer software up to date, so it runs smooth. - Avinstaller programvare:
winget uninstall <pakkenavn>– Gets rid of software ya don’t need no more.
Fer example, if ya wanted to install 7-Zip, ya’d type: winget install 7zip. Simple as that!
# Install several programs at once
winget install Mozilla.Firefox
winget install Microsoft.VisualStudioCode
winget install Git.Git
winget install Python.Python.3.12
This here’s a whole lot faster than downloadin’ and installin’ ever’thin’ by hand. Ya can save the list in a script and run it ever’ time ya set up a new machine.
Task 1 - Build an Update Script
Write a Bash script that:
- Updates the package list
- Installs all available updates
- Restarts Nginx (or another service you’re runnin’)
- Prints a message when it’s done
Run it on one of yer VMs and see if it works right.
Task 2 - Automate Machine Setup
Create a PowerShell script (.ps1) or a simple list of winget install commands for all the programs ya need on a fresh PC. Consider:
- Which programs do ya always install?
- Can ya also configure some settings with the script?
Next time ya set up a machine, just run the script instead of spendin’ an hour on manual installation.
Summin’ It Up
- If ya find yerself doin’ somethin’ more’n twice, reckon ya oughta automate it.
- Bash (Linux) and PowerShell (Windows) are the most common tools for scriptin’.
- Bash scripts are just text files with commands that run one after the other.
- PowerShell works with objects and is mighty powerful for Windows managin’.
- Variables, arguments, if-checks, and loops are the main buildin’ blocks.