Bashで文字列配列を扱う

配列をループして処理したい時に使える技。複数行ある場合も想定

一行文字列の配列

スペースで区切った文字列を()で括っておくとfor inとして使える

取り出した要素は${foo}で拾えます。配列全体は${bar[@]}で取れる。以下のようにダブルクォートで囲っていると機能しなくなるので注意
("AAA BBB CCC")

#!/bin/bash

textList=(YWFhYWEK LWFhYQogICAtIOOBguOBguOBguOBgg== LWFhYQogICAtIOOBguOBguOBguOBgg==)

for text in "${textList[@]}"
do
  echo ${text}
done

出力結果

YWFhYWEK
LWFhYQogICAtIOOBguOBguOBguOBgg==
LWFhYQogICAtIOOBguOBguOBguOBgg==

複数行文字列の配列

Base64辺りでエンコードしておくと扱いが楽

#!/bin/bash

textList=(YWFhYWEK LWFhYQogICAtIOOBguOBguOBguOBgg== LWFhYQogICAtIOOBguOBguOBguOBgg==)

for text in "${textList[@]}"
do
  pure=$(echo ${text} | base64 -d)
  echo "$pure"
done

出力結果

aaaaa
-aaa
   - ああああ
-aaa
   - ああああ

lsの結果を配列にする

#!/bin/bash

# .と..は除外
# 最後にxargsを入れておくと改行が飛ぶ
results=($(ls -1a | grep -vP "^\.+$" | xargs))
for item in "${results[@]}"
do
    echo "$item";
done

スペース区切りの文字を配列にする

IFS変数に区切り文字を設定することで実現する。IFSとはInternal Field Separatorのこと

#!/bin/bash
IFS=$'\n'

text=$(cat <<'EOF'
aa bb
a b c
EOF
)
textList=($(echo "$text"))

for text in "${textList[@]}"
do
  echo "${text}"
done

出力結果

aa bb
a b c

GitHub ActionsでJavaScriptカスタムアクションを作る時にハマった部分

About custom actionsに書いてあることほぼそのままですが、読みづらいのでメモがてら
どうもカスタムアクションからjsを呼び出して使うの、バンドルする必要があるようでかなりだるそうなので実用性は微妙かも…

フォルダ構成

この説明ではtest.yamlをworkflowとし、index.jsを蹴るためのサンプルで説明します

└─.github/
   └─workflows/
       ├─.actions/
       │   ├─action.yaml
       │   └─index.js
       └─test.yaml

サンプルコード

test.yaml

workflow本体です

name: test
on:
  workflow_dispatch:
jobs:
  example:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: ./.github/workflows/actions
        with:
          param1: 'xxx'

action.yaml

custom actionです。
ファイル名はaction.ymlないしaction.yamlである必要があります。
これ以外のファイル名の場合、実行時に次のエラーが出ます。

Can't find 'action.yml', 'action.yaml' or 'Dockerfile' under '/home/runner/work/ci-test/ci-test/.github/workflows/actions'. Did you forget to run actions/checkout before running your local action?
name: test actions
description: test
runs:
  using: 'node16'
  main: 'index.js'

index.js

console.log(123);