irelephant [he/him]🍭

  • 23 Posts
  • 325 Comments
Joined 1 year ago
cake
Cake day: December 14th, 2023

help-circle

















  • Its a completely different shell, not just another terminal emulator.

    Its more readable, and its syntax is less arcane than bash.

    For example, a script to get the first line of a file and its lenght in bash is:

    #!/bin/bash
    
    
    if [ "$#" -ne 1 ]; then
        echo "Usage: $0 filename"
        exit 1
    fi
    
    filename="$1"
    
    
    if [ ! -r "$filename" ]; then
        echo "File '$filename' does not exist or is not readable."
        exit 1
    fi
    
    
    read -r first_line < "$filename"
    
    
    echo "First line: $first_line"
    
    
    length=${#first_line}
    
    echo "Length of first line: $length"
    

    There is so much I hate about this, like using a semicolon after the if condition, and ending it in fi.

    Versus the powershell version:

    param (
        [Parameter(Mandatory = $true)]
        [string]$FilePath
    )
    
    
    if (-not (Test-Path -Path $FilePath)) {
        Write-Error "File '$FilePath' does not exist."
        exit 1
    }
    
    try {
        
        $firstLine = Get-Content -Path $FilePath -TotalCount 1
    }
    catch {
        Write-Error "Could not read from file '$FilePath'."
        exit 1
    }
    
    
    Write-Output "First line: $firstLine"
    
    
    $lineLength = $firstLine.Length
    Write-Output "Length of first line: $lineLength"
    

    It feels more modern.