CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
commands not running

 



ym_chaitu
Novice

May 18, 2010, 5:23 AM

Post #1 of 7 (805 views)
commands not running Can't Post

hai
i am quite new to perl. i know php and mysql. so i require some help from u guys

i have written one small script to run the short commands
this is the code


Code
#!/usr/bin/perl 
use warnings;
# CUSTOM SYSTEM COMMANDS

print "Please Enter The Command You Wish To Initiate \n";

print "Enter The Commands With NO CAPITAL LETTERS\n";

print "HIS --- PRINTS RECENT COMMAND HISTORY\n";

print "LIST --- LISTS THE CONTENTS OF THE CURRENT /DIR \n";

print "INSTALL --- WILL PROMPT YOU TO ENTER PACKAGE NAME, THEN INSTALL\n";

print "UPDATE --- WILL UPDATE YOUR SYSTEM'S PROGRAMS\n";

print "SEARCH --- WILL PROMPT FOR PACKAGE NAME, THEN WILL SEARCH\n";

print "CHANGEDIRECTORY --- WILL CHANGE DIRECTORY\n";



##################################################

chomp ($varinput = <STDIN>);

#print "Var Input=> ".$varinput ."\n";

if ($varinput eq "his")
{

system("history");

}


and the remaining code folows
now what the issue is when i run the his the warning that is given is

Quote
Can't exec "history": No such file or directory at cmd.pl line 33, <STDIN> line 1.


so what could be wrong
i am running this under the cent os 5.4 and the perl version is 5.8.8


FishMonger
Veteran / Moderator

May 18, 2010, 6:29 AM

Post #2 of 7 (797 views)
Re: [ym_chaitu] commands not running [In reply to] Can't Post

Why aren't you making use of the suggestions that we gave in your devshed post?

http://forums.devshed.com/perl-programming-6/help-making-program-start-over-after-code-is-executed-706392.html

Instead of executing the history command, you should read/parse the actual history file.


ym_chaitu
Novice

May 18, 2010, 7:30 AM

Post #3 of 7 (793 views)
Re: [FishMonger] commands not running [In reply to] Can't Post

hai
thank you for ur reply

actually i have tried parsing the file but it is saying as file doesnot exist..
this is the code i have tried..but it is no working..

Code
open(DAT, "~/.bash_history")or die("Cannot open file"); 
$raw_data=<DAT>;
print $raw_data;


can u help me with this..


FishMonger
Veteran / Moderator

May 18, 2010, 8:48 AM

Post #4 of 7 (790 views)
Re: [ym_chaitu] commands not running [In reply to] Can't Post

Use the full path to the file and include $! in the die statement, which gives the reason it could not be opened.

Use the 3 arg form of open and a lexical var for the filehandle.

Put all lines into an array.


Code
my $historyfile = '/home/me/.bash_history'; 
open my $historyFH, '<', $historyfile or die "Cannot open '$historyfile' $!";

my @history = <$historyFH>;


or

Code
use File::Slurp; 

my $historyfile = '/home/me/.bash_history';
my @history = read_file( $historyfile ) ;



ym_chaitu
Novice

May 18, 2010, 10:10 PM

Post #5 of 7 (782 views)
Re: [FishMonger] commands not running [In reply to] Can't Post

hai
finally i figured out what is the issue..
this is the code for any one who is in need of it..

Code
if ($varinput eq "his") 
{
#system( "bash -c history" );
#print "Hai\n";
#system ("/usr/local/bin/history");
open(DAT, glob( '~/.bash_history' ))or die("Cannot open file");
my @lines=<DAT>;
#print $raw_data;
#open(MYINPUTFILE, "<glob('~/.bash_history')"); # open for input
#my(@lines) = <MYINPUTFILE>; # read file into list
#@lines = sort(@lines); # sort the list
my($line);
foreach $line (@lines) # loop thru list
{
print "$line"; # print in sort order
}
close(DAT);

}

Tongue


rovf
Veteran

May 19, 2010, 3:47 AM

Post #6 of 7 (776 views)
Re: [ym_chaitu] commands not running [In reply to] Can't Post

I don't think open does ~-expansion Unsure


FishMonger
Veteran / Moderator

May 19, 2010, 5:57 AM

Post #7 of 7 (770 views)
Re: [ym_chaitu] commands not running [In reply to] Can't Post

Why aren't you using a lexical var and the 3 arg form of open or the $! in the die statement like I showed?

Yes, glob will work, but should not be done directly in the open call. Instead, you should assign its results to a var and use that var in the open call and die statement.

Instead of slurping the file into an array and looping over that array with a foreach loop, it would be better and more efficient to use while to loop over the filehandle.

$line should be declared in the smallest scope that it requires, which is inside the while (or foreach) loop.

Do not quote single vars. See `perldoc -q quoting`: http://perldoc.perl.org/perlfaq4.html#What%27s-wrong-with-always-quoting-%22$vars%22?


(This post was edited by FishMonger on May 19, 2010, 5:58 AM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives