Scriptinge wif Bash e'nd Pow'rsh'll

Skip to content

This doth be a machine-wrought text which may contain errors!

If thou hast performed the selfsame task for the third time, ‘tis time to automate it. Scripting doth concern itself with allowing the machine to do the tedious labour for thee, with greater speed and without forgetting aught.

Wherefore Automate?

Certain tasks in IT operation are oft performed:

  • To update servers
  • To create users
  • To take backup
  • To restart services after an update
  • To check that services do run

To perform these manually each time doth consume time, and ‘tis easy to forget a step. A script doth the same each time, without complaint.

Bash: Scripting in Linux

Bash doth be the shell (the terminal) in most Linux distributions, and ‘tis capable also of running scripts. A Bash script is naught but a text file containing commands which are executed in order.

Thy first script

Create a file named oppdater.sh:

#!/bin/bash
# Updatethepackagelistandinstallupdates
echo "Updatingthesystem..."
sudoupdateapt
sudoaptupgrade-y
echo "Done!"

Make the file executable and run it:

chmod +x oppdater.sh
./oppdater.sh

#!/bin/bash

The foremost line is termed a shebang. It doth inform the operating system that the file shall be executed with Bash. Without it, the system knoweth not which program shall interpret the script.

Variables and Input

‘Tis meet we speak of variables and input, for these do form the very sinews of our calculations. A variable, good sirs, doth hold a value, which may be altered as the course of the program doth unfold. Think of it as a vessel, capable of containing divers quantities.

Input, on the other hand, is the manner whereby we impart knowledge unto the program from the world without. ‘Tis by input that we bid the program to act upon specific data, and thus achieve a desired outcome.

Consider, if you will, a simple example. We might declare a variable named “age,” and then request of the user their years, which shall then be stored within this selfsame variable. Thus armed, the program may proceed to tailor its responses according to the age provided.

#!/bin/bash
# The name of the user shall be provided as an argument

# If no username hath been given
if [ -z "$BRUKERNAVN" ]; then
    # Pray display usage instructions to thee
    echo "Usage: ./create_user.sh <username>"
    exit 1
fi

# Command the system to create thy new account
sudo adduser "$BRUKERNAVN"
# Grant this soul dominion over administrative tasks
sudo usermod -aG sudo "$BRUKERNAVN"
# Proclaim unto all that a new subject stands established, blessed by royal privileges.
echo "User $BRUKERNAVN created with sudo access."

Here doth the script receive a username as argument ($1 being the first argument). Shouldst thou run ./lag_bruker.sh ola, ‘twill create the user ola and grant unto them sudo access.

Useful Bash Concepts

Concept Example What it doth
Variable NAVN="Ola" Doth store a value
Argument $1, $2 Input from the command line
If-check if [ -f "fil.txt" ]; then Checketh if a file doth exist
For-loop for f in *.log; do Doth something for each matching file
Pipe cat logg.txt \| grep "ERROR" Sendeth output from one command to another

PowerShell: Scripting in Windows

PowerShell doth be Windows’ answer to Bash, yet ‘tis more object-orientated. It worketh with objects, and not merely text, which doth render it puissant for the governance of Windows systems.

Example: Check Disk Space

# Prickly check upon thy free space on the disc
Get-PSDrive -PSProvider FileSystem | Select-Object Name, 
    @{Name="Burdened (GiB)"; Expression={[math]::Round($_.Used / 1GiB, 2)}},
    @{Name="Unburdened (GiB)"; Expression={[math]::Round($_.Free / 1GiB, 2)}}

Example: Installing Software via Winget

# Installere flertal av programvarer i éin gang
winget install Mozilla.Firefox
winget install Microsoft.VisualStudioCode
winget install Git.Git
winget install Python.Python.3.12

This doth prove much swifter than to download and install all by hand. Thou mayst preserve the list within a script and run it each time thou dost set up a new machine.

Easy Task the First - A Script for Refreshing

Compose, pray thee, a Bash script which shall:

  1. Renew the catalogue of packages
  2. Install all updates that be available
  3. Restart Nginx (or some other service thou hast running)
  4. Print forth a message when ‘tis finished

Run it upon one of thy Virtual Machines and observe if it doth function as intended.

Easy Task the Second - Automate Machine Setup

Compose a PowerShell script (.ps1) or a simple list of winget install commands for all the programs thou dost require upon a new PC. Consider well:

  • Which programs dost thou ever install?
  • Might thou also configure some settings with the script?

The next time thou dost set up a machine, merely run the script instead of spending an hour upon manual installation.

Summary

  • If thou dost perform a thing more than twice, consider to automate it.
  • Bash (Linux) and PowerShell (Windows) be the most common tools for scripting.
  • Bash scripts are text files with commands which run in sequence.
  • PowerShell doth work with objects and is puissant for Windows administration.
  • Variables, arguments, if checks, and loops be the chief building blocks.