LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Python/Perl Command Equivalents (https://www.linuxquestions.org/questions/programming-9/python-perl-command-equivalents-898357/)

Woodsman 08-19-2011 05:55 PM

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!

kbp 08-20-2011 02:41 AM

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:

echo, ls, grep, cut, sort, uniq, head, tail, rm, cat, mv
or
Quote:

(g)awk, sed
because they're not shell scripts, in the case of perl and python the scripts are compiled on the fly and then executed.

j-ray 08-20-2011 05:10 AM

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)

kbp 08-20-2011 07:44 AM

OP will have to avoid calling system commands or the script won't be cross platform, just use the capabilities of the chosen language.

MTK358 08-20-2011 07:51 AM

@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.

onebuck 08-20-2011 09:44 AM

Moved: This thread is more suitable in <Programming> and has been moved accordingly to help your thread/question get the exposure it deserves.

Proud 08-20-2011 10:29 AM

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:All of which depends on an understanding of the basics, like flow control.

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

Woodsman 08-20-2011 12:49 PM

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:

Most languages have loop statements (for, if/else, while)...
Yeah, I know that. I realized a few hours after posting that I should not have even mentioned those requirements. :)

Quote:

It's not at all like shell scripting, and there are no "equivalents" for stuff like coreutils commands and shell builtins.
That is part of what I did not explain properly. I am presuming I need to find libraries, functions, and procedures that perform the same results as the external commands used in a shell script. As I am unfamiliar with languages such as Perl, Python, or Java, I don't know how easily those same tasks can be rewritten.

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."

TobiSGD 08-20-2011 01:28 PM

Python string manipulation
Perl string manipulation

markush 08-20-2011 01:29 PM

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:

Originally Posted by Woodsman
Shell built-in commands:
exit, for/done, if/then/fi, cd

Coreutils:
echo, ls, grep, cut, sort, uniq, head, tail, rm, cat, mv

as mentioned above the loops and conditional statements are not OS-related. For commands like cd or mv you can use the system command.
Code:

#!/usr/bin/perl -w

system("mv thisfile anothername");

For "ls" you can chose between the Perl-commands opendir/readdir and the so called "backticks". Other than system the backticks make the ouput of a command available in the Perl-script, an example
Code:

#!/usr/bin/perl -w

my @filesinthisdirctory = `ls`;

will create an array @filesinthisdirectory with all the filenames.

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

Sergei Steshenko 08-20-2011 02:40 PM

Quote:

Originally Posted by markush (Post 4448716)
... For commands like cd ... you can use the system command. ...

Not quite. I.e. 'cd' through 'system' will affect the child process, not the script itself.
...
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/ .

MTK358 08-20-2011 03:05 PM

Quote:

Originally Posted by markush (Post 4448716)
Code:

#!/usr/bin/perl -w

system("mv thisfile anothername");


If you're going to do that, you might as well use a shell script. At least you won't need Perl as a dependency.

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.

markush 08-20-2011 03:14 PM

Quote:

Originally Posted by MTK358 (Post 4448778)
If you're going to do that, you might as well use a shell script. At least you won't need Perl as a dependency.

well, I just wanted to give an example ;)

Quote:

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.
That's interesting, I wasn't aware of that, thanks for the information.

Markus

Tinkster 08-20-2011 07:24 PM

Quote:

Originally Posted by markush (Post 4448785)
well, I just wanted to give an example ;)


That's interesting, I wasn't aware of that, thanks for the information.

Markus

For perl:
Code:

man File::Copy

Cheers,
Tink

kbp 08-20-2011 08:09 PM

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.