Battery Powered Project

Dunno about that: awk takes a bit of getting used to because its a specialised tool for extracting data from text files: I suggested using it in this case because its brilliant for tasks like scanning log files for a set of error messages and doing appropriate stuff for each one it spots. As you've seen, the code needed to recognise 'battery low' warnings in, say, /var/log/messages and issue a 'shutdown -h now' command if one is found would be a trivially small program because it automatically reads lines and splits them into words before using a regex to trigger actions on any lines that the regex matches: about all you need to write is the regexes and the code that each of them triggers.

By comparison C, Java, Perl or Python, ... are all general purpose programming languages and so you need to code the file reading loop and (probably) the scan routine that recognises interesting lines in the file as well as the code to do something useful when a line is recognised.

IOW, to spot a 'battery low' message in the /var/log/messages logfile and issue a STOP command the Raspbian would be a one liner in awk:

awk -- '/battery low/ { system("shutdown -h now") }' /var/log/messages

as compared with at least 10-20 lines in any of the other languages I mentioned - and they'll take longer to write simply because the loop and the line parser will need to be coded and debugged.

The above, of course, is assuming that you don't want to simply build the shutdown command into the program that's watching battery voltage

A lot of programmers would do it that way while others, of which I'm one, like to keep different activities in single purpose programs - and, though its unlikely in this particular case, would keep them separate because you might someday need to let another program trigger the boojum as well as the one you're designing right now.

Dunno. These days I bet more people would try to write it in Python or Javascript than would use Perl.

-- Martin | martin at Gregorie | gregorie dot org

Reply to
Martin Gregorie
Loading thread data ...

I've just checked /etc/alternatives/awk on my bakers dozen Pi's, and the two running Raspbian with the Mate desktop are gawk, all the others running Raspbian Pixel and Raspbian Lite are using mawk, so it would seem mawk is the default on standard installations.

---druck

Reply to
druck

tail -f log | perl -ne 'system('shutdown','-h now') if /battery low/'

I would prefer an explicit observer mechanism, bundled with journalctl, but clearly I'm a MS fanboi.

It's not the behaviour of mawk I object to, per se, but the fact that awk can change behaviour so easily with system config. Really speaking, I've only been willing to touch Linux in the last 5-6 years (after a 20 year Unix hiatus) because Vagrant and then Docker allowed me to create a reliable environment.

Reply to
Pancho

Huh. Thanks for checking. Perhaps I am the "victim" of an automated switch from mawk to gawk after installing it as a dependency from other things I always install.

Reply to
A. Dumas

I think that is the case, as I don't recall ever deliberately installing gawk on those two machine.

---druck

Reply to
druck

This type of charger will work PROVIDED the battery doesn't run so low that it's reluctant to take a recharge. In that case you need a REAL LiIon charger (what you describe above doesn't even come close) that detects this condition and charges at a very low rate until the voltage rises to the point that the battery will accept charge.

Simple chargers like you describe above work for cell phones because the logic that protects the battery is in the cell phone.

--
Jim H
Reply to
Jim H

Could be. I have two Pis, one is running Raspbian 8 and /etc/alternatives/awk points to mawk. The other is running Raspbian 10 and /etc/alternatives/awk points to gawk. I don't think I have installed gawk on the latter but I don't really remember for sure.

By the way, since I haven't seen this mentioned, Debian provides a tool to manage those symlinks in /etc/alternatives, it's called update-alternatives. IMO it has an obscure syntax and is hard to use but I'd say it's still better than manually modifying the symlinks.

Reply to
Anssi Saari

Yes, update-alternatives is an exercise in being as arcane as possible I'm sure! :-)

I think, in general, if you explicitly install gawk or mawk then they will set themselves as the preferred (by update-alternatives) version of awk.

--
Chris Green
Reply to
Chris Green

I think you'll find that all Linux distros (and the remaining Unixen) have a similar tool, though the name may differ between distros: Fedora calls the tool 'alternatives'.

As always, the apropos tool is your friend here: "apropos alternate" finds 'alternatives' on my laptop, a Fedora system, and 'update_alternatives' on my RPi.

--
--   
Martin    | martin at 
Gregorie  | gregorie dot org
Reply to
Martin Gregorie

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.