jobs/wc: 奇妙な戻り値を取得

jobs/wc: 奇妙な戻り値を取得

これらの戻り値は理解できません。

を実行すると、jobs -s | wc -l私のディレクトリに応じて異なる結果が表示されます。

タスクを一時停止してディレクトリに戻ると、正しい答えが得られます。

別のディレクトリにある場合は、正解に対して+1を取得します。

スクリーンショットを見る:

ここに画像の説明を入力してください。

また、実行してjobs -s | > test.txt1行の文書を取得し、実行してwc -l < test.txt正しい出力を得ました。

原因は何か知っていますか?ご覧のとおり、私のシェルプロンプトのバックグラウンドタスクインジケータ(右、青)がめちゃくちゃになりました。

この機能を修正する方法について提案を送信していただきありがとうございます。

#tells us how many jobs are in the background  
function jobs_status() {
  count=$(jobs -s | wc -l)
  if [[ $count -ne "0" ]]; then
    echo "$bg_jobs$split$fg_text $count $fg_jobs"
  fi
}

ベストアンサー1

後者の場合、2行があるため、1つは表示され、もう1つはサブシェルに表示され、末尾の改行が計算されるため表示されません。この動作がに存在し、zsh他の形式でも同様に動作することを確認しました。bashzshbash

以下でASCIIダンプを確認してくださいzsh

/tmp/test% jobs
[1]  + running    tail -f /var/log/syslog

/tmp/test% jobs | wc -l
1

/tmp/test% jobs | od -c
0000000   [   1   ]           +       r   u   n   n   i   n   g        
0000020           t   a   i   l       -   f       /   v   a   r   /   l
0000040   o   g   /   s   y   s   l   o   g  \n
0000052

/tmp/test% cd ..

/tmp% jobs | wc -l
2

/tmp% jobs | od -c
0000000   [   1   ]           +       r   u   n   n   i   n   g        
0000020           t   a   i   l       -   f       /   v   a   r   /   l
0000040   o   g   /   s   y   s   l   o   g  \n   (   p   w   d       n
0000060   o   w   :       /   t   m   p   )  \n
0000072

シェルが現在の作業ディレクトリ(look)を追跡しているようです。( p w d n 0000060 o w : / t m p )角かっこはサブシェルを表します。

関連ソースコードは次のとおりですzshjobs.c

/* print "(pwd now: foo)" messages: with (lng & 4) we are printing
 * the directory where the job is running, otherwise the current directory
 */

    if ((lng & 4) || (interact && job == thisjob &&
                      jn->pwd && strcmp(jn->pwd, pwd))) {
        doneprint = 1;
        fprintf(fout, "(pwd %s: ", (lng & 4) ? "" : "now");
        fprintdir(((lng & 4) && jn->pwd) ? jn->pwd : pwd, fout);
        fprintf(fout, ")\n");
        fflush(fout);
    }

以下bashがありますが:

      if (strcmp (temp, jobs[job_index]->wd) != 0)
        fprintf (stream,
          _("  (wd: %s)"), polite_directory_format (jobs[job_index]->wd));

必要なものを取得するには、jobsサブシェルで実行できます。

( jobs ) | wc -l

おすすめ記事