LinuxQuestions.org
Visit Jeremy's Blog.
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-19-2011, 05:55 PM   #1
Woodsman
Senior Member
 
Registered: Oct 2005
Distribution: Slackware 14.1
Posts: 3,482

Rep: Reputation: 546Reputation: 546Reputation: 546Reputation: 546Reputation: 546Reputation: 546
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!
 
Old 08-20-2011, 02:41 AM   #2
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,790

Rep: Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653
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.
 
Old 08-20-2011, 05:10 AM   #3
j-ray
Senior Member
 
Registered: Jan 2002
Location: germany
Distribution: ubuntu, mint, suse
Posts: 1,591

Rep: Reputation: 145Reputation: 145
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)

Last edited by j-ray; 08-20-2011 at 05:13 AM.
 
Old 08-20-2011, 07:44 AM   #4
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,790

Rep: Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653
OP will have to avoid calling system commands or the script won't be cross platform, just use the capabilities of the chosen language.
 
Old 08-20-2011, 07:51 AM   #5
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
@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.
 
Old 08-20-2011, 09:44 AM   #6
onebuck
Moderator
 
Registered: Jan 2005
Location: Central Florida 20 minutes from Disney World
Distribution: SlackwareŽ
Posts: 13,923
Blog Entries: 44

Rep: Reputation: 3158Reputation: 3158Reputation: 3158Reputation: 3158Reputation: 3158Reputation: 3158Reputation: 3158Reputation: 3158Reputation: 3158Reputation: 3158Reputation: 3158
Moved: This thread is more suitable in <Programming> and has been moved accordingly to help your thread/question get the exposure it deserves.
 
Old 08-20-2011, 10:29 AM   #7
Proud
Senior Member
 
Registered: Dec 2002
Location: England
Distribution: Used to use Mandrake/Mandriva
Posts: 2,794

Rep: Reputation: 116Reputation: 116
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

Last edited by Proud; 08-20-2011 at 10:56 AM.
 
Old 08-20-2011, 12:49 PM   #8
Woodsman
Senior Member
 
Registered: Oct 2005
Distribution: Slackware 14.1
Posts: 3,482

Original Poster
Rep: Reputation: 546Reputation: 546Reputation: 546Reputation: 546Reputation: 546Reputation: 546
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."
 
Old 08-20-2011, 01:28 PM   #9
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
Python string manipulation
Perl string manipulation
 
Old 08-20-2011, 01:29 PM   #10
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,979

Rep: Reputation: Disabled
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
 
Old 08-20-2011, 02:40 PM   #11
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by markush View Post
... 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/ .
 
Old 08-20-2011, 03:05 PM   #12
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 markush View Post
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.

Last edited by MTK358; 08-20-2011 at 03:07 PM.
 
1 members found this post helpful.
Old 08-20-2011, 03:14 PM   #13
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,979

Rep: Reputation: Disabled
Quote:
Originally Posted by MTK358 View Post
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
 
Old 08-20-2011, 07:24 PM   #14
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 markush View Post
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

Last edited by Tinkster; 08-20-2011 at 07:25 PM.
 
1 members found this post helpful.
Old 08-20-2011, 08:09 PM   #15
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,790

Rep: Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653
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.
 
  


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
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:00 AM.

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