LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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 08-20-2011, 11:38 PM   #16
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928

Quote:
Originally Posted by kbp View Post
Just to stir the pot ... I used to do everything in perl but I find python a lot cleaner - meaning it's easier to read and maintain.
I'm starting to use Python more frequently because of XML-rpc;
while it's fairly easy to pull everything needed in from CPAN
on my Slackware box, in a corporate environment, running RHEL
on s390 it's a pain in the proverbial. Most of the dependencies
aren't available on EPEL for s390; Python does XML-rpc out of
the box (ancient as the version in RHEL is).

I'm starting to like some of the python ways of doing things ...



Cheers,
Tink
 
Old 08-21-2011, 04:45 AM   #17
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Here are some of the shell equivalents (non-exhaustive) for Python. For os specific tasks, use the os module. (check the docs, not going to link for you here )

1) listing files/directories (ls equivalent)
Code:
import os
for file in os.listdir("/path"):
    print file

# or you can use glob (much like shell globbing)
import glob
for file in glob.glob("*pattern*"):
    print  file

# to go through the file system (or any path ) just like the find command 
for r,d,f in os.walk("path"):
    for file in f:
        print file
2) To move/copy files. Use the shutil module. There are also methods like copytree etc similar to cp -r
Code:
import shutil
shutil.move(....)  # mv equivalent
shutil.copy(....)  # copy equivalent
3) To rename/remove files
Code:
os.rename("old","new")
os.remove(...)
4) head/tail/cat etc. All these involve opening file handle, doing something with the file handle, and closing it
Code:
# head example (head -n 4)
n=4
f=open("file"):
for i in range(0,n):
    print f.readline()    
f.close()
    

# cat equivalent

for line in open("file"):
    print line
5) simple grepping for text files
Code:
for line in open("file"):
    if "pattern" in line:
          print line
6) cut equivalent. Basically, cutting is just splitting up text according to fields and processing
Code:
for line in open("file"):
    splitted_text = line.split(delimiter)
    print splitted_text[:3]  # cut -d delimiter -f1-3
7) ftp (use ftp module)
8) telnet (use telnetlib module)

and many more.........
 
Old 08-21-2011, 05:01 AM   #18
Proud
Senior Member
 
Registered: Dec 2002
Location: England
Distribution: Used to use Mandrake/Mandriva
Posts: 2,794

Rep: Reputation: 116Reputation: 116
Quote:
Originally Posted by Woodsman View Post
Based on some of the replies, I did not explain myself well.

As I mentioned in my last sentence, the idea here is to reduce external command dependencies.
I think I got you, I don't know if you got me.

You really need to clarify if having your users/IT people install perl/python plus tk/wxwindows is going to be ok and look ok, or if you need something easier to work with like Java's Swing. Because you don't want to have to port a half/mostly working solution from perl/python after you've got to the point of trying to make a GUI and finding you/users/IT don't like it.
Quote:
The challenge: convert the shell script into a tool that will be used by non computer people in Windows. Basically that means a point-and-click interface.

Non computer people.

Using a terminal and typing commands is out of the question. A work-around to that criterion is if the script runs transparently as a desktop shortcut. I have created several small "batch files" for these people that run this way. Users point and click to the script shortcut but never actually deal with a terminal window. A similar solution could succeed here too.
Does this still stand?

Last edited by Proud; 08-21-2011 at 05:03 AM.
 
Old 08-21-2011, 05:05 AM   #19
j-ray
Senior Member
 
Registered: Jan 2002
Location: germany
Distribution: ubuntu, mint, suse
Posts: 1,591

Rep: Reputation: 145Reputation: 145
Quote:
...but for this small app, you might find it much quicker and easier to create and deploy a Java Swing GUI than something in other languages.
Quicker and easier than a perl or python script?
Definetely not.
 
Old 08-21-2011, 05:40 AM   #20
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by kbp View Post
Just to stir the pot ... I used to do everything in perl but I find python a lot cleaner - meaning it's easier to read and maintain.
Typically such claims indicate lack of Perl knowledge.
 
Old 08-21-2011, 05:56 AM   #21
Proud
Senior Member
 
Registered: Dec 2002
Location: England
Distribution: Used to use Mandrake/Mandriva
Posts: 2,794

Rep: Reputation: 116Reputation: 116
Because Perl is notoriously easy to read.

j-ray, I'm talking just to make a GUI or some popup dialogs that have a nice look&feel, and ensuring all the dependencies are set up on the system. JRE means you're good to go.
Code:
JOptionPane.showMessageDialog(frame, "Eggs are not supposed to be green.");
http://download.oracle.com/javase/tu...og.html#create

Last edited by Proud; 08-21-2011 at 05:57 AM.
 
Old 08-21-2011, 06:45 AM   #22
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by Proud View Post
Because Perl is notoriously easy to read. ...
I remember posting in a different forum a snippet of Python code stolen from here. A very short snippet. People after that were laughing big time about Python readability.

It's the programmer who mostly causes unreadable code.

Seriously, I reject Python first and foremost for lack of lexical scoping. And for lack of arrays. And for whitespaces being used to denote code blocks - and I do indent in Perl/"C".
 
Old 08-21-2011, 06:58 AM   #23
Proud
Senior Member
 
Registered: Dec 2002
Location: England
Distribution: Used to use Mandrake/Mandriva
Posts: 2,794

Rep: Reputation: 116Reputation: 116
Come on, perl's nicknamed a write-only language, the rep is for a reason even if in jest.
Kinda hard to write python that's super bad being as how whitespace has meaning especially for flow control. And why have to use curly braces when you'd have been in error without them anyway if you do as you say and indent correctly.

I might need to brush up on lexical scoping and python but I'm not clear where it's lacking sane scoping. http://docs.python.org/py3k/referenc...tionmodel.html

Also what are you on about lack of arrays? You wish to edit lists/sequences in place explicitly aka manage the memory behind them, and permit uninitialised entries?

Last edited by Proud; 08-21-2011 at 07:01 AM.
 
Old 08-21-2011, 06:59 AM   #24
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,979

Rep: Reputation: Disabled
Quote:
Originally Posted by kbp View Post
Just to stir the pot ... I used to do everything in perl but I find python a lot cleaner - meaning it's easier to read and maintain.
Quote:
Originally Posted by Proud
Because Perl is notoriously easy to read.
Well, Larry Wall, the creator of Perl, is a Linguist. Perl is designed to be as context-sensitive as any human language. As in any human language it is necessary to learn the language before one can "see" the context.

This is in my opinion the main difference between Perl and the so called "easier to read" languages like Python or Ruby.

Btw, Perl has never been easy for me, though I'm "learning" it since version 4.

Markus
 
Old 08-21-2011, 07:58 AM   #25
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Quote:
Originally Posted by Proud View Post
j-ray, I'm talking just to make a GUI or some popup dialogs that have a nice look&feel

Code:
JOptionPane.showMessageDialog(frame, "Eggs are not supposed to be green.");
http://download.oracle.com/javase/tu...og.html#create
Python with PyQt4 is just as simple (if not simpler):

Code:
QMessageBox.information(window, "Example", "Eggs are not supposed to be green.")
EDIT: And it's not just the GUI functions that make it easier to write a program, it's the language itself. Python is much easier to work with than Java.

Last edited by MTK358; 08-21-2011 at 08:46 AM.
 
Old 08-21-2011, 07:58 AM   #26
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by Proud View Post
Come on, perl's nicknamed a write-only language, ...
At the moment:

Quote:
The Comprehensive Perl Archive Network (CPAN) currently has 98,377 Perl modules
If you have difficulties reading, don't blame Perl. Or you want to say all those module are written for nothing, nobody uses them, nobody submits patches ?

As I wrote many times here, my first acquaintance with Python happened in 2000, and it was through a very intelligent guy who knew both Perl and Python.

The acquaintance was through the following process: "Here are things I do in Perl this way for this reason - what is the equivalent in Python ?". At that moment for many things there was no equivalent. Furthermore, we took a production data structure occupying ~250MBytes in memory and translated it into Python. Python simply crashed with segmentation fault.

Python has evolved since, I am watching it. Python-3.x.y is quite different from Python-2.x.y - backward compatibility is broken. Opposed to that, Perl developers pay a lot of attention to regression testing.

Python is slowly catching up with Perl, for example: http://en.wikipedia.org/wiki/Function_object#In_Python - closures were introduced in Perl 5 in mid nineties.

...

Code:
def accumulator(n):
    def inc(x):
        nonlocal n
        n += x
        return n
    return inc
- how ugly is that "nonlocal" - exactly because of lack of lexical scoping. How ugly is the "return" statement.

In Perl

Code:
sub # an anonymous function
  {
  my ($n) = @_;

  sub # since it is the last executed expression in the outer 'sub', it's also the returned value - code reference
      # the code reference points to this anonymous function
    {
    my ($x) = @_;

    $n += $x; # since it's the last executed expression in the inner 'sub', it's also the returned value - a numeric value
    }
  }
.

The above code can be rewritten without '$x':

Code:
sub{my ($n) = @_; sub{$n += $_[0]}}
.

Of course, a name can be given in the consuming scope, e.g.

Code:
my $accumulator = sub{my ($n) = @_; sub{$n += $_[0]}};
my $accumulator_instance = $accumulator->(2);
my $result = $accumulator_instance->(5);
.

Last edited by Sergei Steshenko; 08-21-2011 at 08:00 AM.
 
Old 08-21-2011, 09:37 AM   #27
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by Sergei Steshenko View Post
And for lack of arrays.
And this indicate a lack of Python knowledge. Before you take a dig at other people's preferences, maybe you should take a look at it yourself.

Last edited by ghostdog74; 08-21-2011 at 09:40 AM.
 
Old 08-21-2011, 10:03 AM   #28
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,148
Blog Entries: 2

Rep: Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886
Hey, anyone, the OP is looking for advice how to make his scripts platform independent, not another Python/Perl flamewar.
Both languages are able to achieve what the OP want, no need for that discussions about readability, there is only one person here that can decide which language suits him better: the OP.
 
Old 08-21-2011, 10:34 AM   #29
Woodsman
Senior Member
 
Registered: Oct 2005
Distribution: Slackware 14.1
Posts: 3,482

Original Poster
Rep: Reputation: 546Reputation: 546Reputation: 546Reputation: 546Reputation: 546Reputation: 546
Thanks everybody for the responses. I get the big picture that at least for Perl and Python, there are ways to perform the same tasks that are performed in shell scripts. Might require some digging into various external modules, but seems porting an equivalent and functional script to either is possible. That observation likely is true for many languages.

Quote:
I think I got you, I don't know if you got me.
I think we are getting each other, just focusing on different objectives.

Quote:
You really need to clarify if having your users/IT people install perl/python plus tk/wxwindows is going to be ok and look ok
I don't think that topic was discussed in detail in either thread. I am aware that portability of a script does not mean the script can be run in other environments. When I started this project one of the first questions I asked is what scripting languages the IT people support. I haven't yet received an answer. Bottom line is if IT personnel do not support Perl or Python, or refuse to support, then portability is a moot issue. In my other thread I addressed that I might have to port the shell script to VB Script because that is a native scripting language in Windows. That option remains open because although I installed Cygwin in my testing system to run my shell script, and I created two desktop shortcuts so the non computer people don't have to deal with the terminal or command line, I still don't yet have permission to implement that solution.

Quote:
Does this still stand?
Yes, but that statement was in another thread and the topic in this thread is different although somewhat related . User skills drive these decisions. You and I might dislike how typical end-users are treated with respect to learning new computer skills, but after three decades I have seen no general change in that attitude. Users will learn new apps but only as much as they need to get by. Most will not dig deeper into an app or process. The people I have worked with through the years are smart people, they just don't care to become more skilled with computers than they are interested in changing the oil on their car or repairing a leaky faucet. Likewise with terminals and the command line. Ain't gonna happen with most of these people. They simply have different priorities in life. These people are all born and bred in a Windows environment and that is what they use at home. Point-and-click. Using a terminal is a horrifying idea to them.
 
Old 08-21-2011, 11:49 AM   #30
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by ghostdog74 View Post
And this indicate a lack of Python knowledge. Before you take a dig at other people's preferences, maybe you should take a look at it yourself.
Oh, really ?! How about:


http://docs.python.org/library/array.html :

Quote:
This module defines an object type which can compactly represent an array of basic values: characters, integers, floating point numbers.
.

In Perl arrays are built-in, come with language, not with a module.
 
  


Reply



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
Perl experts: I need to run shell command from perl using pipe idreesedu Programming 14 01-11-2011 04:52 PM
Python related: How to access a Perl script behind a firewall from Python? vxc69 Programming 8 12-14-2010 07:32 AM
Is there a command list which indicates the UNIX command equivalents also? Lil Linux - Newbie 5 08-05-2010 03:10 AM
Use of "Command line perl" in perl script using system command. aditya007 Linux - Newbie 4 11-29-2009 10:08 PM
How do I make python programs run without entering the command python? trist007 Programming 5 03-22-2009 08:21 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:15 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration