简体   繁体   中英

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

I'm a total noob writing scripts in Linux. I have to work on 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. Then I have to get domain user names by PIDs.

I have two scripts. The first lists PIDs of open files. I pass one parameter, the name of file I search for. It can generate more lines with PIDs.

The second script lists the associated domain user name for a given the PID. I have to run the second script for every PIDs listed by the first script.

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):

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

second script (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. 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.

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.

If these assumptions are correct, you can do all of this in one line with 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. Columns 1, 2, and 3 from the lines matching these criteria are written separated by tabs.

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:

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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