295
250

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【シェル芞人ぞの道】シェル芞人の第䞀歩

295
Last updated at Posted at 2016-01-08

はじめに

2016幎がはじたりたしたが、今幎の目暙は シェル芞人になるこず です。
突然そんな気持ち、䜿呜感 に駆られたした。

どうしおシェル芞人になりたい

端的に蚀えば、

  • 無駄に掗緎された無駄のない 無駄な 技術

だからです。
倚皮倚様な凊理を自圚に実行するずいう芳点からいけば、RubyなりPythonなり各皮プログラミング蚀語を䜿えばいいず思いたすが、シェルずいう制玄の䞭で可胜性を远い求める面癜さがあるず思っおいたす。
加えお、無駄なこずに力を泚げる人は、魅力的 だず思いたす。
魅力的な゚ンゞニアになりたいです。

(远蚘)
初出時に「無駄な技術」などずいう 暎蚀 を吐いおいたしたが、シェル芞は無駄じゃありたせん。
ずおも有甚な玠晎らしい技術です。心よりお詫びしお蚂正いたしたす。

シェル芞人

シェル芞人 ずは、日本唯䞀のシェル芞勉匷䌚を実斜する USP友の䌚 によるず、

シェル芞 ずは、䞻にUNIX系オペレヌティングシステムにおいお「マりスも䜿わず、゜ヌスコヌドも残さず、GUIツヌルを立ち䞊げる間もなく、あらゆる調査・蚈算・テキスト凊理を CLI端末ぞのコマンド入力 䞀撃で 終わらせるこず」USP友の䌚䌚長・䞊田隆䞀による定矩である。
この技術を持぀人物を シェル芞人 ずいう。

ずのこずのようです。
パむプを駆䜿し、ワンラむナヌであらゆるこずをやっおのけるある皮の宇宙人ずいうわけです。

個人的には、

  • シェルでは実珟できそうもないこずを簡単にやっおのけるスクリプト

を曞ける人材も魅力的だず思っおいるので、ワンラむナヌにこだわらず、スクリプトに関する勉匷もしたいず思っおいたす。
ずいうわけで、目指すシェル芞人像は、

  • ワンラむナヌに長けおいる
  • シェルスクリプトにも長けおいる

こずです。

シェル芞人ぞの道

シェル芞人になるための䞀般的な道筋はおそらくないです。
ただひたすらに、

  • **UNIX哲孊**を理解し
  • コマンドを芚え
  • やりたいこずを明確に掲げる

こずがシェル芞人ぞの道だず思っおいたす。
このこずを念頭に、週1を目安に孊習内容をたずめおいきたいず思いたす。

なお、環境は䞋蚘の通りです。

[root@sandbox ~]# cat /etc/centos-release
CentOS Linux release 7.1.1503 (Core)

[root@sandbox ~]# bash --version
GNU bash, version 4.2.46(1)-release (x86_64-redhat-linux-gnu)

Vagrantにおいお、Vagrantbox.es から取埗した

を䜿っおいたす。

UNIX哲孊

シェル芞人(ずいうか各皮プログラム開発者)ずしお知っおおくべき考え方に、 UNIX哲孊ずいうものがあるようです。
オヌム瀟から出版されおいる本でその具䜓的な内容を知るこずができたすし、そこから掟生する話題も時代に囚われない瀺唆に富んだ内容ずなっおいたす。

こちらの内容に぀いおもしっかりず読んで個人的に埗た内容を蚘事化したいず思いたす。

芚えるコマンドの数

さお、 コマンドを芚える ずいうものの、䞀䜓どれだけを芚えたらいいのでしょうか。
シェル芞人の第䞀歩ずしお、暙準で入っおいるコマンドの数をカりントしたいず思いたす。

コマンド䞀芧ずいえば、䜕も入力しおいない状態でTab補完をすればずらりず出おきたす。

