https://www.itmedia.co.jp/news/articles/2112/29/news040.html
TL上でもチラチラ見かけた件。原因となった挙動、bashソースコードのこの部分だねというのも出てた気がする。むかーしからなんだろうか。
実行中に書き換えたり cp したりしようなどと思ったことはなかったので、bash は実行時に適時読み込みますという記述で初めてこの挙動を知った。いやはや。
お〜確かにそうだねという↓これは sleep 中に、ささっと別ターミナルで hoge!! 部分を fuga!! と書いた fuga.sh を cp 。Fedora 35 で。
$ cat hoge.sh; echo; bash --version ; bash -x ./hoge.sh ;echo ; cat hoge.sh
sleep 10
echo "$0: hoge!!"
GNU bash, バージョン 5.1.8(1)-release (x86_64-redhat-linux-gnu)
Copyright (C) 2020 Free Software Foundation, Inc.
ライセンス GPLv3+: GNU GPL バージョン 3 またはそれ以降 <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.
+ sleep 10
+ echo './hoge.sh: fuga!!'
./hoge.sh: fuga!!
sleep 10
echo "$0: fuga!!"
zsh の場合
$ cat hoge.sh; echo; zsh --version ; zsh -x ./hoge.sh ;echo ; cat hoge.sh
sleep 10
echo "$0: hoge!!"
zsh 5.8 (x86_64-redhat-linux-gnu)
+./hoge.sh:1> sleep 10
+./hoge.sh:2> echo './hoge.sh: hoge!!'
./hoge.sh: hoge!!
sleep 10
echo "$0: fuga!!"
長いスクリプトだと違ってきそうな? zsh で -x だとスクリプト名も入る、というのをついでに知れた。
追記: FreeBSD 13 の "sh" で何行か echo させてみると冒頭は hoge で途中から fuga 。上のそのままにその後適当にecho
echo "01:hogeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee"
(中略。20まで。)
echo "20:hogeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee"
で実行中に裏で cp すれば途中から変わった。
$ head -2 hoge2.sh ; echo ; sh -x ./hoge2.sh
sleep 10
echo "$0: hoge!!"
+ sleep 10
+ echo './hoge2.sh: hoge!!'
./hoge2.sh: hoge!!
+ echo 01:hogeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
01:hogeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
+ echo 02:hogeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
02:hogeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
+ echo 03:hogeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
(略)
+ echo 17:hogeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
17:hogeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
+ echo 18:hugaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
18:hugaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
+ echo 19:fugaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
19:fugaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
+ echo 20:fugaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
20:fugaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
↑の18: のところ。1024文字(バイト)っぽい。
(パッケージの) zsh だと変わらず、hoge 。どうなってるかはあとはコード見るのが早いな。
どことなく、昔あった BSD magazine の「デーモン君のソース探検」っぽさを感じた。「うわーん、実行中のスクリプトをコピーして置き換えたら何たらかんたら・・」で、パパが「どれどれ、では sh のソースを見てみようか」的な。いやまぁ、そもそもそんなオペレーションをするなという話ではあるけど。。学びがあるね。
さらに追記: OpenBSD 7.0 の、"ksh" ( /bin/sh が /bin/ksh )
$ ksh -x hoge.sh
+ sleep 10
+ echo hoge.sh: hoge!!
hoge.sh: hoge!!
+ echo 01:hogeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
01:hogeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
+ echo 02:hogeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
02:hogeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
+ echo 03:hogeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
03:hogeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
(略)
+ echo 09:hogeeeeeeeeaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
09:hogeeeeeeeeaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
+ echo 10:fugaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
10:fugaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
ホゲーーーが途中から ホゲーーーああああになった。512文字(バイト)。
・・・じゃ、Fedora 35 の ksh 。
$ ksh --version; ksh -x hoge.sh
version sh (AT&T Research) 93u+m/1.0.0-beta.1 2021-05-10
+ sleep 10
+ echo 'hoge.sh: hoge!!'
hoge.sh: hoge!!
+ echo 01:hogeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
01:hogeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
+ echo 02:hogeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
02:hogeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
+ echo 03:hogeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
03:hogeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
(略)
+ echo 18:hogeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
18:hogeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
+ echo 19:hogeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
19:hogeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
+ echo 20:hogeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
20:hogeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
hoge のまま。
$ rpm -qi ksh
Name : ksh
Epoch : 3
Version : 1.0.0~beta.1
Release : 1.fc35
Architecture: x86_64
Install Date: 2021年12月30日 08時41分02秒
Group : Unspecified
Size : 3166151
License : EPL-1.0
Signature : RSA/SHA256, 2021年08月03日 12時24分51秒, Key ID db4639719867c58f
Source RPM : ksh-1.0.0~beta.1-1.fc35.src.rpm
Build Date : 2021年08月03日 12時08分15秒
Build Host : buildvm-x86-08.iad2.fedoraproject.org
Packager : Fedora Project
Vendor : Fedora Project
URL : http://www.kornshell.com/
Bug URL : https://bugz.fedoraproject.org/ksh
Summary : The Original ATT Korn Shell
Description :
KSH-93 is the most recent version of the KornShell by David Korn of
AT&T Bell Laboratories.
KornShell is a shell programming language, which is upward compatible
with "sh" (the Bourne Shell)
では今度はFreeBSDの ksh パッケージをば・・と思ったが ksh オオスギィ!
$ pkg search ksh|grep Korn
ast-ksh-20141224_1 KornShell 93
ksh2020-2020_1 Development branch of AT&T KornShell 93
ksh93-93.u_1,2 AT&T KornShell 93
ksh93-devel-2020.06.30 Development branch of AT&T KornShell 93
mksh-59c MirBSD Korn Shell
oksh-6.9,1 Portable OpenBSD Korn shell
pdksh-5.2.14p2_6 The Public Domain Korn Shell
まぁ oksh が OpenBSD のそれと同じ挙動でしょう、と予測し答え合わせ。
$ oksh -x hoge2.sh
+ sleep 10
+ echo hoge2.sh: hoge!!
hoge2.sh: hoge!!
+ echo 01:hogeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
01:hogeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
+ echo 02:hogeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
02:hogeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
+ echo 03:hogeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
03:hogeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
(snip)
+ echo 09:hogeeeeeeeeaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
09:hogeeeeeeeeaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
+ echo 10:fugaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
10:fugaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
正解。
以下 pdksh と ksh93
$ ksh -x hoge2.sh
+ alias h=fc -l
+ alias j=jobs
+ alias m=less
+ alias ll=ls -laFo
+ alias l=ls -l
+ alias g=egrep -i
+ sleep 10
+ echo hoge2.sh: hoge!!
hoge2.sh: hoge!!
+ echo 01:hogeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
01:hogeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
+ echo 02:hogeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
02:hogeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
+ echo 03:hogeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
03:hogeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
(中略)ところで alias が出てくるの何・・・?
+ echo 09:hogeeeeeeeeaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
09:hogeeeeeeeeaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
+ echo 10:fugaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
10:fugaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
$ ksh93 -x hoge2.sh
+ sleep 10
+ echo 'hoge2.sh: hoge!!'
hoge2.sh: hoge!!
+ echo 01:hogeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
01:hogeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
+ echo 02:hogeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
02:hogeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
+ echo 03:hogeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
03:hogeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
(中略)
+ echo 18:hogeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
18:hogeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
+ echo 19:hogeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
19:hogeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
+ echo 20:hogeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
20:hogeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
$ pkg info -f oksh pdksh ksh93
oksh-6.9,1
Name : oksh
Version : 6.9,1
Installed on : Fri Dec 10 03:35:22 2021 JST
Origin : shells/oksh
Architecture : FreeBSD:13:amd64
Prefix : /usr/local
Categories : shells
Licenses : PD
Maintainer : tobik@FreeBSD.org
WWW : https://github.com/ibara/oksh
Comment : Portable OpenBSD Korn shell
Options :
CURSES : on
STATIC : off
Annotations :
FreeBSD_version: 1300139
repo_type : binary
repository : FreeBSD
Flat size : 324KiB
Description :
oksh is the portable version of the OpenBSD Korn shell, a continuation
of the Public Domain Korn Shell (PDKSH). Its command language is a
superset of the sh(1) shell language. oksh is best known as the
default user shell and /bin/sh on OpenBSD.
WWW: https://github.com/ibara/oksh
pdksh-5.2.14p2_6
Name : pdksh
Version : 5.2.14p2_6
Installed on : Fri Dec 10 03:38:58 2021 JST
Origin : shells/pdksh
Architecture : FreeBSD:13:amd64
Prefix : /usr/local
Categories : shells
Licenses : BSD3CLAUSE
Maintainer : rodrigo@FreeBSD.org
WWW : http://www.cs.mun.ca/~michael/pdksh/
Comment : The Public Domain Korn Shell
Options :
STATIC : off
Annotations :
FreeBSD_version: 1300139
repo_type : binary
repository : FreeBSD
Flat size : 320KiB
Description :
PDKSH is the Public Domain Korn Shell. Its command language is a
superset of the sh(1) shell language.
WWW: http://www.cs.mun.ca/~michael/pdksh/
ksh93-93.u_1,2
Name : ksh93
Version : 93.u_1,2
Installed on : Fri Dec 10 03:38:58 2021 JST
Origin : shells/ksh93
Architecture : FreeBSD:13:amd64
Prefix : /usr/local
Categories : shells
Licenses : EPL
Maintainer : cy@FreeBSD.org
WWW : http://www.kornshell.com/
Comment : AT&T KornShell 93
Options :
EXAMPLES : on
KSH : off
KSH93 : on
STATIC : off
Annotations :
FreeBSD_version: 1300139
repo_type : binary
repository : FreeBSD
Flat size : 1.94MiB
Description :
KSH-93 is the most recent version of the KornShell Language described
in "The KornShell Command and Programming Language," by Morris
Bolsky and David Korn of AT&T Bell Laboratories. The KornShell is
a shell programming language, which is upward compatible with "sh"
(the Bourne Shell), and is intended to conform to the IEEE P1003.2/ISO
9945.2 Shell and Utilities standard. KSH-93 provides an enhanced
programming environment in addition to the major command-entry
features of the BSD shell "csh". With KSH-93, medium-sized programming
tasks can be performed at shell-level without a significant loss
in performance. In addition, "sh" scripts can be run on KSH-93
without modification.
WWW: http://www.kornshell.com/
そういえば OmniOS は消しちゃったな・・・
0 件のコメント:
コメントを投稿