From f3d6366c8306a358dc9d399e78bb3d78d4e052c8 Mon Sep 17 00:00:00 2001 From: satr14 Date: Thu, 28 May 2026 08:13:18 +0700 Subject: [PATCH] test code with bash --- posts/test.md | 95 +++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 85 insertions(+), 10 deletions(-) diff --git a/posts/test.md b/posts/test.md index 078594d..2374e95 100644 --- a/posts/test.md +++ b/posts/test.md @@ -57,18 +57,93 @@ draft: true ## 4. Code Blocks with Syntax Highlighting -```nix -{ config, pkgs, ... }: +```bash +#!/usr/bin/env bash +set -euo pipefail -{ - environment.systemPackages = with pkgs; [ - git - neovim - tmux - ]; - - nix.settings.experimental-features = [ "nix-command" "flakes" ]; +# Variables & string manipulation +NAME="world" +GREETING="Hello, ${NAME}!" +UPPER=${GREETING^^} +SLICE=${GREETING:0:5} +echo "$UPPER | $SLICE" + +# Arrays +FRUITS=(apple banana cherry) +FRUITS+=(mango) +echo "${FRUITS[@]}" # all elements +echo "${#FRUITS[@]}" # length +echo "${FRUITS[@]:1:2}" # slice + +# Associative array +declare -A CONFIG=([host]="localhost" [port]="8080") +echo "${CONFIG[host]}:${CONFIG[port]}" + +# Arithmetic +COUNT=0 +(( COUNT += 5 )) +RESULT=$(( COUNT ** 2 )) +echo "$RESULT" + +# Conditionals & test operators +FILE="/etc/hosts" +if [[ -f "$FILE" && -r "$FILE" ]]; then + echo "readable" +elif [[ -e "$FILE" ]]; then + echo "exists but not readable" +else + echo "not found" +fi + +# Case +case "$NAME" in + world|earth) echo "planet" ;; + *) echo "unknown" ;; +esac + +# Loops +for fruit in "${FRUITS[@]}"; do + echo "fruit: $fruit" +done + +for (( i=0; i<3; i++ )); do + echo "i=$i" +done + +WHILE=3 +while (( WHILE-- > 0 )); do + echo "while: $WHILE" +done + +# Functions with local vars & return codes +greet() { + local name=${1:-"stranger"} + local -r greeting="Hi, $name!" + echo "$greeting" + return 0 } +greet "$NAME" + +# Command substitution & process substitution +DATE=$(date +%Y-%m-%d) +DIFF=<(echo "foo") +echo "Today: $DATE" + +# Here-doc +cat < /dev/null 2>&1 + +# Parameter expansion: default, error, replace +echo "${UNSET:-default}" +echo "${GREETING/Hello/Hey}" +echo "${FILE##*/}" # basename +echo "${FILE%/*}" # dirname ``` ---