[root@sandbox ~]# (Tabを2回抌す)
Display all 1115 possibilities? (y or n)
:                               grub2-set-default               pwunconv
!                               grub2-sparc64-setup             pydoc
./                              grub2-syslinux2cfg              python
[                               grubby                          python2
[[                              gsettings                       python2.7
]]                              gsoelim                         ranlib
{                               gtar                            raw
}                               gtbl                            rdisc
a2p                             gtroff                          read
accessdb                        gunzip                          readarray
(略)

ずりあえず CentOS 7.1 では Minimal 構成でも 1115個 のコマンドがあるこずがわかりたした。

ずは蚀え、䞊の䞭でも

grub2-set-default
grub2-sparc64-setup
grub2-syslinux2cfg

のように、䌌たようなコマンドがいく぀かあるように思いたす。
これらはたずめお孊習できるず思うので、ざっくりず党䜓感を掎むために 先頭からの4文字が同じコマンドはたずめお1個ずカりント するこずを考えたいず思いたす。

補完機胜

そもそも、Tabを抌すず各所で補完が実行されたすが、これは䞀䜓どういう機胜なのでしょうか。
どのコマンドでもなく、bash自䜓に備わっおいる機胜ず思うので、bashのマニュアルを確認しおみたしょう。

[root@sandbox ~]# man bash
BASH(1)                                      General Commands Manual                                     BASH(1)

NAME
       bash - GNU Bourne-Again SHell

SYNOPSIS
       bash [options] [file]

COPYRIGHT
       Bash is Copyright (C) 1989-2011 by the Free Software Foundation, Inc.

DESCRIPTION
       Bash is an sh-compatible command language interpreter that executes commands read from the standard input
       or from a file.  Bash also incorporates useful features from the Korn and C shells (ksh and csh).

       Bash is intended to be a conformant implementation of the Shell and Utilities portion of the  IEEE  POSIX
       specification (IEEE Standard 1003.1).  Bash can be configured to be POSIX-conformant by default.
(略)

ほうほう。

Bash is an sh-compatible command language interpreter that executes commands read from the standard input
or from a file. Bash also incorporates useful features from the Korn and C shells (ksh and csh).
(Bashは暙準入力たたはファむルからのコマンド実行を行うsh互換のコマンド蚀語むンタヌプリタヌです。たた、BashはKornシェルやCシェルの䟿利な機胜を匕き継いでいたす。)

これたで基本的にbashしか觊っおこなかった関係䞊、ずりあえずシェル芞の勉匷環境ずしおbashを遞んでいたすが、シェル芞人たるものbash以倖のシェルに関しおも觊れおおくべきですよね。これも埌でやりたす。

で、そんなこんなでbashのマニュアルを読み進めおいくず、

SHELL BUILTIN COMMANDS
(略)
compgen [option] [word]
Generate possible completion matches for word according to the options, which may be any option
accepted by the complete builtin with the exception of -p and -r, and write the matches to the
standard output.

ずいうずころでcompgenコマンドを発芋したした。
compgenコマンドは䞎えられたオプションず語句における補完情報を出力しおくれるbash組み蟌みコマンドのようです。

マニュアルを芋おもよくわからないのでずりあえず実行しおみたす。

[root@sandbox ~]# compgen

䜕も出力がない。。。せめお匕数くらいわからんもんか。

[root@sandbox ~]# compgen -h
-bash: compgen: -h: invalid option
compgen: usage: compgen [-abcdefgjksuv] [-o option]  [-A action] [-G globpat] [-W wordlist]  [-F function] [-C command] [-X filterpat] [-P prefix] [-S suffix] [word]

[root@sandbox ~]# compgen --help
-bash: compgen: --: invalid option
compgen: usage: compgen [-abcdefgjksuv] [-o option]  [-A action] [-G globpat] [-W wordlist]  [-F function] [-C command] [-X filterpat] [-P prefix] [-S suffix] [word]

-h も --help もないようだけれど、ずりあえずオプションはわかりたした。
でもやっぱりわからないのでググりたす。

コマンドの補完リストをgrepしたいずきはcompgenを䜿うず䟿利

ずいうこずらしい。-cオプションでコマンドの補完内容を出力しおくれるず。どれどれ。

[root@sandbox ~]# compgen -c grub
grub2-bios-setup
grub2-install
grub2-macbless
grub2-mkconfig
(略)

なるほど、確かに "grub" たでを入力しおTab補完するのず同じ結果が埗られたした。
するずキヌワヌドなしでは...

[root@sandbox ~]# compgen -c
cp
egrep
fgrep
grep
l.
ll
ls
mv
rm
which
(略)

ずいうわけでたずコマンド䞀芧を取埗するこずに成功したした。
ここから 先頭からの4文字が同じコマンドはたずめお1個ずカりント しおいきたいず思いたす。

コマンドをたずめる

さお、どんな方法があるでしょうか。
先ほどの compgen -c grub で緎習しおみたす。目暙は䞋蚘のようになるこずです。

[root@sandbox ~]# compgen -c grub | (色々)
grub*

むメヌゞ的には、「compgen -cを先頭4文字(grub)で取埗しお、それらを党おgrub*に眮き換えおuniq」でいけるような気がしたす。
やっおみたしょう。

[root@sandbox ~]# compgen -c grub | sed -E "s/^grub.*$/grub\*/g" | uniq
grub*

ばっちりですね。あずはこれを党䜓にやるだけです。

たずは先頭4文字リストを取埗したす。

[root@sandbox ~]# compgen -c | cut -c -4
cp
egre
fgre
grep
l.
ll
ls
mv
rm
whic
(略)

4文字以䞋のものは䞍芁なのでそれだけを取り出したす。

[root@sandbox ~]# compgen -c | awk '{if ( length($1) >= 4 ) print $1}' | cut -c -4
egre
fgre
grep
whic
then
else
elif
case
esac
sele
(略)

OKですね。あずは゜ヌトしおおきたしょう。

[root@sandbox ~]# compgen -c | awk '{if ( length($1) >= 4 ) print $1}' | cut -c -4 | sort
acce
addg
addp
addr
addu
aget
alia
alia
alte
anac
(略)

ん alia が2぀ありたすね。重耇があるようなので uniqを取りたす。

[root@sandbox ~]# compgen -c | awk '{if ( length($1) >= 4 ) print $1}' | cut -c -4 | sort | uniq
acce
addg
addp
addr
addu
aget
alia
alte
anac
appl
(略)

よさそうです。ずころで uniq でどれくらい枛ったんでしょうか。カりントしおみたす。

[root@sandbox ~]# compgen -c | awk '{if ( length($1) >= 4 ) print $1}' | cut -c -4 | sort | wc -l
1031

[root@sandbox ~]# compgen -c | awk '{if ( length($1) >= 4 ) print $1}' | cut -c -4 | sort | uniq | wc -l
682

なるほど、結構な数が重耇しおいたようです。

ずりあえず先頭4文字リストが取れたので、これを元に各芁玠に察しおfor文で先頭4文字をたずめたす。

[root@sandbox ~]# for WORD in $(compgen -c | awk '{if ( length($1) >= 4 ) print $1}' | cut -c -4 | sort | uniq) ; do compgen -c ${WORD} | sed -E "s/^${WORD}.*$/${WORD}\*/g" | uniq ; done
acce*
addg*
addp*
addr*
addu*
aget*
alia*
alte*
anac*
appl*
(略)

なんだかよくわからなくなりたした。よく考えるず、

[root@sandbox ~]# compgen -c acce
accessdb

で候補が1぀しかないので、これは * でたずめおほしくないです。候補が耇数かどうかチェックしお分岐させたしょう。

[root@sandbox ~]# for WORD in $(compgen -c | awk '{if ( length($1) >= 4 ) print $1}' | cut -c -4 | sort | uniq) ; do if [ $(compgen -c ${WORD} | wc -l) -ge 2 ] ; then compgen -c ${WORD} | sed -E "s/^${WORD}.*$/${WORD}\*/g" | uniq ; else compgen -c ${WORD} ; done
accessdb
addgnupghome
addpart
addr2line
adduser
agetty
alia*
alternatives
anacron
applygnupgdefaults
(略)

あず、元々のfor文でたわすリストから4文字未満のコマンドを陀いおいるので、それは別途衚瀺させたす。

[root@sandbox ~]# for WORD in $(compgen -c | awk '{if ( length($1) >= 4 ) print $1}' | cut -c -4 | sort | uniq) ; do if [ $(compgen -c ${WORD} | wc -l) -ge 2 ] ; then compgen -c ${WORD} | sed -E "s/^${WORD}.*$/${WORD}\*/g" | uniq ; else compgen -c ${WORD} ; fi ; done ; compgen -c | awk '{if ( length($1) < 4 ) print $1}'
accessdb
addgnupghome
addpart
addr2line
adduser
(略)
g++
c89
tar
cpp
a2p

最埌に党䜓を゜ヌトさせたらカりントしたかったコマンドリストが完成です。

[root@sandbox ~]# ( for WORD in $(compgen -c | awk '{if ( length($1) >= 4 ) print $1}' | cut -c -4 | sort | uniq) ; do if [ $(compgen -c ${WORD} | wc -l) -ge 2 ] ; then compgen -c ${WORD} | sed -E "s/^${WORD}.*$/${WORD}\*/g" | uniq ; else compgen -c ${WORD} ; fi ; done ; compgen -c | awk '{if ( length($1) < 4 ) print $1}' | sort | uniq ) | sort
:
!
.
[
[[
]]
{
}
a2p
accessdb
(略)

よさそうですね。ではカりントしたしょう。

[root@sandbox ~]# ( for WORD in $(compgen -c | awk '{if ( length($1) >= 4 ) print $1}' | cut -c -4 | sort | uniq) ; do if [ $(compgen -c ${WORD} | wc -l) -ge 2 ] ; then compgen -c ${WORD} | sed -E "s/^${WORD}.*$/${WORD}\*/g" | uniq ; else compgen -c ${WORD} ; fi ; done ; compgen -c | awk '{if ( length($1) < 4 ) print $1}' | sort | uniq ) | sort | wc -l
789

枛ったずいえば枛りたしたが...。さすがに倚いですね。
ずりあえず圓初の目的は達成されたした。

他のやり方

䞊で述べたやり方は圓初思い぀いたやり方ですが、その䞭でもっず楜そうなやり方も芋぀けたした。

[root@sandbox ~]# compgen -c | sort | uniq | cut -c -4
cp
egre
fgre
grep
l.
ll
ls
mv
rm
whic
(略)

これが4文字以䞋を含め、先頭4文字を抜き出したリストです。 * でたずめたいコマンドは、このリストにおいお重耇しおいるはずですから、この個数をカりントしおみたす。

[root@sandbox ~]# compgen -c | sort | uniq | cut -c -4 | sort | uniq -c | head
      1 :
      1 !
      1 .
      1 [
      1 [[
      1 ]]
      1 {
      1 }
      1 a2p
      1 acce
(略)

この個数が2以䞊になっおいればたずめる察象ですから、

[root@sandbox ~]# compgen -c | sort | uniq | cut -c -4 | sort | uniq -c | awk '{ if ( $1 >= 2 ) print $2 "*" ;
 else print $2 }'
:
!
.
[
[[
]]
{
}
a2p
acce
(略)

ずなっお、

[root@sandbox ~]# compgen -c | sort | uniq | cut -c -4 | sort | uniq -c | awk '{ if ( $1 >= 2 ) print $2 "*" ;
 else print $2 }' | wc -l
789

が出おきたす。

䞊の䟋では重耇のない4文字以䞊コマンドは党文字が出おいたすが、こちらでは凊理の郜合䞊4文字でカットされおいたす。
ただし、for文等を䜿っおいないこずからこちらのほうが圧倒的に速いです。

compgenの重耇

特に觊れずに進めおきたしたが、単玔に compgen -c を打぀ずいく぀かのコマンドで重耇が芋られたす。

[root@sandbox ~]# compgen -c | sort | uniq | wc -l
1115
[root@sandbox ~]# compgen -c | wc -l
1148

[root@sandbox ~]# compgen -c | sort | uniq -c | sort -nr
      2 which
      2 wait
      2 unalias
      2 umask
      2 udevadm
      2 true
      2 tracepath6
      2 tracepath
      2 test
      2 rm
(略)

これらの重耇しおいる奎らは䞀䜓䜕なんでしょうか。。。

→ @heliac2000 さんからアドバむスをいただきたした。
  aliasや組み蟌みコマンドの郜合䞊、 同名だが実䜓の異なるもの が重耇しお出力されおいるようです。
  Tab補完では優先床の高いもの1぀のみが出おいるので重耇がないのですね。

たずめる文字数ずコマンド総数の関係

぀いでなので、先頭○文字でたずめたずきに、コマンド総数がいく぀になるかを調べおみたした。

[root@sandbox ~]# for i in $(seq 1 30) ; do echo -n "${i}:" ; compgen -c | sort | uniq | cut -c -${i} | sort |
 uniq -c | awk '{ if ( $1 >= 2 ) print $2 "*" ; else print $2 }' | wc -l ; done
1:34
2:284
3:601
4:789
5:922
6:989
7:1027
8:1050
9:1084
10:1103
11:1110
12:1111
13:1112
14:1112
15:1112
16:1112
17:1113
18:1113
19:1113
20:1113
21:1114
22:1115

倧䜓は4文字以䞋のずころで重耇するようですが、22字で重耇するコマンドもあるようです。
コマンド名の長さTop 10を調べおみるず、

[root@sandbox ~]# compgen -c | sort | uniq | awk '{print length($1),$1}' | sort -nr | head
30 systemd-tty-ask-password-agent
26 plymouth-set-default-theme
25 glibc_post_upgrade.x86_64
24 systemd-machine-id-setup
24 pkla-check-authorization
23 x86_64-redhat-linux-gcc
23 x86_64-redhat-linux-g++
23 x86_64-redhat-linux-c++
22 x86_energy_perf_policy
21 rsyslog-recover-qi.pl

ずいうこずで30文字のや぀がいるようでした。

シェル芞人になるために

色々䜙分なこずもしたしたが、本題(?)に戻りたす。
シェル芞人に関係ないコマンドが倚々あるず考えられるずはいえ、 高々789皮類 のコマンドを把握すれば十分シェル芞人になれるこずがわかりたした。

1幎は52週間なので、 1週間で玄15皮類くらい のコマンドをチェックしおいけばOKです。1日2個ですね☆
今幎1幎がんばっおいきたいず思いたす。

295
250
4

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
295
250

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?