忍者ブログ
  • 2024.03«
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • » 2024.05
[PR]
×

[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。

【2024/04/27 15:38 】 |
mkfifoの使い道
mkfifoを使うと標準出力と標準入力を用いてインタプリタのプログラムを組むことが容易になる。

pythonプログラム(subprocessを用いていない)
import re
def send(s):
    RET = "@@RETURN VALUE@@"
    print(s+";echo {} $?".format(RET))
    outputs = []
    while True:
        output = input()
        if re.search("^{} ".format(RET), output):
            m = re.match("^{} (.*)".format(RET), output)
            return (int(m.groups()[0]), outputs)
        outputs.append(output)

import sys
print(send("ping www.google.co.jp -c 4"), file=sys.stderr)
print(send("ls ----"), file=sys.stderr)
subprocessなしでシェルをキック
$ mkfifo net
$ bash < net 2>&1 | tee /dev/stderr | python ls.py | tee net
ping www.google.co.jp -c 4;echo @@RETURN VALUE@@ $?
PING www.google.co.jp (142.250.206.195) 56(84) bytes of data.
64 bytes from kix07s07-in-f3.1e100.net (142.250.206.195): icmp_seq=1 ttl=56 time=14.8 ms
64 bytes from kix07s07-in-f3.1e100.net (142.250.206.195): icmp_seq=2 ttl=56 time=16.6 ms
64 bytes from kix07s07-in-f3.1e100.net (142.250.206.195): icmp_seq=3 ttl=56 time=15.8 ms
64 bytes from kix07s07-in-f3.1e100.net (142.250.206.195): icmp_seq=4 ttl=56 time=15.4 ms

--- www.google.co.jp ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3005ms
rtt min/avg/max/mdev = 14.760/15.666/16.645/0.681 ms
@@RETURN VALUE@@ 0
(0, ['PING www.google.co.jp (142.250.206.195) 56(84) bytes of data.', '64 bytes from kix07s07-in-f3.1e100.net (142.250.206.195): icmp_seq=1 ttl=56 time=14.8 ms', '64 bytes from kix07s07-in-f3.1e100.net (142.250.206.195): icmp_seq=2 ttl=56 time=16.6 ms', '64 bytes from kix07s07-in-f3.1e100.net (142.250.206.195): icmp_seq=3 ttl=56 time=15.8 ms', '64 bytes from kix07s07-in-f3.1e100.net (142.250.206.195): icmp_seq=4 ttl=56 time=15.4 ms', '', '--- www.google.co.jp ping statistics ---', '4 packets transmitted, 4 received, 0% packet loss, time 3005ms', 'rtt min/avg/max/mdev = 14.760/15.666/16.645/0.681 ms'])
ls ----;echo @@RETURN VALUE@@ $?
ls: unrecognized option '----'
Try 'ls --help' for more information.
@@RETURN VALUE@@ 2
(2, ["ls: unrecognized option '----'", "Try 'ls --help' for more information."])

ほかにも、通信系のインタプリタをライブラリなしで実行することができる。
便利なり。
その代わり、標準出力は使えないが(笑)簡易RPA的なコンソールプログラムはすぐに作れる。

拍手[2回]

PR
【2022/08/14 07:38 】 | linux | 有り難いご意見(2)
<<MovableType To Yaml のsedスクリプト作ってみた。 | ホーム | Ubuntuの復旧 Ubuntu18.04→Ubuntu20.04へのアップデート>>
有り難いご意見
無題
コンパクトでいいですね。今度、使ってみよ。
【2022/08/15 17:44】| | ゼンガイチ #4a2a24980a [ 編集 ]


無題
意外と応用ききそうです。(^^
【2022/08/15 18:12】| | nwpfh #5323276394 [ 編集 ]


貴重なご意見の投稿















前ページ | ホーム | 次ページ

忍者ブログ [PR]