Bashで文字列にスネークケース変換を行うには?

HelloWorld → hello_world WMClass → wm_class このように、キャメルケース(PascalCase含む)をスネークケースに変換 to_snake_case() { local input="$1" echo "$input" \ | sed -E 's/([A-Z]+)([A-Z][a-z])/\1_\2/g' \ | sed -E 's/([a-z0-9])([A-Z])/\1_\2/g' \ | tr 'A-Z' 'a-z' } # テスト for s in HelloWorld WMClass URLLoader MyXMLParser FooBAR BazQux; do echo "$s -> $(to_snake_case "$s")" done # 出力例 HelloWorld -> hello world POSIXBased -> posix based XMLHTTPRequest -> xml http request URLLoader -> url loader FooBAR -> foo bar BazQux -> baz qux

2026年1月21日

Bashで/tmpに一時ファイルを保存する

# 一時ファイルを作成 tmpfile=$(mktemp) # /tmp を明示したい場合 tmpfile=$(mktemp /tmp/myapp.XXXXXX) XXXXXX は必須(衝突防止用) 例: /tmp/myapp.a8F3kQ

2026年1月21日

bashでシステムのデフォルトのフォントのパスを取得するには?

A. デフォルトフォント fc-match -f '%{file}\n' B. sans-serif fc-match sans-serif -f '%{file}\n' C. serif fc-match serif -f '%{file}\n' D. monospace fc-match monospace -f '%{file}\n' E. GTK環境(GNOMEなど)のUIフォントを取得したい場合 gsettings get org.gnome.desktop.interface font-name 出力例: 'Noto Sans 11' ただし、※これはフォント名+サイズで、パスではない

2026年1月21日

bash+awkでiniファイルを読み込むには?

今回読み取るiniファイル。 コメントなし、キーと値の間にはスペースなし。 [setting] encoding=libx264 default_width=1920 default_height=1080 default_fps=60 A. すべての値を読み出す #!/bin/bash ini_file="config.ini" section="setting" eval "$( awk -F= -v section="$section" ' $0 == "["section"]" {in_section=1; next} /^\[/ {in_section=0} in_section && NF==2 { gsub(/\r/, "", $2) print $1 "=\"" $2 "\"" } ' "$ini_file" )" # 使用例 echo "$encoding" echo "$default_width" echo "$default_height" echo "$default_fps" B. 特定のキーの値を読み出す #!/bin/bash readSetting() { local key="$1" local section="setting" local ini_file="config.ini" awk -F= -v section="$section" -v key="$key" ' $0 == "["section"]" {in_section=1; next} /^\[/ {in_section=0} in_section && $1 == key { print $2 exit } ' "$ini_file" } # 使用例 ENCODING=$(readSetting encoding) WIDTH=$(readSetting default_width) HEIGHT=$(readSetting default_height) FPS=$(readSetting default_fps)

2026年1月21日

BashでYes Or No

read -p "続行しますか? (y/n): " ans if [[ "$ans" != "y" && "$ans" != "Y" ]]; then echo "中止しました" exit 1 fi echo "処理を続行します" Yes以外の場合は即刻スクリプトを終了

2026年1月6日