シェル芸160ノック28
9月末に発売されたシェル芸本の続きです。
前回はこちら
問題151
$ curl http://file.ueda.tech/eki/l/11302.xml > a $ cat a | grep -Pe "lon|lat|name" | sd '<[^>]+>' '' | awk '{print $1}' | xargs -n3 | while read D E F; do echo $D $(curl "https://cyberjapandata2.gsi.go.jp/general/dem/scripts/getelevation.php?lon=$E&lat=$F" | jq -r '.elevation'); sleep 1s; done
問題152
$ while true; do command wget localhost:8000/index.html |& grep 200 &> /dev/null && echo Success || echo Warning; sleep 1s; done Success Success Success ...
問題153
$ (echo -e "HEAD / HTTP/1.1\nHost: www.google.co.jp\n\n";sleep 1s) | telnet www.google.co.jp 80
問題154
$ echo wget curl ping dig nslookup | fmt -1 | while read CMD; do sudo strace -f $CMD google.com |& grep /etc/hosts && echo $CMD; done
問題155
$ echo -n test > index.html&&python3 -m http.server $ curl localhost:8000 -v * Trying 127.0.0.1:8000... * TCP_NODELAY set * Connected to localhost (127.0.0.1) port 8000 (#0) > GET / HTTP/1.1 > Host: localhost:8000 > User-Agent: curl/7.68.0 > Accept: */* > * Mark bundle as not supporting multiuse * HTTP 1.0, assume close after body < HTTP/1.0 200 OK < Server: SimpleHTTP/0.6 Python/3.9.0 < Date: Tue, 21 Dec 2021 12:52:52 GMT < Content-type: text/html < Content-Length: 4 < Last-Modified: Tue, 21 Dec 2021 12:52:51 GMT < * Closing connection 0 test $ (echo -e "HTTP/1.1 200 OK\nContent-Length: 5";echo -en "\ntest") | nc -N -l 8080
シェル芸160ノック27
9月末に発売されたシェル芸本の続きです。
前回はこちら
問題146
$ git branch --no-merged=main -r | xargs -n1 -I@ zsh -c "git log -1 --since $(date -d '30 days ago' +%F) @ | grep . &> /dev/null || {echo branch @; git log @ -1 | head -n3}" | sel --remove-empty 2: | paste - - - - -d ' '
問題147
$ cat o | grep -P "[\p{Hiragana}\p{Katakana}\p{Han}]+" | sd '<[^>]+>' '' | sed '/^$/d' | mecab | sel -gd\\s+ 1 | grep -v EOS | sort | uniq -c | sort -rn | head 89 の 88 シェル 85 芸 74 が 72 。 69 月 66 日 51 会 43 勉強 42 で
問題148
$ grep line_cd o -A1 | sd '<[^>]+>' '' | awk '{print $1}' | grep -v -- -- | paste - - | grep 京急本線 27001 京急本線 $ http http://file.ueda.tech/eki/l/27001.xml > o $ cat o | grep station_name | sd '<[^>]+>' '' | awk '{print $1}' 泉岳寺 品川 北品川 新馬場 青物横丁 鮫洲 立会川 大森海岸 平和島 大森町 梅屋敷 京急蒲田 雑色 六郷土手 京急川崎 ...
問題149
$ curl wttr.in/Shizuoka Weather report: Shizuoka \ / Clear .-. +7(5) °C ― ( ) ― ↘ 10 km/h `-’ 10 km / \ 0.0 mm ...
問題150
$ http https://gihyo.jp/magazine/SD/backnumber > o $ cat o | grep 品切 -B4 | nkf -Lu | sd '<[^>]+>' '' | sed '/^$/d' | grep -v -- -- | paste - - - | sort -u Software Design 2020年10月号 2020年9月18日発売 定価1,342円(本体1,220円+税10%)[品切] Software Design 2020年11月号 2020年10月17日発売 定価1,342円(本体1,220円+税10%)[品切] Software Design 2020年12月号 2020年11月18日発売 定価1,342円(本体1,220円+税10%)[品切] Software Design 2020年6月号 2020年5月18日発売 定価1,342円(本体1,220円+税10%)[品切] Software Design 2020年7月号 2020年6月18日発売 定価1,342円(本体1,220円+税10%)[品切] Software Design 2020年8月号 2020年7月18日発売 定価1,342円(本体1,220円+税10%)[品切] Software Design 2020年9月号 2020年8月18日発売 定価1,342円(本体1,220円+税10%)[品切] Software Design 2021年10月号 2021年9月18日発売 定価1,342円(本体1,220円+税10%)[品切] Software Design 2021年11月号 2021年10月18日発売 定価1,342円(本体1,220円+税10%)[品切] Software Design 2021年12月号 2021年11月18日発売 定価1,342円(本体1,220円+税10%)[品切] Software Design 2021年1月号 2020年12月18日発売 定価1,342円(本体1,220円+税10%)[品切] Software Design 2021年2月号 2021年1月18日発売 定価1,342円(本体1,220円+税10%)[品切] Software Design 2021年3月号 2021年2月18日発売 定価1,342円(本体1,220円+税10%)[品切] Software Design 2021年4月号 2021年3月18日発売 定価1,342円(本体1,220円+税10%)[品切] Software Design 2021年5月号 2021年4月16日発売 定価1,342円(本体1,220円+税10%)[品切] Software Design 2021年6月号 2021年5月18日発売 定価1,342円(本体1,220円+税10%)[品切] Software Design 2021年7月号 2021年6月17日発売 定価1,342円(本体1,220円+税10%)[品切] Software Design 2021年8月号 2021年7月16日発売 定価1,342円(本体1,220円+税10%)[品切] Software Design 2021年9月号 2021年8月18日発売 定価1,342円(本体1,220円+税10%)[品切] $ dateutils.dseq --format "%Y/%m https://gihyo.jp/magazine/SD/archive/%Y/%Y%m" 2020-01-01 1mo 2020-12-01 | while read YM URL; do curl "$URL" | nkf -Lu | grep ございません &> /dev/null && echo $YM;done
シェル芸160ノック26
9月末に発売されたシェル芸本の続きです。
前回はこちら
問題141
$ yes | awk '{print "echo",NR",""$(shuf -n5 -re {a..z}),$(shuf -n10 -re {a..z}),$(shuf -n5 -re {a..z})@mail.com"}' | head -n1000 |bash | tr -d ' ' | head 1,hyoah,pgdawiluzg,xwrui@mail.com 2,xbabm,gnmbcoxsri,ixpzm@mail.com 3,jyojm,rdjdcqqcus,hwrpa@mail.com 4,ehzdr,ihdnpkjvzi,gknmw@mail.com 5,jdxht,hnyxucnbcw,rvvvi@mail.com 6,bypgy,brjilztgyc,pnqwy@mail.com 7,foxwp,aymknffafy,mvclb@mail.com 8,dlwte,umqxtwcdal,uwoth@mail.com 9,wbhkw,cxrtjswfmt,bxsrq@mail.com 10,alkvf,bpdwkemdsh,phqcv@mail.com
問題142
$ echo blog.ueda | command xxd -b -c1 | sel 2 | tr -d \\n | ocs '{for(var i=0;i<F0.Length;i++)println(F0)}' | awk '{$NR=!$NR;print}' FS='' OFS='' | perl -lne 'print pack("B*", $_)' | grep -P '^[a-z\.]+$' rlog.ueda jlog.ueda flog.ueda clog.ueda bdog.ueda bhog.ueda bnog.ueda bmog.ueda blgg.ueda ...
問題143
$ git status --short | awk '$1=="M"&&$2~/B/{print $2}' | xargs git add
問題144
$ fd . ./img ./css --type=f --exec git checkout "HEAD^" {} Updated 1 path from c335175 Updated 1 path from c335175 Updated 1 path from c335175 Updated 1 path from c335175
問題145
$ git log --date=unix | grep Date: | awk '{if(b)print $2,b;b=$2}' | awk '{print ($2-$1)/3600}' 5.87694 0.0141667 232.218 47.4703 31.0544 0.0580556 16.2336 1181.22 1345.62 1581.95 ...
シェル芸160ノック25
9月末に発売されたシェル芸本の続きです。組み合わせの列挙系は楽しいですね。
前回はこちら
問題132
$ cat ./qdata/132/httpd-access.log | awk '$(NF-1)==500' | while read L; do mail -s "500があったよ" こ こにメールアドレス <<< $L; done
問題133
$ echo "@reboot sleep 180s && shutdown -h now" | sudo crontab
問題134
$ grep -vnPe "^( {4})*[^ ]|^$" qdata/134/hoge.py 5: print(i+1) $ autopep8 qdata/134/hoge.py --select E11 --diff --- original/qdata/134/hoge.py +++ fixed/qdata/134/hoge.py @@ -2,5 +2,5 @@ for i in range(1,10): print(i) - print(i+1) + print(i+1) print(i+2)
autopep8
解はもうちょっと成形してやる方が題意通りだけど、diffの方がいいかなって
問題135
cat qdata/135/sample.lisp | grep defun | ocs "F0.Count(c=>c=='(')!=F0.Count(c=>c==')'){println(F[2])}" fib sum1
問題136
$ cat qdata/136/somecode.c | awk '/^#/{h=h$0"\n"}/void a/,/^\}/{a=a$0"\n"}/int b/,/^\}/{b=b$0"\n"}/main/,/^}/{m=m$0"\n"}END{print h;print a;print b;print m}' #include <stdio.h> #include <stdlib.h> #include <time.h> void a() { int i = 0, j = b(); for (; i < j; i++){ puts("a"); } } int b() { return rand()%10; } int main(int argc, char const *argv[]) { srand(time(NULL)); a(); return 0; }
問題137
$ clang-format ./qdata/137/fib.c #include <stdio.h> int fib(int n) { if (n <= 1) { return n; } return fib(n - 1) + fib(n - 2); } int main(void) { int i; for (i = 0; i < 10; i++) { printf("%d\n", fib(i)); } return 0; }
問題138
$ cat os.csv|tr , \\n| join -j9 - <(cat service.csv|tr , \\n) | join -j9 - <(cat browser.csv|tr , \\n)| awk '!($1!="Windows"&&$3=="IE")&&!($1!="macOS"&&$3=="Safari")' Windows ServiceA IE Windows ServiceA Chrome Windows ServiceA FireFox Windows ServiceB IE Windows ServiceB Chrome Windows ServiceB FireFox Windows ServiceC IE Windows ServiceC Chrome Windows ServiceC FireFox macOS ServiceA Chrome macOS ServiceA FireFox macOS ServiceA Safari macOS ServiceB Chrome macOS ServiceB FireFox macOS ServiceB Safari macOS ServiceC Chrome macOS ServiceC FireFox macOS ServiceC Safari Linux ServiceA Chrome Linux ServiceA FireFox Linux ServiceB Chrome Linux ServiceB FireFox Linux ServiceC Chrome Linux ServiceC FireFox
問題139
$ echo https://{,{,{,cc.}bb.}aa.}example.com/A{/B{/C,},} | fmt -1 https://example.com/A/B/C https://example.com/A/B https://example.com/A https://aa.example.com/A/B/C https://aa.example.com/A/B https://aa.example.com/A https://bb.aa.example.com/A/B/C https://bb.aa.example.com/A/B https://bb.aa.example.com/A https://cc.bb.aa.example.com/A/B/C https://cc.bb.aa.example.com/A/B https://cc.bb.aa.example.com/A
一般解じゃないのでダメかな?
問題140
$ mkdir tmp $ cp ./dir.tar.gz ./tmp/ $ cd ./tmp $ tar xvfz ./dir.tar.gz $ rm ./dir.tar.gz $ shuf -i 0-255 -rn 400 | paste - - - - -d. | paste - <(dateutils.dseq --format "- - [%Y/%m/%d %H:%M:%S +0900] \"GET" 2021-01-01T00:00:00 1s 2021-12-31T23:59:59 | shuf -n 100 | sort) | paste -d ' ' - <(fd . ./ --exec echo /{}|shuf -rn100) | sed 's@$@ HTTP/1.1" 200@' | paste -d ' ' - <(shuf -i 100-1000 -rn100) 225.20.241.13 - - [2021/01/09 05:13:30 +0900] "GET /js/bootstrap.js HTTP/1.1" 200 272 208.207.0.117 - - [2021/01/13 12:36:05 +0900] "GET /product.php HTTP/1.1" 200 593 14.11.149.123 - - [2021/01/19 04:56:24 +0900] "GET /cart.php HTTP/1.1" 200 153 242.122.183.51 - - [2021/01/21 16:38:16 +0900] "GET /search.php HTTP/1.1" 200 334 220.85.189.142 - - [2021/01/24 16:01:08 +0900] "GET /js/main.js HTTP/1.1" 200 494 122.200.73.176 - - [2021/01/25 18:47:54 +0900] "GET /index.php HTTP/1.1" 200 266 22.80.42.4 - - [2021/02/02 04:45:09 +0900] "GET /img HTTP/1.1" 200 757 129.176.186.244 - - [2021/02/04 06:21:02 +0900] "GET /js/bootstrap.js HTTP/1.1" 200 575 203.238.5.90 - - [2021/02/07 22:17:50 +0900] "GET /js/main.js HTTP/1.1" 200 484 233.1.189.253 - - [2021/02/08 19:48:11 +0900] "GET /img HTTP/1.1" 200 273 63.156.210.229 - - [2021/02/18 15:54:29 +0900] "GET /js/bootstrap.js HTTP/1.1" 200 530 149.201.91.75 - - [2021/03/06 01:26:53 +0900] "GET /js/main.js HTTP/1.1" 200 716 ...
dseq
が激重なので範囲減らすかファイルに書き出すといいと思いますね
シェル芸160ノック24
9月末に発売されたシェル芸本の続きです。
前回はこちら
問題127
$ while true; do echo -en "$(tput lines) $(tput cols)\r"; done
問題128
$ man pcrepattern | tail -n1 | sel 1 2 PCRE 8.38
問題129
$ base64 -d qdata/129/crypt | zcat | xxd | grep 50: | sel -- -1 | tr -d . | grep -o .... | sed "s/.*/echo -e '\\\\U&'/" | bash | tr -d \\n シェル
30..
みたいな文字列から日本語っぽいな…?とまではならなかった…まだまだですね
問題130
$ python3 -m http.server
問題131
wget example.com/big_file.tar.gz && { echo "完了だよ!" | mail -s "通知だよ" ここにメールアドレス } || { echo "失敗だよ!" | mail -s "通知だよ" ここにメールアドレス }
メールじゃなくてwslからwindowsのトーストを表示させようと思ったんだけどある程度のサイズのスクリプトを用意しないといけないのでやめた
シェル芸160ノック23
9月末に発売されたシェル芸本の続きです。むずかしい
前回はこちら
問題121
$ (strace seq 1e7 | head) |& grep -oPe '"[^"]+"' | grep / "/usr/bin/seq" "/etc/ld.so.preload" "/etc/ld.so.cache" "/lib/x86_64-linux-gnu/libc.so.6" "/usr/lib/locale/locale-archive" "/usr/share/locale/locale.alias" "/usr/lib/locale/C.UTF-8/LC_IDENTIFICATION" "/usr/lib/x86_64-linux-gnu/gconv/gconv-modules.cache" "/usr/lib/locale/C.UTF-8/LC_MEASUREMENT" "/usr/lib/locale/C.UTF-8/LC_TELEPHONE" "/usr/lib/locale/C.UTF-8/LC_ADDRESS" "/usr/lib/locale/C.UTF-8/LC_NAME" "/usr/lib/locale/C.UTF-8/LC_PAPER" "/usr/lib/locale/C.UTF-8/LC_MESSAGES" "/usr/lib/locale/C.UTF-8/LC_MESSAGES/SYS_LC_MESSAGES" "/usr/lib/locale/C.UTF-8/LC_MONETARY" "/usr/lib/locale/C.UTF-8/LC_COLLATE" "/usr/lib/locale/C.UTF-8/LC_TIME" "/usr/lib/locale/C.UTF-8/LC_NUMERIC" "/usr/lib/locale/C.UTF-8/LC_CTYPE"
問題122
$ strace xargs -n1 < qdata/122/words|&grep -cPe "^clone" 3000
問題123
$ awk -F '[( ]' '$NF ~ /\<[0-9]+\>/{print FILENAME=="dir1_strace"?1:2,$1,$NF}' dir{1,2}_strace | tr -d "<>" | awk '{a[$2]+=$1==1?-$3:$3}END{for(k in a) print k, a[k]*1000}' | sort -k2nr | head -n3 read 11879.7 openat 860.119 close 487.181
問題124
$ ip a | sel 2 | tr -d : | xargs lo bond0 dummy0 eth0 tunl0@NONE sit0@NONE
sel
はデフォルトの区切り文字がスペース1つなのがこういう時ちょっと便利
問題125
壊していいLinuxが無かった
問題126
$ bash --version GNU bash, version 5.0.17(1)-release (x86_64-pc-linux-gnu) Copyright (C) 2019 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> This is free software; you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law.
シェル芸160ノック22
9月末に発売されたシェル芸本の続きです。
前回はこちら
問題116
$ exa /dev/ -l | grep "^b" | awk '{print $NF,$2}' loop0 7,0 loop1 7,1 loop2 7,2 loop3 7,3 loop4 7,4 loop5 7,5 loop6 7,6 loop7 7,7 ram0 1,0 ram1 1,1 ram2 1,2 ram3 1,3 ram4 1,4 ram5 1,5 ram6 1,6 ram7 1,7 ram8 1,8 ram9 1,9 ram10 1,10 ram11 1,11 ram12 1,12 ram13 1,13 ram14 1,14 ram15 1,15 sda 8,0 sdb 8,16 $ exa -l /sys/dev/block | sel -- 7 -1 | teip -f2 -- zsh -c "xargs -n1 -I@ basename @" 1:0 ram0 1:1 ram1 1:2 ram2 1:3 ram3 1:4 ram4 1:5 ram5 1:6 ram6 1:7 ram7 1:8 ram8 1:9 ram9 1:10 ram10 1:11 ram11 1:12 ram12 1:13 ram13 1:14 ram14 1:15 ram15 7:0 loop0 7:1 loop1 7:2 loop2 7:3 loop3 7:4 loop4 7:5 loop5 7:6 loop6 7:7 loop7 8:0 sda 8:16 sdb
問題117
$ b=$(lsusb | wc -l); while :; do a=$(lsusb | wc -l); [[ "$a" -gt "$b" ]] && echo "挿されました" || [[ "$a" -lt "$b" ]] && echo "抜かれました"; b=$a sleep 1s; done
lsusbの出力をエスパーしてます
問題118
$ ./hidoi.bash 5728 $ awk '$4==5728{print $1}' /proc/[0-9]*/stat | xargs -n1 -I@ ls /proc/@/fd/1 -l | sel -- -1 /home/xztaityozx/ghq/github.com/shellgei/shellgei160/qdata/118/5728 /tmp/tmp.IvE4RzXOKL /home/xztaityozx/ghq/github.com/shellgei/shellgei160/qdata/118/1639576737.611781307
問題119
$ fd . /sys/firmware/memmap/ --max-depth=1 --type=d --exec paste {}/start {}/end {}/type | teip -f1,2 -- xargs -n1 printf "0x%016x\n" | awk '{$1=$1"-"$2;$2="";print}' | sort 0x0000000000000000-0x000000000009ffff System RAM 0x00000000000e0000-0x00000000000e0fff Reserved 0x0000000000100000-0x00000000001fffff ACPI Tables 0x0000000000200000-0x00000000f7ffffff System RAM 0x0000000100000000-0x0000000669ffffff System RAM
問題120
$ cat qdata/120/enigma | base64 -d > hoge.gz && gunzip hoge.gz && mkdir tmp &&sudo mount -o loop -t msdos hoge ./tmp && ls ./tmp && sudo umount ./tmp && rm -r tmp hoge