您如何确定正在输入您的实际命令?

St.*_*son 9 linux bash pipe

假设我有一个名为 .bash 的 bash 脚本log.sh。在这个脚本中,我想从管道读取输入,但我也想知道用于将输入管道输入到我的命令。例子:

tail -f /var/log/httpd/error | log.sh
Run Code Online (Sandbox Code Playgroud)

在shell脚本中,我想知道命令tail -f /var/log/httpd/error.

aki*_*ira 10

管道将作为一个条目出现在您的进程的打开文件描述符列表中:

 % ls -l /proc/PID/fd
 lr-x------ 1 xyz xyz 64 Feb 11 08:05 0 -> pipe:[124149866]
 lrwx------ 1 xyz xyz 64 Feb 11 08:05 1 -> /dev/pts/2
 lrwx------ 1 xyz xyz 64 Feb 11 08:05 2 -> /dev/pts/2
 lr-x------ 1 xyz xyz 64 Feb 11 08:05 10 -> /tmp/foo.sh
Run Code Online (Sandbox Code Playgroud)

你也可以使用类似的东西:

 % lsof -p PID
 sh      29890 xyz  cwd    DIR   0,44    4096  77712070 /tmp
 sh      29890 xyz  rtd    DIR   0,44    4096  74368803 /
 sh      29890 xyz  txt    REG   0,44   83888  77597729 /bin/dash
 sh      29890 xyz  mem    REG   0,44 1405508  79888619 /lib/tls/i686/cmov/libc-2.11.1.so
 sh      29890 xyz  mem    REG   0,44  113964  79874782 /lib/ld-2.11.1.so
 sh      29890 xyz    0r  FIFO    0,6         124149866 pipe
 sh      29890 xyz    1u   CHR  136,2                 4 /dev/pts/2
 sh      29890 xyz    2u   CHR  136,2                 4 /dev/pts/2
 sh      29890 xyz   10r   REG   0,44      66  77712115 /tmp/foo.sh
Run Code Online (Sandbox Code Playgroud)

因此,比您拥有管道的 inode :) 您现在可以搜索/proc/该管道下的所有其他进程。然后您将拥有向您发送管道的命令:

 % lsof | grep 124149866 
 cat     29889 xyz    1w  FIFO                0,6          124149866 pipe
 sh      29890 xyz    0r  FIFO                0,6          124149866 pipe
Run Code Online (Sandbox Code Playgroud)

在这个例子中,通过cat管道传送到 wards sh。在/proc/29889你可以找到一个名为cmdline告诉你,究竟什么叫:

 % cat /proc/29889/cmdline
 cat/dev/zero%  
Run Code Online (Sandbox Code Playgroud)

命令行的字段用 NUL 分隔,因此看起来有点难看 :)


Mik*_*kel 7

Akira 建议使用lsof.

以下是编写脚本的方法:

whatpipe2.sh

#!/bin/bash

pid=$$
pgid=$(ps -o pgid= -p $pid)
lsofout=$(lsof -g $pgid)
pipenode=$(echo "$lsofout" | awk '$5 == "0r" { print $9 }')
otherpids=$(echo "$lsofout" | awk '$5 == "1w" { print $2 }')
for pid in $otherpids; do
    if cmd=$(ps -o cmd= -p $pid 2>/dev/null); then
        echo "$cmd"
        break
    fi
done
Run Code Online (Sandbox Code Playgroud)

运行它:

$ tail -f /var/log/messages | ./whatpipe2.sh
tail -f /var/log/messages
^C
Run Code Online (Sandbox Code Playgroud)

另一种方法是使用进程组。

whatpipe1.sh

#!/bin/bash    

pid=$$
# ps output is nasty, can (and usually does) start with spaces
# to handle this, I don't quote the "if test $_pgrp = $pgrp" line below
pgrp=$(ps -o pgrp= -p $pid)
psout=$(ps -o pgrp= -o pid= -o cmd=)
echo "$psout" | while read _pgrp _pid _cmd; do
    if test $_pgrp = $pgrp; then
        if test $_pid != $pid; then
            case $_cmd in
            ps*)
                # don't print the "ps" we ran to get this info
                # XXX but this actually means we exclude any "ps" command :-(
                ;;
            *)
                echo "$_cmd"
                ;;
            esac
        fi
    fi
done
Run Code Online (Sandbox Code Playgroud)

运行它:

$ tail -f /var/log/messages | ./whatpipe1.sh
tail -f /var/log/messages
^C
Run Code Online (Sandbox Code Playgroud)

请注意,它们都只在管道左侧的命令运行足够长的时间ps才能看到它时才起作用。你说你在使用它tail -f,所以我怀疑这是一个问题。

$ sleep 0 | ./whatpipe1.sh 

$ sleep 1 | ./whatpipe1.sh
sleep 1
Run Code Online (Sandbox Code Playgroud)