Execute Unix commands from Perl script

Reply

Join Date: Mar 2005
Posts: 30
Reputation: crestaldin is an unknown quantity at this point 
Solved Threads: 0
crestaldin crestaldin is offline Offline
Light Poster

Execute Unix commands from Perl script

 
0
  #1
Oct 7th, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: Execute Unix commands from Perl script

 
1
  #2
Oct 7th, 2006
  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. }
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC