Skip to content

Instantly share code, notes, and snippets.

View greymd's full-sized avatar
😉
Nanchatte

Yasuhiro Yamada greymd

😉
Nanchatte
View GitHub Profile
@greymd
greymd / function.sh
Last active September 11, 2019 14:31
Run given command as is with Custom AWS Lambda Runtime
# Input is like {"command":"bHMgLWFsIHwgZ3JlcCBob2dlaG9nZQo="}
function handler () {
EVENT_DATA="$1"
local _cmd
local _tmpdir
_cmd="$(tr -d '[:space:]' <<<"$EVENT_DATA" | sed -E "s/\{[\"']command[\"']:[\"']([0-9A-Za-z+/=]*)[\"']\}/\1/" | base64 --decode)"
_tmpdir="$(mktemp)"
eval "$_cmd" 2> "$_tmpdir-stderr" > "$_tmpdir-stdout" || true
echo "{\"stdout\": \"$(cat "$_tmpdir-stdout" | base64 | tr -d '\n')\", \"stderr\": \"$(cat "$_tmpdir-stderr" | base64 | tr -d '\n')\"}" || true
rm -rf "$_tmpdir-stderr" "$_tmpdir-stdout" "$_tmpdir"
@greymd
greymd / yamaya_fib.md
Last active February 20, 2023 08:12
yamayaさんの難読化シェル芸(フィボナッチ数列) 解説

yamayaさんの難読化シェル芸(フィボナッチ数列) 解説

経緯

(1) ある日、yamaya さんという怖い方がこのツイートを投稿する。

yamaya_src

@greymd
greymd / equalized_partition_nums.sh
Last active August 12, 2018 01:42
Divide integer num much equally with bash
#!/bin/bash
ceiling () {
local divide="$1";shift
local by="$1"
printf "%s\\n" $(( ( divide + by - 1 ) / by ))
}
equalized_partition_nums () {
local number="$1";shift
local count="$1"
@greymd
greymd / fifo.sh
Last active July 31, 2018 23:23
Docker + fifo + tmux bug ?
#!/bin/bash
# Try this script on the docker container.
# Tested on macoOS High Sierra + Ubuntu 18.04 docker container.
rm -f "$HOME"/work/*
tmux -S "$HOME"/work/sess new-session -d
printf "%s\\n" A B C D | while read -r f;do
--------------------
bash 3.2.0
--------------------
~$ bash --version
GNU bash, version 3.2.0(1)-release (x86_64-unknown-linux-gnu)
Copyright (C) 2005 Free Software Foundation, Inc.
~$ [[ "hoge" =~ ho.. ]] && echo "${BASH_REMATCH[0]}"
hoge
@greymd
greymd / gist:6edbff59405fcf5d4802cdb910587db0
Last active July 7, 2018 12:32
めも:【問題のみ】jus共催 第36回七夕・・・7は素数じゃないですか(しかも2つ)シェル芸勉強会
Q1:
<welcome.txt tr '\0' '*' | fold -w 70
初っ端からひどい
Q2:
ls | perl -C -Mutf8 -nle 'printf "mv $_ ";$_ =~ tr/0-9/0-9/ ;$_ =~ tr/A-Z/A-Z/;$_ =~ m/([0-9]).*([A-Z])/; print " $1年$2組.doc"'
Q3:
echo 2018{01..12}{01..31}$'\n' | date -f- +'%Y%m%d' 2>/dev/null | grep -P '([2357].*){4}'
@greymd
greymd / script.sh
Last active May 16, 2018 10:17
Convert app.log to csv
sed '/response:/{:loop;N;/<\/root>/!b loop;s/\n//g}' app.log | \
# 特定の時間帯のログだけをgrep
grep '^2017-05-09 20:5[0-9]' | \
# あとはgrepなりsedなりawkなりで自由に抽出
grep -oP '<record> *\K.*?(?= *</record>)' | sed 's/<\/[^>]*> *<[^>]*>/,/g;s/<[^>]*>//g'
# 同じディレクトリにapp.logを置いて実行
# $ bash script.sh
# 出力結果:
# A001,1990-01-04,0
@greymd
greymd / app.log
Last active May 25, 2018 15:42
app.log
2017-05-09 20:53:26 INFO --- [thread-1] myapp.logic : Register records
2017-05-09 20:53:26 INFO --- [thread-1] myapp.logic : --> POST https://api.example/v2/records http/1.1
2017-05-09 20:53:27 INFO --- [thread-1] myapp.logic : response: <?xml version="1.0" encoding="UTF-8"?>
<root>
<records>
<record>
<id>A001</id>
<date>1990-01-04</date>
<status>0</status>
</record>
@greymd
greymd / gist:079c4964f04ab03003f35c7e5a9c8dc1
Last active April 12, 2018 00:55
sum digits in the line
# https://unix.stackexchange.com/questions/205936/how-can-i-quickly-sum-all-numbers-in-a-file/205939
perl -pe 's/\d*/$s+=$&/ge}{$_=$s' file
perl -F= -pe '$s+=$F[1]}{print $s' file
awk -F= '{sum+=$2};END{print sum}'
@greymd
greymd / hibike.dc
Last active April 12, 2018 00:14
響け!ユーフォニアムをdcのみで
[響] 0:a [け] 1:a [!] 2:a [ユ] 3:a [ー] 4:a [フ] 5:a [ォ] 6:a [ニ] 7:a [ア] 8:a [ム] 9:a
0sc
[
lc
lC+
10%;an
lC
1+
d
sC