( exec sh -i 3<<SCRIPT 4<&0 <&3 ⏎
echo "do this thing"
echo "do that thing"
exec 3>&- <&4
SCRIPT
)
exec $0.
または、これらのファイル記述子のいずれかが現在使用されていない端末デバイスに向けられている場合は、スクリプトを使用して行う方が良いでしょう-他のプロセスもその端末をチェックしたいことを覚えておく必要があります。
ところで、もしあなたの目標が、私がそうであるように、スクリプトの環境をそれを実行した後に保存することであるなら、あなたはたぶんはるかに良いサービスを受けるでしょう:
. ./script
シェル.dot
とbash's source
はまったく同じではありません-シェル.dot
は特別なシェルビルトインとして指定されたPOSIXであるため、入手できる限り保証されていますが、これは決してそこにあるという保証ではありません...
上記は、ほとんど問題なく期待どおりに動作するはずです。たとえば、次のことができます。
( exec sh -i 3<<SCRIPT 4<&0 <&3 ⏎
echo "do this thing"
echo "do that thing"
$(cat /path/to/script)
exec 3>&- <&4
SCRIPT
)
シェルはスクリプトを実行し、インタラクティブプロンプトに戻ります-スクリプトからシェルを実行するexit
、つまりプロセスをバックグラウンド化しない限り、i / oをリンクします/dev/null.
デモ:
% printf 'echo "%s"\n' "These lines will print out as echo" \
"statements run from my interactive shell." \
"This will occur before I'm given the prompt." >|/tmp/script
% ( exec sh -i 3<<SCRIPT 4<&0 <&3
echo "do this thing"
echo "do that thing"
$(cat /tmp/script)
exec 3>&- <&4
SCRIPT
)
sh-4.3$ echo "do this thing"
do this thing
sh-4.3$ echo "do that thing"
do that thing
sh-4.3$ echo "These lines will print out as echo"
These lines will print out as echo
sh-4.3$ echo "statements run from my interactive shell."
statements run from my interactive shell.
sh-4.3$ echo "This will occur before I'm given the prompt."
This will occur before I'm given the prompt.
sh-4.3$ exec 3>&- <&4
sh-4.3$
たくさんの JOBS
シェルのビルトインタスク管理オプションについてもう少し詳しく知っておくべきだと思う。@Kiwyと@jillagreの両方は、回答ですでにこれに触れていますが、詳細を説明する必要があるかもしれません。そして、私はすでに内蔵の1 POSIX指定の特別なシェルを述べたが、きたset, jobs, fg,
とbg
別の答えが示すように、いくつかのより多くのであり、trap
そしてkill
より多くの、まだ2つです。
同時実行中のバックグラウンドプロセスのステータスに関するインスタント通知をまだ受信していない場合、現在のシェルオプションがPOSIXで指定されたデフォルトのに設定されているためですが-m
、set -b
代わりにこれらを非同期に取得できます。
% man set
−b This option shall be supported if the implementation supports the
User Portability Utilities option. It shall cause the shell to
notify the user asynchronously of background job completions. The
following message is written to standard error:
"[%d]%c %s%s\n", <job-number>, <current>, <status>, <job-name>
where the fields shall be as follows:
<current> The character '+' identifies the job that would be
used as a default for the fg or bg utilities; this
job can also be specified using the job_id "%+" or
"%%". The character '−' identifies the job that
would become the default if the current default job
were to exit; this job can also be specified using
the job_id "%−". For other jobs, this field is a
<space>. At most one job can be identified with '+'
and at most one job can be identified with '−'. If
there is any suspended job, then the current job
shall be a suspended job. If there are at least two
suspended jobs, then the previous job also shall be a
−m This option shall be supported if the implementation supports the
User Portability Utilities option. All jobs shall be run in their
own process groups. Immediately before the shell issues a prompt
after completion of the background job, a message reporting the
exit status of the background job shall be written to standard
error. If a foreground job stops, the shell shall write a message
to standard error to that effect, formatted as described by the
jobs utility. In addition, if a job changes status other than
exiting (for example, if it stops for input or output or is
stopped by a SIGSTOP signal), the shell shall write a similar
message immediately prior to writing the next prompt. This option
is enabled by default for interactive shells.
Unixベースのシステムの非常に基本的な機能は、processを処理する方法signals
です。私はかつて、このプロセスをダグラス・アダムズの「今の惑星」の説明に例える主題に関する啓発的な記事を読みました。
「ギャラクシーへのヒッチハイカーのガイドで、ダグラス・アダムズは、太いで非常に激しく噛むことによって人間と通信する鋭い歯を持つたくさんの落ち込んだ人間と動物の種が生息する非常に鈍い惑星について言及します。これは驚くべきことです。 UNIXと同様、カーネルはプロセスに麻痺または致命的な信号を送信することでプロセスと通信します。プロセスは信号の一部を傍受し、状況に適応しようとしますが、ほとんどはそうではありません。
これはを参照していkill signals
ます。
% kill -l
> HUP INT QUIT ILL TRAP ABRT BUS FPE KILL USR1 SEGV USR2 PIPE ALRM TERM STKFLT CHLD CONT STOP TSTP TTIN TTOU URG XCPU XFSZ VTALRM PROF WINCH POLL PWR SYS
少なくとも私にとっては、上記の引用は多くの質問に答えました。たとえば、私は常に非常に奇妙であり、dd
プロセスを監視したい場合にそれが必要であると直観的ではないと考えていましkill
た。読んだ後、それは理にかなっています。
私は彼らのほとんどが正当な理由で適応しようとしないと言います-開発者があなたにとって重要だと思った情報であなたの端末をスパムするプロセスの束を持つことは恩恵よりもはるかに大きな迷惑になる可能性があります。
端末の構成(で確認できstty -a
ます)に応じて、現在のフォアグラウンドプロセスグループリーダー(シェルである可能性CTRL+Z
が高い)にaを転送するSIGTSTP
ように設定されている可能性がありますtrap
。繰り返しますが、@ jillagreと@Kiwyの回答が示すように、この機能を好みに合わせて調整することを止めることはできません。
SCREEN JOBS
したがって、これらの機能を利用するには、まずそれらを理解し、自分のニーズに合わせて処理をカスタマイズすることが期待されます。たとえば、Githubで次のscreen
キーバインドを含むこのscreenrcを見つけましたSIGTSTP
。
# hitting 'C-z C-z' will run Ctrl+Z (SIGTSTP, suspend as usual)
bind ^Z stuff ^Z
# hitting 'C-z z' will suspend the screen client
bind z suspend
これにより、子screen
プロセスとして実行中のプロセス、またはscreen
希望どおりに子プロセス自体を一時停止するのが簡単になります。
そしてその直後:
% fg
または:
% bg
必要に応じてプロセスをフォアグラウンドまたはバックグラウンドします。jobs
内蔵のは、いつでもあなたにこれらのリストを提供することができます。-l
オペランドを追加すると、pidの詳細が含まれます。