Bash related issue, not sure where to post it

I couldn’t see a specific section for bash/scripting etc so not sure where to post the question.

I’m trying to make an alias called lastup that will tell me the last time the system was updated.

This is what I tried:

alias lastup='awk 'END{sub(/\[/,""); print $1}' /var/log/pacman.log'

And this is the error I’m getting when I open the terminal:

bash: /home/redacted/.bashrc: line 33: syntax error near unexpected token `('
bash: /home/redacted/.bashrc: line 33: `alias lastup='awk 'END{sub(/\[/,""); print $1}' /var/log/pacman.log''

I’m assuming the quotes within quotes is the issue but I don’t know enough bashfu to rephrase what I’m trying to achieve. Any bash wizards around to enlighten a noobie? :upside_down_face:

Try wrapping the whole alias in double quotes.

I’m also not completely sure what the line is doing, but if you need to add quotes inside of quotes you need to switch from single to double quotes, or you will just be ending the first quote instead of starting a new one.

' ... " ... " ... '
2 Likes

Normally, you can’t nest the same type of quotes inside each other.

Use a string, like this:

 alias lastup=$'awk \'END{sub(/\[/,""); print $1}\' /var/log/pacman.log'

Also worth noting that the command doesn’t tell you the last time you updated but the last time you did anything with pacman.

1 Like

As you have realized, inserting complicated (not a simple command with a couple of words/arguments) code in .bashrc is messing your system.
If you want to add an alias to something complicated, create a bash script, and use it as a simple command for an alias. :wink:

When the syntax is to complicated for an alias, I use a function :

lastup () {
    awk 'END{sub(/\[/,""); print $1}' /var/log/pacman.log
}
2 Likes

Another solution:

lastup() { tail -n1 /var/log/pacman.log | awk '{print $1}' ; }
1 Like

Looks cleaner. I’m still at the monkey-see monkey-do phase so that would never have occurred to me.

Have a rainbow. :rainbow:

This one is the most aesthetically pleasing response. :mage:

1 Like

Depending on the exact use you will make of this, you can specify the type of operation (from the pacman log) that interests you. Just saying (in case you wondering about updates only :grin: )

1 Like

(Likely) the two shortest solutions to see the latest update time:

cat /var/log/pacman.log        # slow
tail -n1 /var/log/pacman.log   # fast
1 Like

I have endeavour os installed on some old machines and laptops trying out different setups and for learning, and if I get distracted and don’t update one for a while, I want a quick way to check the last time the system was updated so I know if to check for issues before doing an update etc.

As mentioned here that command only will produce the date of the last pacman transaction. It could be about updates but also installing one/some package/s or remove one/some package/s.

1 Like

The OPs original solution was probably more efficient because it is a single awk call instead of running a program to get the last line and then piping it to awk.

2 Likes
lastup1 () { awk 'END{sub(/\[/,""); print $1}' /var/log/pacman.log; }

$ time lastup1

real    0m0,014s
user    0m0,010s
sys     0m0,003s

VS.

lastup2() { tail -n1 /var/log/pacman.log | awk '{print $1}' ; }

$ time lastup2

real    0m0,003s
user    0m0,001s
sys     0m0,002s
1 Like
grep "system upgrade" /var/log/pacman.log | tail -1 
[2023-04-30T10:31:56+0200] [PACMAN] starting full system upgrade
1 Like

They aren’t currently equivalent because of the substitution.

If you make them equivalent they are basically the same.

$ time lastup1
[2023-04-28T13:59:18-0500]

real    0m0.007s
user    0m0.007s
sys     0m0.000s
$ time lastup2
[2023-04-28T13:59:18-0500]

real    0m0.006s
user    0m0.007s
sys     0m0.000s

This is exactly what I was looking for thanks!

Just out of curiosity what is the tail -1 part?

It takes the last line. Without that, it will return all the lines in the log with upgrade in them.

1 Like

Same as tail -n 1, I just didn’t have to type n+space. :grinning:

1 Like

Gotcha. Makes sense.