Andrew Newby
Andrew Newby

Reputation: 5197

Grab value of tail of file in bash script and store as variable

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

Upvotes: 1

Views: 2566

Answers (1)

0stone0
0stone0

Reputation: 43934

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";

Upvotes: 1

Related Questions