Python/Perl Command Equivalents
Recently I asked for some ideas about porting a shell script to a cross platform scripting language.
I have no experience with Python or Perl, although learning both is on my to-do list. I'm looking for a chart of some sort or mini how-to that explains which shell built-in and standard command line tools are available directly within the Python and Perl languages. Specifically I am interested in these commands: Shell built-in commands: exit, for/done, if/then/fi, cd Coreutils: echo, ls, grep, cut, sort, uniq, head, tail, rm, cat, mv Other: (g)awk, sed I am aware of and have used the GNUWin32 ports of many standard command line tools, but the idea here is to reduce as much external command dependency as possible. Thank much! |
I think you're missing the point, you'll need to do things differently because it's not a shell script. Most languages have loop statements (for, if/else, while) but you won't have
Quote:
Quote:
|
In both languages you find the mentioned flow controls like if, else, while and such. Both allow to execute system commands and use the results.
in Perl you can say system("echo 'aha'"); whereas in Python you need the subprocess module to do the same. (I havn't used Python very often so I can't tell in detail) |
OP will have to avoid calling system commands or the script won't be cross platform, just use the capabilities of the chosen language.
|
@Woodsman
It looks like you have a fundamentally wrong idea of what a "real" programming language is like. It's not at all like shell scripting, and there are no "equivalents" for stuff like coreutils commands and shell builtins. |
Moved: This thread is more suitable in <Programming> and has been moved accordingly to help your thread/question get the exposure it deserves.
|
You might have to specify exactly what your script is currently doing and how it's using the features of those coreutils. As I think has been expressed, languages like perl and python provide many robust ways to control the flow of a program written in them, but you typically use the features found in their libraries rather than call shell utilities and therefore you need to know the basics of the language to performs the basics like echo or mv.
For python: For ls, rm, mv, see: http://docs.python.org/py3k/library/shutil.html And note the mention of the os library/module/package/whatever. For cat/tail/head it might be: http://docs.python.org/py3k/library/io.html See the readline() methods. For grep/cut/sort/unique you'll need:
Lastly, are you tied to perl or python? Because most IT departments would be happier supporting something like a Java app than getting python and tk packaged if you must create a nice GUI and remain crossplatform. The Java Runtime Environment is typically on most Windows machines for regular office builds. The Java documentation's very similarly structured. Java, or rather Oracle, dependency is becoming a concern, 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. But I haven't touched python in quite a while and just remember struggling a bit with tk and wxwindows/wxwidgets for GUIs. Also I have no real experience of perl, but afaik it's made for text manipulation, it's RE syntax is inherited in python and Java iirc. Just happened across this for python PDF manipulation http://pybrary.net/pyPdf/ For Java you might need http://pdfbox.apache.org/ or possibly can use http://itextpdf.com/ As for perl: http://www.perl.com/pub/2007/09/20/p...with-perl.html |
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. Shell scripts are wonderful in native 'nix environments but are not portable to other operating systems. If I port a shell script to another language and that port must run on Windows, then I no longer want to depend upon common Linux tools and commands because I can't expect those tools to exist natively in Windows. Yes, there is Cygwin and GNUWin32 tools, but that requires installing that software, which creates external dependencies. Therefore I am looking for how to perform the same tasks I perform in my shell script without the external dependencies. Quote:
Quote:
Some preliminary reading indicates that Perl has some built in methods to simulate or perform the equivalent of external commands such as grep and sed. I was hoping I could find a more exhaustive list of these "equivalents." |
|
Hi Woodsman,
Perl has many commands adapted from the shell/s and other tools like grep, sed, awk and many others. But Perl often does things on it's own way. Perl-regex for example has become more or less the de facto standard for regular expressions at all (this is not directly related to your question about shell commands). I've written some Perl-scripts which I use on Windows-servers and these run on my Slackware-machine as well. to your list from your first post: Quote:
Code:
#!/usr/bin/perl -w Code:
#!/usr/bin/perl -w When it comes to compatibility with Windows you'll have to make sure that different systemcommands are handled correct and that in pathes we us "/" in Linux but "\" in Windows. But you can let Perl find out which OS it is and put the various commands in an if-statement. Markus |
Quote:
... To the OP: just start patiently reading http://perldoc.perl.org/ -> http://perldoc.perl.org/index-overview.html , and then items one by one in the left column of http://perldoc.perl.org/ . |
Quote:
https://duckduckgo.com/?q=perl+move+file Almost all common languages come with functions for cross-platform file system manipulation. And they also usually automatically convert "/" to the OS's native path separator, so you don't even need to worry about that. |
Quote:
Quote:
Markus |
Quote:
Code:
man File::Copy Cheers, Tink |
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.
|
All times are GMT -5. The time now is 09:25 AM. |