journalctl --after-cursor is not working well with shell script

1.2k views Asked by At

I am trying to get the logs from journalctl after a specified time with the use of cursor option. Below is code in script file.

value=$( journalctl -n 0 --show-cursor | tail -1 | cut -f2- -d: | sed 's/ /"/;s/$/"/')
echo "$value"
sleep 20
echo "journalctl --after-cursor=$value"
journalctl --after-cursor=$value

The ouput of this script file is

"s=3057f92d5b3e4afdb5eb91c22e880074;i=1f646;b=0bc7e6e9f16f4847b2d50e0a0dd31023;m=a10d4c4d1;
t=5bba8ac2477ae;x=1cc1645fed6ffc79"
journalctl --after-cursor="s=3057f92d5b3e4afdb5eb91c22e880074;i=1f646;
b=0bc7e6e9f16f4847b2d50e0a0dd31023;m=a10d4c4d1;t=5bba8ac2477ae;x=1cc1645fed6ffc79"
Failed to seek to cursor: Invalid argument

As we can see above the journalctl --after-cursor results in "Failed to seek cursor error".

However if the same is executed in command line terminal, the --after-cursor gives the output.

Is there something needed to be done before calling journalctl with after-cursor option in shell script?

1

There are 1 answers

0
Ken Jenney On

Bash is very finicky about interpolation. You need to trim the double-quotes around $value and then quote it later on:

value=$( journalctl -n 0 --show-cursor | tail -1 | cut -f2- -d: | sed 's/ /"/;s/$/"/') | tr -d '"')
echo "$value"
sleep 20
echo "journalctl --after-cursor=$value"
journalctl --after-cursor="$value"

I've confirmed that this works by running the above and I've also used this same method on another script which uses cursors.