Shiver me timbers, scriptin' with Bash 'n PowerShell

Skip to content

Avast ye! This be a machine-translated text, an’ it may contain errors, aye!

If ye’ve done the same task for the third time, ‘tis time to automate it. Scriptin’ be about lettin’ the machine do the dreary work for ye, faster and without forgettin’ a thing.

Why Automate?

Some tasks in IT operations be done often:

  • Update the servers
  • Create users
  • Take backup
  • Restart services after an update
  • Check that services be runnin’

Do’n these by hand each time takes time, an’ it be easy to forget a step. A script does the same each time, without complainin’.

Bash: Scriptin’ in Linux

Bash be the shell (the terminal, aye) in most Linux distributions, an’ it can also run scripts. A Bash script be nothin’ more than a text file with commands that be run in order, savvy?

Yer First Script

Create a file named oppdater.sh:

#!/bin/bash
# Avast ye, update the package list and install updates!
echo "Oppdaterer systemet..."
sudo apt update
sudo apt upgrade -y
echo "Ferdig!"

Make the file runnable an’ hoist the colours an’ run it:

chmod +x oppdater.sh
# Gjør skriptet kjørbart, aye!
./oppdater.sh
# Kjør skriptet, shiver me timbers!

#!/bin/bash

The first line be called a shebang, aye. It tells the operating system that the file be runnin’ with Bash. Without it, the system knows not which program to interpret the script with.

Variables and Booty

A ship needs its stores, aye? Variables be like the hold o’ yer vessel – they keep the treasures ye need for yer calculations. Input, then, be the goods ye load onto the ship from port, the information ye give the vessel to work with.

Think o’ it this way: ye might have a variable for the number o’ cannons (antallKanoner), and ye’d input the actual count – say, 10 – when ye prepare for a skirmish. Or a variable for the amount o’ grog (grogBeholdning), and ye input how many barrels ye’ve got aboard.

These variables and inputs be crucial for any calculation or task yer ship – or yer program – needs to perform. Without ‘em, ye’d be sailin’ blind!

#!/bin/bash
BRUKERNAVN=$1

if [ -z "$BRUKERNAVN" ]; then
    echo "Avast! Use: ./lag_bruker.sh <brukernavn>"
    exit 1
fi

sudo adduser "$BRUKERNAVN"
sudo usermod -aG sudo "$BRUKERNAVN"
echo "Shiver me timbers! User $BRUKERNAVN created with sudo access."

Here be the script takin’ a username as an argument ($1 be the first argument, ye see). If ye run ./lag_bruker.sh ola, it’ll create the user ola an’ grant ‘em sudo access, aye.

Useful 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 be Windows’ answer to Bash, but ‘tis more object-oriented, aye. It works with objects instead o’ just text, makin’ it powerful for managin’ Windows systems.

Example: Check Disk Space

Aye, ‘tis time to be checkin’ yer disk space, savvy?

# Avast ye! Check the hold for space!
Get-PSDrive -PSProvider FileSystem | Select-Object Name, 
    @{Name="Brukt (GB)"; Expression={[math]::Round($_.Used / 1GB, 2)}},
    @{Name="Ledig (GB)"; Expression={[math]::Round($_.Free / 1GB, 2)}}

Example: Installin’ Software with Winget

Winget be a package manager fer Windows, allowin’ ye to install software from the command line, aye. It be makin’ life easier fer pirates like ourselves who prefer not to be clickin’ ‘round with graphical installers.

Hvordan bruke Winget?

To install a program, ye’ll be usin’ the winget install command, followed by the name o’ the program ye be wantin’. Fer example:

winget install Spotify

This’ll download and install Spotify fer ye, swift as the wind.

Søke etter programvare

If ye be unsure o’ the exact name, ye can search fer software with the winget search command:

winget search video editor

This’ll return a list o’ packages matchin’ yer search, along with their IDs. Then ye can install ‘em by usin’ their ID.

Oppgradere programvare

To keep yer software up to date, use the winget upgrade command:

winget upgrade

This’ll check fer updates fer all yer installed packages and install ‘em, keepin’ yer ship shipshape!

# Install several programs at once, aye!
winget install Mozilla.Firefox
winget install Microsoft.VisualStudioCode
winget install Git.Git
winget install Python.Python.3.12

Aye, this be much swifter than downloadin’ an’ installin’ everythin’ by hand. Ye can save the list in a script an’ run it each time ye be settin’ up a new vessel.

Easy Task 1 - Craft an Updatin’ Script

Write a Bash script that:

  1. Updates the package list
  2. Installs all available updates
  3. Restarts Nginx (or another service ye be runnin’)
  4. Prints a message when ‘tis done

Run it on one o’ yer VMs and see that it works, aye.

Easy Task 2 - Automate Machine Setup

Create a PowerShell script (.ps1) or a simple list o’ winget install commands for all the programs ye need on a new PC. Consider this, ye scallywags:

  • Which programs do ye always install?
  • Can ye also configure some settings with the script?

Next time ye be settin’ up a machine, just run the script instead o’ spendin’ an hour on manual installation.

Summary

  • If ye be doin’ somethin’ more than twice, consider automatizin’ it
  • Bash (Linux) and PowerShell (Windows) be the most common tools for scriptin’
  • Bash scripts be text files with commands that be run in order
  • PowerShell works with objects and be powerful for Windows administration
  • Variables, arguments, if checks, and loops be the most important buildin’ blocks