You are not logged in.
Pages: 1
Hi guys,
I want to create a simple bash script that would execute a simple checkupdates and if the result is greater than zero, push a notification or open terminal with pacman -Syu. This is what I have so far but my bash skills are close to non existent:
#!/bin/bash
if eval "(/usr/bin/checkupdates|wc -l)" -gt 0;
then
COMMAND="xfce4-terminal -e 'sh -c \"sudo pacman -Syu ; echo Done - Press enter to exit; read\" '"
else
printf '%s\n' "${PWD##*/}"
fi
I am getting (eval):1: parse error near `-gt'. Obviously that's not how the comparison should be done.
Can you help me out here?:)
Thanks,
Offline
It appears you are mistaking `eval` for `test`. `-gt` is an argument used by the `test` command to compare two numerical values, whereas `eval` simply executes a command that is composed by its arguments (but to be honest, I don't know in detail how it works for certain corner cases like these).
Also, how would this script be invoked? Automatically (by some background process/daemon) or manually?
If it's the former, I would strongly advise against that (sometimes updates may require human intervention, or other kind of fiddling). And in both cases, I don't see the point in running `checkupdates` first if it's followed by an immediate `pacman -Syu` anyway.
--edit If you use `test`, you probably want command substitution for the `checkupdates | ...` part:
if test "$(checkupdates | wc -l)" != 0; then ...
Last edited by ayekat (2017-12-31 13:50:38)
Offline
You should use `test` or one of it's variants and a subshell rather than eval:
if [ $(checkupdates | wc -l) -gt 0 ]; then
xfce4-terminal -e 'sh -c ...
else
printf "what's this for?"
fi
Note, though that you don't actually need the line count, you just need to know if it returned anything at all, so it can be even simpler:
if [ $(checkupdates) ]; then
Also note that I removed the COMMAND from the `then` clause. I'm not sure what it's supposed to be for, but as is it prevents xcfe4-terminal from actually running (and makes quoting more complex. Don't you just want to run the command there?
I'm also not sure what the `else` clauses is intended to do.
If you don't need the else clause, then this could just be a single line as follows:
[ $(checkupdates) ] && xfce4-terminal ...
I'd love to get rid of the test brackets and the subshell too, but it seems checkupdates' return value is the same regarless of whether if found packages to update.
---
I don't see the point in running `checkupdates` first if it's followed by an immediate `pacman -Syu` anyway.
It's only followed by the pacman command if there is something to update. Without that conditional check, a new terminal would open and run pacman every single time this script runs. That would be a bit ridiculous especially if this script is run on a timer.
Though I will second the concern about unattended updates. However, ayekat, note that it is not running in the background - it is opening a terminal to run pacman and leaving it there until the user gets a chance to see it.
Last edited by Trilby (2017-12-31 14:10:19)
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Offline
However, ayekat, note that it is not running in the background - it is opening a terminal to run pacman and leaving it there until the user gets a chance to see it.
Hmm, that's true.
I guess it's just a matter of taste then; I prefer to decide myself whether I want to run a system upgrade, after inspecting the output of `checkupdates` — were I to keep the "spirit" of this script, I would probably open the terminal window and present the user with the list of packages first (to give the user the chance to say: "No, wait, I don't want these updates right now").
Last edited by ayekat (2017-12-31 14:18:18)
Offline
Agreed on all points. As for the matter of taste, I've always found it a bit funny that "checkupdates" is more key strokes than "pacman -Syu" But then I always have a terminal open in front of me - often it's the only thing open.
If I read the OP right, the actual command run might still be getting polished (maybe that's why it's just assigned to a variable for now). They are also considering pushing a notification: that sounds like a good idea to me. And that could be as simple as:
[ $(checkupdates) ] && notify-send -flags "You've got packages"
I don't actually use notify-send, so I don't know what the flags would be, but that's the basic idea.
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Offline
Thanks guys.
Yes, the variables assigned and the else statement are there because the script was very much work in progress. The COMMAND variable would later be called in the code and the else statement was there to let me know the code made it there. Based on your feedback so far, the current version looks as follows:
[$(checkupdates; cower -u 2>/dev/null | wc -l)] && xfce4-terminal -e 'sh -c \"sudo pacman -Syu ; echo Done - Press enter to exit; read\" '
The basic idea is to run a cron job every say 30 minutes. I wanted to push a notification instead with an update button that would call the xfce part, but notify-send does not support actions and I have no clue what else could be used so I settled on the terminal piece.
Thanks,
Offline
Note that there must be a space after the opening [ and before the closing ]. Also, you will need the -gt 0 clause if you are actually counting lines as `wc -l` will always produce output even if the output is zero.
But I'm not sure why you'd include aur updates being avaiable in that condition as this will not run pacman -Syu if there are only aur updates available which doesn't make sense. Perhaps you want two different checks:
[ $(checkupdates) ] && xfce4-terminal -e # ... pacman stuff
[ $(cower -u 2>/dev/null | wc -l) -gt 0 ] && xfce4-terminal -e # ... whatever you'd want to do for AUR updates
EDIT: but I don't know why the error redirection and line counting would be needed for cower, it will also produce no ouput if there are no updates available, so it's line could be:
[ $(cower -u) ] && xfce4-terminal # ...
Last edited by Trilby (2017-12-31 15:14:22)
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Offline
Well, fortunately, as sudo is usually not configured to allow pacman to be run without a password, this will run no risk of partial updates even if the window is killed or ignored. On the other hand, sudo will most likely time out before the user has a chance to do anything, if they don't happen to be at the computer at exactly the right time.
Both problems have the same solution -- do a `read -p "Are you ready to start the update?"` before sudo as well. For that matter, use `read -p "Done - Press enter to exit"` rather than using a separate echo command which is kind of messy.
Managing AUR repos The Right Way -- aurpublish (now a standalone tool)
Offline
Of course a problem remains, if the computer is unattended for a few hours, you might come back to a dozen terminal windows each asking if you are ready to start the update! (or just waiting to be closed after sudo timed out).
Short of finding a notify-send-like tool that has buttons/actions, you could just split the problem in two. Something like the following two bits:
## script to run on timer/cron job
if [ $(checkupdates) ]; then
touch /tmp/updates_available
notify-send "you have updates"
else
rm -f /tmp/updates_available
fi
## in your shellrc
if [ -f /tmp/updates_available ]; then
printf "Running available updates"
sudo pacman -Syu
rm -f /tmp/updates_available
fi
This way you'll get notifications through notify-send, then all you need to do is open a terminal to complete the update.
You could also simplify this a great deal and get rid of the temp file, just do the original checkupdates triggering just a notification, then in your shellrc file run checkupdates again and trigger the update as needed. The only downside of this would be that checkupdates would run every single time you open a terminal and it can take a couple seconds. Depending on how frequently/rarely you use the terminal, this may not matter.
Last edited by Trilby (2017-12-31 15:29:24)
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Offline
Pages: 1