Detect Linux Runlevel

Michael Torrie torriem at gmail.com
Mon Dec 5 22:46:22 EST 2016


On 12/05/2016 08:27 PM, Wildman via Python-list wrote:
> On Mon, 05 Dec 2016 18:25:58 -0700, Michael Torrie wrote:
> 
>> I think Python is a good choice for such a utility, but I agree it is
>> much better to rely on these external utilities as children to do the
>> platform-dependent work, rather than try to re-implement everything in
>> Python.  A long time ago I wrote a simple wrapper to Popen that would
>> run a command and return the standard out and standard error to me.
> 
> My rational is that all Linux distros are not created equal.
> One comes with one certain set of utilities and another can
> have different ones.  I can't always depend on a given
> utility being there.  And there is not way to know the
> names of same utility across all distros.  This is especially
> a problem when comparing .rpm with .deb based distros.

Well this is a problem regardless of which scripting language you choose
and the solutions will be the same.

> In cases where I have to use an external program, I mean in
> cases where there is no apparent Python solution, I check
> for the existence of that program and just skip over that
> section of the code if it is not found.

Sure. That's probably reasonable.

> 
> BTW here is a link for the Bash script I mentioned in case
> you would like to take a look at it.  The guy who wrote
> it had only been learning Bash for a few months.  Not bad.
> I have tried to tweak an interest in him for Python but
> he sticks with Bash.  He says that is the best language
> for programming on Linux and he is not interested in GUI
> programming.
> 
> https://github.com/marek-novotny/linfo

That all depends on what he's programming. For anything a user interacts
with, Bash is a pretty poor tool.  But Bash is really good at
shell-scripting system tasks.  It treats external commands as
first-class entities, and process control and I/O piping is integrated
into the syntax of the language.  On the other hand, Python is a good
language but it's not particularly well suited to shell scripting
(wasn't designed for that purpose), though it does have some facilities
like generators that make certain forms of system programming really
slick. In short they overlap in purpose, but they aren't good at the
same things.  However when a bash script gets too long, I'll often
switch to Python, using my run wrapper and generator filters to process
the output of external commands.

Personally for a script of this type, I'd probably stick with Bash
myself. Which by the way is what inxi is written in.



More information about the Python-list mailing list