0

I have a simple question with you that I cannot answer myself. I have a variable below which shows a blank after printing the result.

SCRIPT:

core_out=find coretst.*
echo "$core_out"
Result: No display *blank*
#tried to echo without double quotes as well but it has the same result

btw, I'm using putty on this.

Actual objective: 1. Check core.* in /opt/datintdv/Informatica/9.6.1/server/bin --dir1 - If core.* exist and more than 1, get 1 core.* then execute strings (core.*) | grep “pmdtm(“ --script1 then store the output in a parameter if script1 did not work execute file (core filename) until all core. have been executed then store the output in a variable. - If core.* does not exist on dir1 then check /opt/datintdv/Informatica/9.6.1/tomcat/bin If core.* exist and more than 1, get 1 core.* then execute strings (core.*) | grep “pmdtm(“ --script1 then store the output in a parameter if script1 did not work execute file (core filename) until all core. have been executed then store the output in a variable. *output is separated by a new line

  1. Get all the output then concatenate it as the e-mail body.
  2. Send it to the user via e-mail. *I'm currently writing a script for this but I'm having a hard time since this is my first time to do it.

Hopefully you can help me! Thanks!

  • It looks like you are trying to get the output of a command and store it in a variable. I'm assuming bash so try: core_out=$(find coretst.*) – pacmanwa Mar 5 '18 at 14:37
2

Command substitution, which means using the result of a command for something in-line, is done with $( ... ).

In your case,

core_out=$( find coretst.* )

However, this will prompt find to look in all paths that starts with anything that coretst.* expands to. What I assume you want to do is to look in the current directory or below for files that matches the pattern coretst.*. You would do that with

core_out=$( find . -type f -name 'coretst.*' )

Quoting the coretst.* pattern stops the shell from expanding it to names in the current directory.

Then, it's a question what you would like to do with the pathnames that are returned by this. If you just want to print them, then there is no need for the variable at all:

find . -type f -name 'coretst.*'

If you would like to remove these files, then you should do so from find:

find . -type f -name 'coretst.*' -exec rm {} +

Any other operation on the files can also be performed from within find.

See the question "Why is looping over find's output bad practice?" for reasons why using the paths that find returns may be a bad habit (the answers are about looping over the result of find, which is what I presume you might want to be doing later).


From comments:

I want to search it in a current folder, and the output on this will be used on a substring and consequently will be used on this: strings ($core_out) | grep "pmdtm("

If I understand this correctly, this could be done with

find . -type f -name 'coretst.*' -exec strings {} + | grep -F 'pmdtm('

I.e., run strings on all found files and find the lines that contains the string pmdtm(.

If you want to know what file the match was found in:

find . -type f -name 'coretst.*' -exec sh -c '
    for name; do
        if strings "$name" | grep -qF "pmdtm("; then
            printf "%s\n" "$name"
        fi
    done' find-sh {} +
  • Thanks Kusalananda! Your assumption is quite right. I want to search it in a current folder, and the output on this will be used on a substring and consequently will be used on this: strings ($core_out) | grep “pmdtm(“ – Geronimo Mar 5 '18 at 14:57
  • @JeromeSabareza See updated answer. – Kusalananda Mar 5 '18 at 15:04
  • +1. BTW, strings isn't needed here - it seems it's only being used to find which files contain a particular string. Instead, you can use grep's -a option to force it to search binary files as if they were text. e.g. find . -type f -name 'coretst.*' -exec grep -alF 'pmdtm(' {} + – cas Mar 6 '18 at 3:14
  • Thanks Kunsalananda and cas, I'll try all your inputs and let you know the results. I edited my original question for a better understanding, actually that's the whole context why I'm writing script which is my first time - having a hard time though! :) – Geronimo Mar 6 '18 at 21:02
  • Hi Kunsalananda/Cas, I almost completed my script. However, some of it were working as expected and some were not (specially the nested if statements). Can you help me check my script? ( Is there away to attach my script here?) Let me know if you have any questions. Thank you in advance. Regards, Jerome P.S. I tried this one: find . -type f -name 'coretst.*' -exec sh -c ' for name; do if strings "$name" | grep -qF "pmdtm("; then printf "%s\n" "$name" fi done' find-sh {} + but it prompts/wait for an enter to generate the result. – Geronimo Apr 12 '18 at 16:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.