質問編集履歴

4

read -tを使った例を追記

2019/06/30 14:06

投稿

YouheiSakurai
YouheiSakurai

スコア6142

test CHANGED
File without changes
test CHANGED
@@ -85,3 +85,19 @@
85
85
 
86
86
 
87
87
  よろしくお願いします。
88
+
89
+
90
+
91
+ # 追記(回答を受けて)
92
+
93
+
94
+
95
+ 奥が深い。。。
96
+
97
+
98
+
99
+ ```bash
100
+
101
+ watchdog() { T=$1; shift && while :; do read -t $T || "$@"; done }
102
+
103
+ ```

3

2019/06/30 14:06

投稿

YouheiSakurai
YouheiSakurai

スコア6142

test CHANGED
File without changes
test CHANGED
@@ -1,4 +1,4 @@
1
- 以下の動きをするシンプルなコマンドについて、すでにもうどこかに存在しそうな気もするのですが、見つけられなかったのでPythonで書きました。でも、できれば再発明した車輪をプロジェクトに組み込むのは避けたいと思っています。こんな動きをする既存のLinuxコマンド or Pythonライブラリ or Goパッケージをご存知の方はいませんでしょうか?
1
+ 以下の動きをするシンプルなコマンドについて、すでにもうどこかに存在しそうな気もするのですが、見つけられなかったのでPythonで書きました。でも、できれば再発明した車輪をプロジェクトに組み込むのは避けたいと思っています。こんな動きをする既存のLinuxコマンド or Pythonライブラリ or Goパッケージ or シェル芸をご存知の方はいませんでしょうか?
2
2
 
3
3
 
4
4
 

2

exceptの位置が悪い

2019/06/30 12:55

投稿

YouheiSakurai
YouheiSakurai

スコア6142

test CHANGED
File without changes
test CHANGED
@@ -30,19 +30,13 @@
30
30
 
31
31
  while True:
32
32
 
33
- try:
33
+ if select([fp], [], [], timeout)[0]:
34
34
 
35
- if select([fp], [], [], timeout)[0]:
35
+ next(fp)
36
36
 
37
- next(fp)
37
+ else:
38
38
 
39
- else:
40
-
41
- run(cmd)
39
+ run(cmd)
42
-
43
- except KeyboardInterrupt:
44
-
45
- break
46
40
 
47
41
 
48
42
 
@@ -59,6 +53,10 @@
59
53
  print("Usage: %s timeout command [argument] ..." % argv[0],
60
54
 
61
55
  file=stderr)
56
+
57
+ except KeyboardInterrupt:
58
+
59
+ pass
62
60
 
63
61
 
64
62
 

1

fpの位置がしっくりこない

2019/06/30 12:39

投稿

YouheiSakurai
YouheiSakurai

スコア6142

test CHANGED
File without changes
test CHANGED
@@ -26,7 +26,7 @@
26
26
 
27
27
 
28
28
 
29
- def watchdog(timeout, cmd, fp):
29
+ def watchdog(fp, timeout, cmd):
30
30
 
31
31
  while True:
32
32
 
@@ -52,7 +52,7 @@
52
52
 
53
53
  try:
54
54
 
55
- watchdog(int(argv[1]), argv[2:], stdin)
55
+ watchdog(stdin, int(argv[1]), argv[2:])
56
56
 
57
57
  except (IndexError, ValueError):
58
58