Find the answer to your Linux question:
Results 1 to 6 of 6
1) How do I get "find -exec" in a script to call a function also defined in that script? For example, I wanted to do something like this: Code: #!/bin/sh ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Jun 2005
    Posts
    23

    Several "find -exec" and "find | xargs" questions


    1) How do I get "find -exec" in a script to call a function also defined in that script?

    For example, I wanted to do something like this:
    Code:
    #!/bin/sh
    Function1() {
        # This function takes one argument
        # blah blah blah
    }
    find srcDir -name *.log -exec Function1 {} \;
    The thing is -exec does not recognize Function1.

    2) How to use command replacements with the backtick in "find -exec"?

    As a trivial example, I wanted to do something like this:
    Code:
    #!/bin/sh
    find srcDir -name *.log -exec echo `dirname {}` \;
    However, this does not come out right.

    3) How can I use the output from "find" at several places in "xargs"?

    For example, I wanted to do something like this:
    Code:
    #!/bin/sh
    find srcDir -name *.log | xargs python pyScript {} `dirname {}`\/python.log
    However, it seems that I cannot use {} in xargs and xargs will only append whatever is piped from find to the end of its specified command.


    Much appreciation if anyone can show me how to do the three things listed above, or alternative ways to accomplish the same objectives.

  2. #2
    Linux Guru lakerdonald's Avatar
    Join Date
    Jun 2004
    Location
    St. Petersburg, FL
    Posts
    5,035

    Re: Several "find -exec" and "find | xargs" questions

    Quote Originally Posted by thanhvn
    1) How do I get "find -exec" in a script to call a function also defined in that script?

    For example, I wanted to do something like this:
    Code:
    #!/bin/sh
    Function1() {
        # This function takes one argument
        # blah blah blah
    }
    find srcDir -name *.log -exec Function1 {} \;
    The thing is -exec does not recognize Function1.

    2) How to use command replacements with the backtick in "find -exec"?

    As a trivial example, I wanted to do something like this:
    Code:
    #!/bin/sh
    find srcDir -name *.log -exec echo `dirname {}` \;
    However, this does not come out right.

    3) How can I use the output from "find" at several places in "xargs"?

    For example, I wanted to do something like this:
    Code:
    #!/bin/sh
    find srcDir -name *.log | xargs python pyScript {} `dirname {}`\/python.log
    However, it seems that I cannot use {} in xargs and xargs will only append whatever is piped from find to the end of its specified command.


    Much appreciation if anyone can show me how to do the three things listed above, or alternative ways to accomplish the same objectives.
    1. The -exec flag on find will not read in functions from the current environment
    3. xargs is NOT find, so it will NOT recognize find-specific things such as {}.

  3. #3
    Just Joined!
    Join Date
    Jun 2005
    Posts
    23
    Don, you just told me what I already learned. What's more helpful is telling me what are the alternative ways to accomplish the same objectives.

  4. $spacer_open
    $spacer_close
  5. #4
    scm
    scm is offline
    Linux Engineer
    Join Date
    Feb 2005
    Posts
    1,044

    Re: Several "find -exec" and "find | xargs" questions

    Quote Originally Posted by thanhvn
    2) How to use command replacements with the backtick in "find -exec"?

    As a trivial example, I wanted to do something like this:
    Code:
    #!/bin/sh
    find srcDir -name *.log -exec echo `dirname {}` \;
    However, this does not come out right.
    You have to realise that the backticked expression is evaluated by the shell before the find command is run. To list the directories contain .log files something like the following should suffice. Note the quoting round the *.log to guard against having any .log files in the current directory.
    Code:
    find srcDir -name "*.log" -print | sed 's|/[^/]*$||'

  6. #5
    Linux Guru lakerdonald's Avatar
    Join Date
    Jun 2004
    Location
    St. Petersburg, FL
    Posts
    5,035
    Quote Originally Posted by thanhvn
    Don, you just told me what I already learned. What's more helpful is telling me what are the alternative ways to accomplish the same objectives.
    My name is not don, and I was simply trying to keep you from wasting any more time investigating those alternatives. Sorry for helping.

  7. #6
    Just Joined!
    Join Date
    Jun 2005
    Posts
    23
    Alright, after obtaining help from several people, I finally got it figured out: use a for loop, duh!

    1) Either I have to pull Function1 out into another script or use a for loop:
    Code:
    #!/bin/sh
    for FILE in `find srcDir -name "*.log" -print 2> /dev/null`
    do
        # implement Function1 here
    done
    2) Once again, use a for loop:
    Code:
    #!/bin/sh
    for FILE in `find srcDir -name "*.log" -print 2> /dev/null`
    do
        echo `dirname $FILE`
    done
    3) What else, the for loop:
    Code:
    #!/bin/sh 
    for FILE in `find srcDir -name "*.log" -print 2> /dev/null`
    do
        python pyScript $FILE `dirname $FILE`/python.log
    done
    That's what I meant by alternative ways to accomplish the same objectives. Thanks to everyone who helped me.


    P.S. lakerdonald, I didn't meant anything by calling you don. I was in a hurry. My apologies.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •