私は誰?


以前にも書いたネタですが、Linux のように /proc があれば以下のようにして取得できます。

#! /usr/bin/gawk -f
# who_am_i.awk - print my name :-P
# usage: gawk -f who_am_i.awk
#   input:  none
#   output: print my name

BEGIN {
    getline s < "/proc/self/cmdline";
    split(s, a_s, "\0");
    print "My name is:", a_s[3];
}
$ gawk -f who_am_i.awk
My name is: who_am_i.awk

複数 '-f' を繋げる場合には以下のようにします。

#! /usr/bin/gawk -f
# who_are_them.awk - print their names :-P
# usage: gawk -f who_are_them.awk
#   input:  none
#   output: print their names

BEGIN {
    getline s <"/proc/self/cmdline";
    split(s, a_s, "\0");
    for (i in a_s) {
        if (a_s[i] == "-f") {
            print "My name is: " a_s[i + 1];
        } else if (substr(a_s[i], 1, 2) == "-f") {
            print "My name is: " substr(a_s[i], 3);
        }
    }
}

他にも ps コマンドから取ったり、history から取ったりもできるようですが、awk ではベストな方法はなさそうですね。