Unable to run built-in commands from system

N

noauth

The following code works fine for CommandString1, but it does not work for CommandString2.
The result is a "No such file or directory" error message. I believe this is because "cd"
is a built-in shell command. Does anyone have any suggestions on how I can overcome this?

$CommandString1 = "ls ..";
$CommandString2 = "cd ..";

local $/ = \1; # Read 1 character at a time
open(STATUS, "$CommandString1 2>&1|") || ddie "Unable to run specified command...$!";
while (<STATUS>) {
dprint "$_";
}
close(STATUS);

open(STATUS, "$CommandString2 2>&1|") || ddie "Unable to run specified command...$!";
while (<STATUS>) {
dprint "$_";
}
close(STATUS);
 
J

J. Gleixner

noauth said:
The following code works fine for CommandString1, but it does not work for CommandString2.
The result is a "No such file or directory" error message. I believe this is because "cd"
is a built-in shell command. Does anyone have any suggestions on how I can overcome this?

You don't provide your commands, so how can we tell what may be
causing that error?

More than likely, you're not doing the right thing in
your $CommandString2, it's a typical error when the
directory doesn't exist.

cd /bogusdir
/bogusdir: No such file or directory.

Why are you running 'cd' and 'ls' in the first place?

perldoc -f chdir
perldoc -f readdir

What do you actually want to do?

dprint..ddie??.. d'oh...
 
S

smallpond

The following code works fine for CommandString1, but it does not work for CommandString2.  
The result is a "No such file or directory" error message.  I believe this is because "cd"
is a built-in shell command.  Does anyone have any suggestions on how Ican overcome this?

$CommandString1 = "ls ..";
$CommandString2 = "cd ..";

local $/ = \1; # Read 1 character at a time
open(STATUS, "$CommandString1 2>&1|") || ddie "Unable to run specified command...$!";
while (<STATUS>) {
  dprint "$_";}

close(STATUS);

open(STATUS, "$CommandString2 2>&1|") || ddie "Unable to run specified command...$!";
while (<STATUS>) {
  dprint "$_";}

close(STATUS);

It looks like you are trying to capture the output of
a shell builtin. This does want you want:

perl -e 'open FOO,"bash -c cd ..|"; while(<FOO>) {print $_}'

Of course, cd doesn't have very much to say.
 
J

Jürgen Exner

noauth said:
The following code works fine for CommandString1, but it does not work for CommandString2.
The result is a "No such file or directory" error message. I believe this is because "cd"
is a built-in shell command. Does anyone have any suggestions on how I can overcome this?

$CommandString2 = "cd .."; [...]
open(STATUS, "$CommandString2 2>&1|") || ddie "Unable to run specified command...$!";

What is this supposed to do? Fork a new process, run "cd .." to change
the CWD of that new process, and then close that new process
immediately?

Doesn't make much sense to me...

jue
 
J

Josef Moellers

Tad said:
No it doesn't.


^
^

The problem is that your keyboard has a wonky "d" key.

And <whoever> has forgotten to insert "use warnings; use strict;"!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top