LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 10-23-2013, 09:43 AM   #1
chicken76
Member
 
Registered: Mar 2009
Distribution: Slackware
Posts: 74

Rep: Reputation: 1
Question 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?
 
Old 10-23-2013, 09:45 AM   #2
dugan
Senior Member
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 4,673

Rep: Reputation: 1440Reputation: 1440Reputation: 1440Reputation: 1440Reputation: 1440Reputation: 1440Reputation: 1440Reputation: 1440Reputation: 1440Reputation: 1440
Quote:
Originally Posted by chicken76 View Post
Those being said, would you think python could replace bash for medium-sized scripts in linux?
Depends on the script.
 
Old 10-23-2013, 10:00 AM   #3
chicken76
Member
 
Registered: Mar 2009
Distribution: Slackware
Posts: 74

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by dugan View Post
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.
 
Old 10-23-2013, 10:04 AM   #4
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6
Posts: 1,470

Rep: Reputation: 438Reputation: 438Reputation: 438Reputation: 438Reputation: 438
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.
 
Old 10-23-2013, 10:34 AM   #5
chicken76
Member
 
Registered: Mar 2009
Distribution: Slackware
Posts: 74

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by TenTenths View Post
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)
 
Old 10-23-2013, 10:39 AM   #6
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6
Posts: 1,470

Rep: Reputation: 438Reputation: 438Reputation: 438Reputation: 438Reputation: 438
Quote:
Originally Posted by chicken76 View Post
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.
 
Old 10-23-2013, 10:55 AM   #7
chicken76
Member
 
Registered: Mar 2009
Distribution: Slackware
Posts: 74

Original Poster
Rep: Reputation: 1
Thank you TenTenths. I'll add PHP to the list of candidates.
 
Old 10-23-2013, 11:42 AM   #8
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,513

Rep: Reputation: 1895Reputation: 1895Reputation: 1895Reputation: 1895Reputation: 1895Reputation: 1895Reputation: 1895Reputation: 1895Reputation: 1895Reputation: 1895Reputation: 1895
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
 
Old 10-23-2013, 11:42 AM   #9
dugan
Senior Member
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 4,673

Rep: Reputation: 1440Reputation: 1440Reputation: 1440Reputation: 1440Reputation: 1440Reputation: 1440Reputation: 1440Reputation: 1440Reputation: 1440Reputation: 1440
Quote:
Originally Posted by chicken76 View Post
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).

Last edited by dugan; 10-23-2013 at 12:22 PM.
 
Old 10-23-2013, 02:18 PM   #10
chicken76
Member
 
Registered: Mar 2009
Distribution: Slackware
Posts: 74

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by grail View Post
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". 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...
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?
 
Old 10-23-2013, 02:26 PM   #11
chicken76
Member
 
Registered: Mar 2009
Distribution: Slackware
Posts: 74

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by grail View Post
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.

Last edited by chicken76; 10-23-2013 at 02:58 PM.
 
Old 10-23-2013, 02:30 PM   #12
chicken76
Member
 
Registered: Mar 2009
Distribution: Slackware
Posts: 74

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by grail View Post
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.
 
Old 10-23-2013, 02:43 PM   #13
chicken76
Member
 
Registered: Mar 2009
Distribution: Slackware
Posts: 74

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by dugan View Post
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?

Last edited by chicken76; 10-23-2013 at 02:45 PM.
 
Old 10-23-2013, 02:44 PM   #14
dugan
Senior Member
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 4,673

Rep: Reputation: 1440Reputation: 1440Reputation: 1440Reputation: 1440Reputation: 1440Reputation: 1440Reputation: 1440Reputation: 1440Reputation: 1440Reputation: 1440
Quote:
Originally Posted by chicken76 View Post
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.

Last edited by dugan; 10-23-2013 at 02:50 PM.
 
Old 10-23-2013, 03:04 PM   #15
chicken76
Member
 
Registered: Mar 2009
Distribution: Slackware
Posts: 74

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by dugan View Post
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?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell scripting help needed burwood_301 Linux - Newbie 3 04-19-2010 02:00 PM
Shell Scripting Geeks, advice and assist needed :-) WhisperiN Programming 6 09-25-2009 01:02 AM
moving linux and expert advice needed mrgreaper Linux - Newbie 4 02-06-2007 08:48 AM
Bash Scripting newb.. Advice needed. trey85stang Linux - General 5 09-28-2006 12:05 PM
shell scripting - help needed jonhewer Linux - Newbie 54 09-09-2005 02:58 PM


All times are GMT -5. The time now is 11:28 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration