LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Advice needed for moving away from shell scripting (https://www.linuxquestions.org/questions/programming-9/advice-needed-for-moving-away-from-shell-scripting-4175481869/)

chicken76 10-23-2013 09:43 AM

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?

dugan 10-23-2013 09:45 AM

Quote:

Originally Posted by chicken76 (Post 5050940)
Those being said, would you think python could replace bash for medium-sized scripts in linux?

Depends on the script.

chicken76 10-23-2013 10:00 AM

Quote:

Originally Posted by dugan (Post 5050941)
Depends on the script.

Well, generally I handle files (copy, move, compress, etc.), do some arithmetic evaluations, read run-time user input, all things which are the "bread and butter" of the shell, I admit. But these things could also be handled by another language I presume, and some of them might have a simpler, easier to read syntax. Not really knowing any of them, I thought I'd post here and see what the people that "speak" bash and several other scripting languages have to say.

TenTenths 10-23-2013 10:04 AM

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.

chicken76 10-23-2013 10:34 AM

Quote:

Originally Posted by TenTenths (Post 5050951)
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.

I frequently need to work with the file/folder names as strings and my scripts use such programs as cut, rev, tail, wc, etc. I don't find them difficult at all, as they have very clear man pages. It's the bash syntax that's needed to put them all together that I find difficult. Would you say this is easily done in PHP and the syntax needed is simpler and easier to read? (I have no experience with PHP other than what's used in web pages, and that feels different)

TenTenths 10-23-2013 10:39 AM

Quote:

Originally Posted by chicken76 (Post 5050967)
I frequently need to work with the file/folder names as strings and my scripts use such programs as cut, rev, tail, wc, etc. I don't find them difficult at all, as they have very clear man pages. It's the bash syntax that's needed to put them all together that I find difficult. Would you say this is easily done in PHP and the syntax needed is simpler and easier to read? (I have no experience with PHP other than what's used in web pages, and that feels different)

Well PHP has an extremely well developed set of functions etc. for string handling so it may be easier to use than a lot of cut/rev/tr etc. etc. I guess the thing is, pick one of your simpler scripts and have a go at doing the same thing in PHP and see how you get on. There's no shortage of material out there for learning PHP it just happens to be more geared towards web output rather than scripting.

chicken76 10-23-2013 10:55 AM

Thank you TenTenths. I'll add PHP to the list of candidates.

grail 10-23-2013 11:42 AM

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 a result I look for answers online and many times I just copy&paste solutions provided in forums
Then I believe no language will be easy for you to understand.

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

dugan 10-23-2013 11:42 AM

Quote:

Originally Posted by chicken76 (Post 5050950)
Well, generally I handle files (copy, move, compress, etc.), do some arithmetic evaluations, read run-time user input.

Python would be fine for that.

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:

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.
Those are the same consideration. The answer is always obvious if you have access to the machines that you will be working on. (And typically, the answer is 2.7).

chicken76 10-23-2013 02:18 PM

Quote:

Originally Posted by grail (Post 5051007)
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.

Thank you for answering my question.
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
 $a=2;
 echo $a;
?>

In Python this is pretty straightforward:
Code:

a=2
print a

In bash this should work, right?
Code:

$a=2;
echo $a

Wrong. You have to use a=2 and then refer to it as $a. Ok, maybe it wasn't initialized so that's why I couldn't start with $a. But now it is, so $a=4 should be ok. Wrong again.
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
 $a=2; $b=3; $c=$a+$b;
 echo $c;
?>

In Python, you follow the same simple syntax to achieve this.
Code:

a=2
b=3
c=a+b
print c

In bash, you'd do:
Code:

a=2; b=3; c=$a+$b;
echo $c

Wait, it says "2+3". :doh: That's not what I wanted. Arithmetic evaluations you say? Ok, it has to be enclosed within double parenthesis to be evaluated, otherwise they're treated as strings and concatenated. Oh wait, it has to have a dollar sign in front too!
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
 $a=2; $b=3;
 if ( $a > $b ) echo "a is bigger than b";
    else echo "b is bigger than a";
?>

In Python you would write it like this:
Code:

a=2
b=3
if a > b:
    print "a is bigger than b"
else:
    print "b is bigger than a"

Pretty readable I'd say. All you have to remember is the colon.

In bash this should work, right?
Code:

a=2; b=3;
if [ $a > $b ]
    then echo "a is greater than b"
    else echo "b is greater than a"
fi

And it works. It says "a is greater than b". What the ....!? Was this in my CPU's errata and I missed it? :)
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?

chicken76 10-23-2013 02:26 PM

Quote:

Originally Posted by grail (Post 5051007)
As to a recommendation, I would possibly suggest Perl.

From what I can tell about Perl, it is the opposite of Python, it is the-100-ways-of-doing-something-and-all-are-valid kind of language, whereas Python strives to provide one way of doing one thing in order to keep it easy to read.

I'm not really familiar with any of them, so please correct me if I've developed the wrong impression about either of them.

chicken76 10-23-2013 02:30 PM

Quote:

Originally Posted by grail (Post 5051007)
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

Thank you for posting your opinions though. Your input is appreciated.
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.

chicken76 10-23-2013 02:43 PM

Quote:

Originally Posted by dugan (Post 5051008)
Python would be fine for that.

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.

Those are the same consideration. The answer is always obvious if you have access to the machines that you will be working on. (And typically, the answer is 2.7).

Thank you dugan.
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?

dugan 10-23-2013 02:44 PM

Quote:

Originally Posted by chicken76 (Post 5051074)
Thank you dugan.
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 version of Python even for small scripts?

The standard practice with Python is to specifically write for either 2 or 3. So no, you don't "worry about the version."

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. :)

chicken76 10-23-2013 03:04 PM

Quote:

Originally Posted by dugan (Post 5051075)
The standard practice with Python is to specifically write for either 2 or 3. So no, you don't "worry about the version."

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. :)

Thank you again dugan.
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.