简体   繁体   English

如何在 Linux 中合并两个脚本,使用第二个输出之一

[英]How to merge two scripts in Linux, using one of the output in the second

I'm a total noob writing scripts in Linux.我是一个在 Linux 中编写脚本的菜鸟。 I have to work on QNAP NAS.我必须在 QNAP NAS 上工作。 The problem is that sometimes files remains open.问题是有时文件保持打开状态。 I have to check who opened the file.我必须检查谁打开了文件。 First I need to get list of open files.首先,我需要获取打开文件的列表。 Every open file has an associated PID.每个打开的文件都有一个关联的 PID。 Then I have to get domain user names by PIDs.然后我必须通过 PID 获取域用户名。

I have two scripts.我有两个脚本。 The first lists PIDs of open files.第一个列出了打开文件的 PID。 I pass one parameter, the name of file I search for.我传递了一个参数,即我搜索的文件名。 It can generate more lines with PIDs.它可以生成更多带有 PID 的行。

The second script lists the associated domain user name for a given the PID.第二个脚本列出给定 PID 的关联域用户名。 I have to run the second script for every PIDs listed by the first script.我必须为第一个脚本列出的每个 PID 运行第二个脚本。

I don't know how to handle in the second script the multi line output from the frist script.我不知道如何在第二个脚本中处理第一个脚本的多行输出。

Thanks for advice.谢谢你的建议。

Mark标记

first script (smbopenfiles):第一个脚本(smbopenfiles):

/usr/local/samba/bin/smbstatus -v|grep $1|awk '{print $1}'

second script (smbwhois):第二个脚本(smbwhois):

/usr/local/samba/bin/smbstatus -v|grep $1|awk '{print $3}'|grep -v -e DENY|grep -v -e domain

I'm making a couple assumptions here based on your scripts:我在这里根据您的脚本做出一些假设:

1.) grep $1 is intended identify/isolate the first column of the output. 1.) grep $1旨在识别/隔离输出的第一列。 I think this is possibly a misunderstanding of how grep works on your part since the awk '{print $1}' part at the end of the pipeline should do what you want in this case.我认为这可能是对 grep 对您的工作方式的误解,因为管道末尾的awk '{print $1}'部分应该在这种情况下执行您想要的操作。

2.) Since the -v switch to grep returns lines which do not match the specified pattern, in the second script, you intend to display all lines which do not contain "DENY" or "domain" in the third column. 2.) 由于-v切换到 grep 返回与指定模式匹配的行,因此在第二个脚本中,您打算在第三列中显示所有不包含“DENY”或“domain”的行。

If these assumptions are correct, you can do all of this in one line with awk:如果这些假设是正确的,您可以使用 awk 在一行中完成所有这些:

smbstatus -v | awk '($1 ~ /[0-9]+/) && ($3 !~ /DENY/) && ($3 !~ /domain/) {print $1"\t"$2"\t"$3}'

This should cause awk to only give you lines where the first column is a number (presumably your PID), and the third column does not contain DENY or domain.这应该会导致 awk 只为您提供第一列是数字(大概是您的 PID)的行,而第三列不包含 DENY 或域。 Columns 1, 2, and 3 from the lines matching these criteria are written separated by tabs.与这些条件匹配的行中的第 1、2 和 3 列用制表符分隔。

Ideally you want to do something like the command above to avoid running smbstatus twice, but if I've guessed something wrong and there is some real need to run another command based on the list of pids returned by the first, you can do something like this:理想情况下,您希望执行类似上述命令的操作以避免两次运行 smbstatus,但是如果我猜错了并且确实需要根据第一个返回的 pid 列表运行另一个命令,您可以执行以下操作这个:

pids=`smbstatus -v | awk '($1 ~ /[0-9]+/) {print $1}'`
for pid in $pids
do
     echo $pid
done

You would replace echo $pid with come command which makes use of each pid we found.您可以将echo $pid替换echo $pid come 命令,该命令使用我们找到的每个 pid。

This is the final script.这是最终的剧本。

if [ "$1" = "" ] || [ "$1" = "-?" ]; then
        echo Enter file or folder name to search for...
else
        pids=`/usr/local/samba/bin/smbstatus -v|grep $1 |grep DENY_WRITE|awk '($1 ~ /[0-9]+/) {print $1}'`
        oldid=0
        for pid in $pids
        do
                if [ $oldid != $pid ]; then
                        echo $pid
                        /usr/local/samba/bin/smbstatus -v|grep $pid|awk '{print $3}'|grep -v -e DENY|grep -v -e domain|tail -n 1
                        /usr/local/samba/bin/smbstatus -v|grep $pid|grep DENY_WRITE
                        echo
                        oldid=$pid
                fi
        done
fi

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM