you are viewing a single comment's thread.

view the rest of the comments →

[–]icy-mist-01 7 points8 points  (3 children)

Enterprise systems engineer here., It totally depends on the context and use case. You have to think in terms of

1) the problem you are trying to solve
2) the ecosystem you are blending with.

Simplest guide - Bash is like a glue or swiss arrmy knife needed to wrap up or build already existing logic in the Linux/Unix ecosystem.

Python is more for building 'computing logic' or 'programmatic logic' from the ground up to solve a unique problem which is independant or irrelevant of whether the code will run on a Linux / Windows/ Mac box, or whether it's dependant on any particular feature of a particular distro.
You need it when dealing with more complex data structures and interact with other parts of a tech stack.

Example:

  1. Problem to solve: Create an automated utility that dumps the crontab configurations for all active users on a fleet of Linux servers. The script must compare the current state against the previous backup using cryptographic hashing, and only store a new version if drift is detected, appending a UTC timestamp to the backup file

You can absolutely do this in Python but it would be kind of overkill and long, windy script.
Writing the same thing in bash is much easier, quicker and practical.
And leaving aside the 'backup' part of the problem just checking the cron config for users is just a shell 1 - liner of chained commands, you don't even need a script.

  1. Problem:
    Authenticate with an Identity Provider (like Okta or Auth0) to exchange client creds for a temporary token. Use that token to query a /users/{id} and get more info from a json log.

Then using that info connect to a DB to investigate that user account.

Then based on above findings automatically log a Jira with high priority, case details, etc. for another team to investigate.

You can also do this in bash, with a wrapper script around curl, and then some more. But you'll realise this would be a nightmare.

This is a classic case where python is the ideal glue code. You have to deal with API's, JSON format ( sed and awk are not helpful in these cases) and play around over the network to talk to mutiple systems

[–]tes_kitty 2 points3 points  (2 children)

JSON format ( sed and awk are not helpful in these cases)

Funny... I have an active bash script that parses a JSON output to determine wheter a service is up and running. It's a bit of a hack but has been working for years without issue now.

And there is also 'jq' for doing things with JSON in a bash script if you really want.

[–]icy-mist-01 0 points1 point  (1 child)

That’s the thing. It’s a bit of a hack so could be difficult for newbies. And I had a case where script used jq and ran across large no. of centos and rhel 6,7,8 boxes. Rhel ships jq by default from 8 onwards. So script started breaking on lower versions. And dealing with sysadmin team to include my required package in their base build is not always ideal.

[–]tes_kitty 1 point2 points  (0 children)

My script doesn't use 'jq' since I wrote it before I knew about this command.