R.A. Epigonos et al.

[dash] シェルスクリプトの関数 vs alias (内容説明視点)

単純なコマンドの内容説明を目的に、そのコマンドに名前をつけたいケースがある。このとき、function を使うか alias を使うかの選択を迫られる。set -x した際のログ短縮化や呼び出しコストの最小化が重要なら alias を使うべき。説明内容をログに残すことが重要なら関数である。

具体的には、スクリプトの「回数指定して ping」という箇所に名前をつけることを考える。

$ cat --number test.sh
     1  #!/bin/dash
     2  set -eux;
     3
     4  _ping_to_host_with_count_function () {
     5          ping \
     6                  -c "${1}" \
     7                  "${2}" \
     8          ;
     9  }
    10
    11  _ping_to_host_with_count_function_function () {
    12          _ping_to_host_with_count_function \
    13                  ${@} \
    14          ;
    15  }
    16
    17
    18  alias _ping_to_host_with_count_alias='ping \
    19          -c \
    20  '
    21
    22  alias _ping_to_host_with_count_alias_alias='_ping_to_host_with_count_alias'
    23
    24  _ping_to_host_with_count_function \
    25          4 \
    26          example.com \
    27  ;
    28
    29  _ping_to_host_with_count_alias \
    30          4 \
    31          example.com \
    32  ;
    33
    34  _ping_to_host_with_count_function_function \
    35          4 \
    36          example.com \
    37  ;
    38
    39  _ping_to_host_with_count_alias_alias \
    40          4 \
    41          example.com \
    42  ;
    43
    44  exit 0;

このシェルスクリプトを実行すると以下のようになる。function で呼び出した場合には _ping_to_host_with_count_function がログに残るが、alias で呼び出した場合にはそれがないことがわかる。また、関数を更に関数で説明するケースではその説明が全てログに残るのに対し、aliasを更にaliasで説明するケースでは全くログが残らない事がわかる。

$ sh test.sh
+ alias _ping_to_host_with_count_alias=ping \
        -c \

+ alias _ping_to_host_with_count_alias_alias=_ping_to_host_with_count_alias
+ _ping_to_host_with_count_function 4 example.com
+ ping -c 4 example.com
PING example.com (93.184.216.34) 56(84) bytes of data.
64 bytes from 93.184.216.34 (93.184.216.34): icmp_seq=1 ttl=52 time=104 ms
64 bytes from 93.184.216.34 (93.184.216.34): icmp_seq=2 ttl=52 time=104 ms
64 bytes from 93.184.216.34 (93.184.216.34): icmp_seq=3 ttl=52 time=104 ms
64 bytes from 93.184.216.34 (93.184.216.34): icmp_seq=4 ttl=52 time=104 ms

--- example.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3005ms
rtt min/avg/max/mdev = 104.726/104.841/104.947/0.082 ms
+ ping -c 4 example.com
PING example.com (93.184.216.34) 56(84) bytes of data.
64 bytes from 93.184.216.34 (93.184.216.34): icmp_seq=1 ttl=52 time=104 ms
64 bytes from 93.184.216.34 (93.184.216.34): icmp_seq=2 ttl=52 time=104 ms
64 bytes from 93.184.216.34 (93.184.216.34): icmp_seq=3 ttl=52 time=104 ms
64 bytes from 93.184.216.34 (93.184.216.34): icmp_seq=4 ttl=52 time=104 ms

--- example.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3005ms
rtt min/avg/max/mdev = 104.777/104.815/104.863/0.231 ms
+ _ping_to_host_with_count_function_function 4 example.com
+ _ping_to_host_with_count_function 4 example.com
+ ping -c 4 example.com
PING example.com (93.184.216.34) 56(84) bytes of data.
64 bytes from 93.184.216.34 (93.184.216.34): icmp_seq=1 ttl=52 time=104 ms
64 bytes from 93.184.216.34 (93.184.216.34): icmp_seq=2 ttl=52 time=104 ms
64 bytes from 93.184.216.34 (93.184.216.34): icmp_seq=3 ttl=52 time=104 ms
64 bytes from 93.184.216.34 (93.184.216.34): icmp_seq=4 ttl=52 time=104 ms

--- example.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3004ms
rtt min/avg/max/mdev = 104.741/104.801/104.861/0.399 ms
+ ping -c 4 example.com
PING example.com (93.184.216.34) 56(84) bytes of data.
64 bytes from 93.184.216.34 (93.184.216.34): icmp_seq=1 ttl=52 time=104 ms
64 bytes from 93.184.216.34 (93.184.216.34): icmp_seq=2 ttl=52 time=104 ms
64 bytes from 93.184.216.34 (93.184.216.34): icmp_seq=3 ttl=52 time=104 ms
64 bytes from 93.184.216.34 (93.184.216.34): icmp_seq=4 ttl=52 time=104 ms

--- example.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3003ms
rtt min/avg/max/mdev = 104.754/104.803/104.862/0.046 ms
+ exit 0

ソーシャルブックマーク

  1. はてなブックマーク
  2. Google Bookmarks
  3. del.icio.us

ChangeLog

  1. Posted: 2010-10-07T13:57:34+09:00
  2. Modified: 2010-10-07T13:57:34+09:00
  3. Generated: 2024-11-18T23:09:16+09:00