From 66c4b4d23316539a16a467325c5a7e9ac7ef1f58 Mon Sep 17 00:00:00 2001 From: Alexander Popov Date: Mon, 2 Dec 2024 11:44:50 +0300 Subject: [PATCH] added pure sh arrays example --- content/posts/2024/bash/password-generator.md | 2 +- content/posts/2024/shell/pure-sh-array.md | 25 +++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 content/posts/2024/shell/pure-sh-array.md diff --git a/content/posts/2024/bash/password-generator.md b/content/posts/2024/bash/password-generator.md index ff65e0b..d0e0b2d 100644 --- a/content/posts/2024/bash/password-generator.md +++ b/content/posts/2024/bash/password-generator.md @@ -2,7 +2,7 @@ title: "🔑 Генератор паролей в виде однострочника на Bash" date: 2024-08-17T22:33:12+03:00 draft: false -tags: [linux, tips, security] +tags: [shell, linux, tips, security] --- Пост [Cyrus](https://stackoverflow.com/a/44377013) на **SoF**. diff --git a/content/posts/2024/shell/pure-sh-array.md b/content/posts/2024/shell/pure-sh-array.md new file mode 100644 index 0000000..cb72f2a --- /dev/null +++ b/content/posts/2024/shell/pure-sh-array.md @@ -0,0 +1,25 @@ +--- +title: "✴️ Массивы на числом Shell" +date: 2024-12-02T11:42:09+03:00 +draft: false +tags: [shell, linux, tips, development] +--- + +```sh +#!/bin/sh + +IFS='|' +STROKE='Firebrick|SeaGreen|Sienna|Blue|Purple|Tomato' +set -f + +getNth() { shift "$(( $1 + 1 ))"; printf '%s\n' "$1"; } +getLast() { getNth "$(( $(length "$@") - 1 ))" "$@"; } +length() { echo "$#"; } + +INDEX=0 +while [ $INDEX -le $(expr $(length $STROKE) - 1) ]; do + echo $(getNth $INDEX $STROKE) + + INDEX=$(expr $INDEX + 1) +done +```