1

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
2
  • I have no idea what you tried to do, should focus the question more. But if you want to execute script_1 and then use its output as the input for script_2, as the title suggest, I recommend reading about piping in linux. For instance, ./script_1 | ./script_2. Commented Apr 2, 2020 at 8:39
  • 3
    I guess you want ./smbwhois $(./smbopenfiles Szabads) but your question could do with improving. Commented Apr 2, 2020 at 9:20

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

terafl0ps, Thanks. The second part helped a lot. I could write the whole script.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.