Code snippetの種類と動作1 / 1
言語の特徴
Pythonはimport、defとindent、dictionary/list comprehension等を使う。PowerShellはVerb-Nounのcmdlet、$変数、object pipelineとWhere-Object等、Bashはshell変数・command/pipe、for/do/doneやtest等を使う。同じ記号があっても言語と実行環境で意味が変わる。
rows = [{"host": "A", "severity": 8}, {"host": "B", "severity": 3}]
print([row["host"] for row in rows if row["severity"] >= 7])
このPythonはAだけを出力する。値を表示する処理と外部commandを実行するeval/exec等を区別し、未知の文字列を実行codeへ渡さない。
$rows = @([pscustomobject]@{Host='A'; Severity=8}, [pscustomobject]@{Host='B'; Severity=3})
$rows | Where-Object { $_.Severity -ge 7 } | Select-Object -ExpandProperty Host
これはPowerShellのobject pipelineでAを出力する。Bash等の標準出力text pipeと同じfieldの扱いとは限らない。Bashの例ではprintf '%s\n' 'alert A' 'info B' | grep '^alert'がalert Aを出力する。Codeの拡張子や見た目だけで悪性を決めず、入力・変換・外部操作と結果を読む。