0

I can't work out what I'm doing wrong. What I actually want to do, is run this command in my bash script, and grab the value back:

tail -1 com-html.log | grep 'Min: ,'

This works when manually running:

tail -1 com-html.log | grep 'Min: ,'
Min: , MAX:

Yet I can't get it to work in a bash script: (I've simplified it, in case the pipe bit was an issue)

#!/bin/bash

test = $(tail -1 com-html.log)

echo "test: $test";

What am I doing wrong? Bash is pretty new to me - so please be gentle =)

As per the suggestions below - I've even tried:

    test=$(tail -1 com-html.log | grep 'Min: ,');
    echo "test: $test"

    if [ -z "$test"]
    then
        echo "hmmm test is empty"
    fi

But it always just shows:

hmmm test is empty

I must be missing something silly

I've tried a few methods:

    test=$(tail -1 com-html.log | grep 'Min: ,')
    echo "test: $test";

    test=$(tail com-html.log | grep 'Min: ,')
    echo "test: $test";

and even the simplest just to see if I get any value:

    test=$(tail -1 com-html.log)
    echo "test: $test";

All of them have $test as empty

UPDATE: OMG this is why you shouldn't do too much coding for one day. You miss silly stuff! So this script is keeping an eye on load for the server, and also RAM. If it detects either are too high, it'll kill the script and wait for it to go back down before restarting. So I have stuff like:

    if [ -n "$(ps -ef | grep -v grep | grep 'get-html-all-domains.cgi')" ];
    then : ;
    else
        echo "nohup perl get-html-all-domains.cgi $tld new $forks $per_page > com-html.log"
        nohup perl get-html-all-domains.cgi $tld new $forks $per_page > com-html.log &  # restart it...
    fi

Now that works - but when it triggers, the output into com-html.log isn't instantaneous (as it has to get the value from the DB and then decide there is nothing to run). So to get around it, I changed the line:

nohup perl get-html-all-domains.cgi $tld new $forks $per_page > com-html.log &

to:

nohup perl get-html-all-domains.cgi $tld new $forks $per_page >> com-html.log &

(notice >> instead of just > now, so it's appending and not overwriting it completely)

Then this at the end to check if the last line is what we are looking for:

    if [ -n "$(tail -2 com-html.log | grep 'Min: ,')" ]
    then
        echo "2: Seems to be at end... lets stop :)"
        echo -ne '\007'
        exit 0

    fi
Andrew Newby
  • 4,941
  • 6
  • 40
  • 81
  • `var=$(tail -1 com-html.log | grep 'Min: ,')` – anubhava Feb 17 '21 at 17:02
  • Does this answer your question? [Why does a space in a variable assignment give an error in Bash?](https://stackoverflow.com/questions/41748466/why-does-a-space-in-a-variable-assignment-give-an-error-in-bash) – 0stone0 Feb 17 '21 at 17:04
  • @anubhava thanks - that doesn't seem to grab the variable still? `test=$(tail -1 com-html.log | grep 'Min: ,') echo "test: $test"` , unless I'm missing something else? I don't get any errors btw - just no value in $test :) – Andrew Newby Feb 17 '21 at 17:14
  • @0stone0 thanks but not sure thats my issue. I get no errors - it just doesn't seem to store the output into `$test` – Andrew Newby Feb 17 '21 at 17:22
  • 2
    Ahh, your example code shows `test = $`, there shouldn't be any spaced between those, just saying ;) Could you please add an example of `com-html.log`? – 0stone0 Feb 17 '21 at 17:24
  • @0stone0 sure - it literally just has `Min: , MAX: ` as the value. I've even tried it with `tail -2` in case the newline was causing an issue at the end (again, both of those versions run fine when running just in the terminal and not in the bash script) – Andrew Newby Feb 17 '21 at 17:28

1 Answers1

1

tail -1 only shows the last line of com-html.log.

So if the grep string (Min: ,) isn't on the last line, test will be empty.

Last line to variable

test=$(tail -1 com-html.log)
echo "test: $test";

Any line that contains Min: ,

test=$(grep 'Min: ,' < test.log)
echo "test: $test";

Last line; if containing Min: , Try it online!

test=$(tail -1 com-html.log | grep 'Min: ,')
echo "test: $test";
0stone0
  • 34,288
  • 4
  • 39
  • 64
  • Thanks - but that is what I already have :) I want to do the grep, as I only want a match if that is the last line (this is to detect if the script has got to the end, and if so stop it trying to re-start). Even if I remove the `-1` from the tail, it still comes up empty – Andrew Newby Feb 17 '21 at 17:24
  • Please take a look at the 'try it online' script, it's working as expected. – 0stone0 Feb 17 '21 at 17:26
  • hmmm I must be missing something silly then. I'll have another look tomorrow with a fresh set of eyes. All logic says it *should* be working. BTW not sure if it makes a different - but I'm running the script with : `bash foo.sh` – Andrew Newby Feb 17 '21 at 17:31
  • A simple test script works with your example using the com-html.log: `#!/bin/bash test=$(tail -1 com-html.log | grep 'Min: ,') echo "FOO: $test"` .. yet it doesn't work in my other script. I must be missing something stupid – Andrew Newby Feb 17 '21 at 17:41
  • Ahhh man - I got it! The issue was that the nohup further up the code was starting the script again, and wiping out the .log file! So when it got to the line where it was checking, it didn't exist (as it hadn't have enough time to run and output the results). So what I've done is change it to >> instead of > for the log file (more details in my original post, in case it helps someone!) – Andrew Newby Feb 18 '21 at 06:58