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!

Geronimo
  • 3
  • 3
  • 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 05 '18 at 14:37

1 Answers1

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 {} +
Kusalananda
  • 333,661
  • 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 05 '18 at 14:57
  • @JeromeSabareza See updated answer. – Kusalananda Mar 05 '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 06 '18 at 03: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 06 '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
  • @JeromeSabareza I would suggest making a new question about this. – Kusalananda Apr 12 '18 at 16:22
  • @Kusalananda Well, I'm unable to test my nested if statements, even though I made it in a single line, just to check whether it is working. Would you mind taking a look here https://pastebin.com/J8jQ9wMk ? – Geronimo Apr 12 '18 at 16:33
  • @JeromeSabareza There are too many issues in that script for me to comment on all of them in this comment section. Why, for example, do you pass the result through awk to insert empty lines between all output? Why do you save the result of find in variables to start with? Most of the if statements have syntax errors (need space after [ and before ] and there are many missing then). Please use https://www.shellcheck.net/ – Kusalananda Apr 12 '18 at 16:38
  • Thanks for your inputs. I'll try to address those issues via shellcheck.net (thank you for this). For the awk, I added to make the output convenient to read, because it has multiple outputs. The variables were used on the email body and it's easier for me to construct the if statements on the single variable. Any suggestions for this would be greatly appreciated, or do you suggest it to be posted on another question? – Geronimo Apr 13 '18 at 04:31
  • @Kusalananda, here's my new post – Geronimo Apr 18 '18 at 15:01