Advice needed for moving away from shell scripting
I'm looking to sink some time into learning ***something*** that could replace shell (bash) scripts effectively. My main gripe with the shell is that the syntax is not easy to remember/work with. From time to time the need arises to write non-trivial shell scripts, let's say 100-200 lines, and the lessons learned while writing one script are in good part forgotten several months later when they are called upon. As a result I look for answers online and many times I just copy&paste solutions provided in forums, and while they work, it drives me nuts that I don't understand what every character there does and it makes debugging a nightmare.
Am I programming impaired for finding linux shells (bash in particular) hard to wrap my mind around? Basic certainly didn't seem so cryptic 20-some years ago when I started using computers. Granted, the implementations of Basic around that time cannot be compared to today's languages, but still, the paradigms were clear and easy to remember. Those being said, would you think python could replace bash for medium-sized scripts in linux? From what I can tell, it's syntax is simpler and there aren't 100 different ways of doing something. If not, would you think another scripting language is better suited for the job? |
Quote:
|
Quote:
|
We have many tasks at the "day job" that do things suh as update databases, etc.
While these probably COULD be done with bash scripts nearly all are done with PHP. |
Quote:
|
Quote:
|
Thank you TenTenths. I'll add PHP to the list of candidates.
|
I am probably like many of the others here who believe the write tool for the job :)
I must say I personally find it intriguing that you find bash so confusing as most of the more full featured programming languages such as Python, Perl, PHP or Ruby have such a depth to them that some times this is what makes them a little tougher for new people to learn. I can also tell you that if you use the same method as you have with bash: Quote:
As with all languages (including bash and spoken for that matter) if your practice does not involve you writing / creating something yourself then you are unlikely to ever fully comprehend the language. As to a recommendation, I would possibly suggest Perl. Whilst I do like Python the first gripe would be do you learn 2.x or 3.x. The other consideration is also to what is available on the machine(s) you will be performing the tasks on. Lastly, not sure if this will help your bash woes at all, but the following link I find invaluable for not only correcting some bad habits but also to be a good source of explanation: http://mywiki.wooledge.org/TitleIndex |
Quote:
Furthermore, one of the advantages that Python has over its competition is its extremely well designed and comprehensive standard library. "Batteries included" is one of its marketing slogans. Quote:
|
Quote:
I'll try to explain in a little more detail what I find difficult about bash. Here are a few examples: Assigning values to / initializing variables In PHP you define it like $a and refer to it as $a Code:
<?php Code:
a=2 Code:
$a=2; Intuitive, isn't it? Now, let's do some simple operations with those variables. I want to assign variable c the value of the sum of variables a and b. In PHP it's pretty straightforward and logical: Code:
<?php Code:
a=2 Code:
a=2; b=3; c=$a+$b; Now THAT is really intuitive and hard to forget for someone who doesn't regularly write scripts in bash. :) Now let's have an action executed based on a condition. In PHP the if statement is pretty simple: Code:
<?php Code:
a=2 In bash this should work, right? Code:
a=2; b=3; Oh, you have to do it like this [[ $a > $b ]]. Wait, what?! It functions differently with double brackets than it functions with single ones? How effing logical! Wonder what it does with three of them... :D Ahem. An easy one to remember bash is. [/blundering_bouncy_creature_with_a_limp impersonation] :) What I really want to say is, things that should be easily human-readable are not. And they aren't easy to remember either, except for those who regularly use them. I don't "speak" Python or PHP. All of the above Python and PHP code was written without knowing the syntax of either by heart, but by googling them. Now I know how to declare and add variables and how the if clause works within these languages, and I don't think I'll forget anytime soon. What's frustrating is that the bash parts were googled too, despite using it (sporadically, I admit) for more than 10 years! So why do I have to relearn bash every time I need to use it? And I'm not talking about the advanced stuff, all of the above is pretty basic, something one would learn in the first 30 minutes of contact with a new language. So am I impaired in some way, or do you find bash less than easy too? |
Quote:
I'm not really familiar with any of them, so please correct me if I've developed the wrong impression about either of them. |
Quote:
And thank you for that link. It looks like there's a lot of useful info there and I'll consult it whenever I'll struggle with bash in the future. |
Quote:
Do you think the usual range of problems one would use Python as a bash replacement for, would expose the differences between version 2 and version 3? I mean, do I have to worry about the Python version even for small and simple scripts? |
Quote:
If you are interested in testing your scripts against both 2 and 3, then the standard practice for that is to use tox to facilitate the tests. The "print" statement is different between 2 and 3, so yes, the differences between 2 and 3 matters even for small scripts. :) |
Quote:
As an experienced programmer, do you find what I said above about bash at least partially founded, or am I being daft? |
All times are GMT -5. The time now is 03:01 AM. |