943,691 Members | Top Members by Rank

Ad:
  • Perl Discussion Thread
  • Unsolved
  • Views: 36725
  • Perl RSS
Oct 7th, 2006
0

Execute Unix commands from Perl script

Expand Post »
Hi everyone,

Ps I'm new to Perl (but have been working on Unix for a while) and I'm trying to develop my skills by taking on a project . I am trying to create a perl script that when executed behaves like a Unix Shell, but I need help on how to capture user inputs and then run them as commands at the prompt.

For example , when the perl script is run and the user types ls ., I want to capture the ls, store it in a variable...say $input , and then run the ls command so that all files listed in the current working directory are listed.


Kindly explain how to go about this to me as this is the foundation of the larger picture that I have in mind.

Thanks in advance.

NB. If you know any online resource that will help in this work, kindly let me know.
Reputation Points: 10
Solved Threads: 0
Light Poster
crestaldin is offline Offline
30 posts
since Mar 2005
Oct 7th, 2006
1

Re: Execute Unix commands from Perl script

Perl Syntax (Toggle Plain Text)
  1. #!/usr/bin/perl
  2.  
  3. while ($uinput ne "quit") {
  4. print "# "; $uinput = <>;
  5.  
  6. chomp($uinput);
  7.  
  8. if ($uinput eq "ls") {
  9. open(LS, "ls -AF1 |");
  10. while (<LS>) {
  11. chomp;
  12. push @flist, $_;
  13. }
  14. close(LS);
  15.  
  16. foreach $file (@flist) {
  17. print "$file\n";
  18. }
  19. }
  20. }
Team Colleague
Reputation Points: 361
Solved Threads: 214
Taboo Programmer
Comatose is offline Offline
2,413 posts
since Dec 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Perl Forum Timeline: problems writing multiple files
Next Thread in Perl Forum Timeline: How do you do efficient array (de)referencing?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC