For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > March 2006 > is it a bug with system()?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author is it a bug with system()?
19840129

2006-03-21, 3:56 am

system("cd") return -1 and set errno msg "cd :no this file or
directory"
so it is not like c, "cd" may not be send to shell to explain.
but the perldoc -f system says that it is.
so is it bug? if not ,can somebody tell me why even system("cd") has no
use?

sorry for my poor English

Josef Möllers

2006-03-21, 3:56 am

19840129 wrote:

> system("cd") return -1 and set errno msg "cd :no this file or
> directory"
> so it is not like c, "cd" may not be send to shell to explain.
> but the perldoc -f system says that it is.
> so is it bug? if not ,can somebody tell me why even system("cd") has no
> use?


You'll start a shell which will change to the current user's home directory
and then exit.
What use should that have?
--
josef punkt moellers bei gmx punkt de
prawn

2006-03-21, 7:56 am

19840129 wrote:
> system("cd") return -1 and set errno msg "cd :no this file or
> directory"
> so it is not like c, "cd" may not be send to shell to explain.
> but the perldoc -f system says that it is.
> so is it bug? if not ,can somebody tell me why even system("cd") has no
> use?
>
> sorry for my poor English
>


This is windows, right? "cd" is an internal command, so a system call
won't find a corresponding .exe/.cmd/.bat/.com file on the path.



--
p LotR#9 BotM#1
xcm

2006-03-21, 7:56 am


"Josef Möllers" <1700-820@onlinehome.de> ??????:dvohdt$cr9$1@online.de...
> 19840129 wrote:
>
>
> You'll start a shell which will change to the current user's home

directory
> and then exit.
> What use should that have?
> --
> josef punkt moellers bei gmx punkt de



en ,I know it,but why system("cd") return -1 ?
it is not the behavior as perldoc -f system() describes


xcm

2006-03-21, 7:56 am


"prawn" <prawnMUNG@prawn.me.uk>
??????:441fd0df$0$3626$ed2e19e4@ptn-nntp-reader04.plus.net...
> 19840129 wrote:
>
> This is windows, right? "cd" is an internal command, so a system call
> won't find a corresponding .exe/.cmd/.bat/.com file on the path.
>
>
>
> --
> p LotR#9 BotM#1




oh,no,it's in linux and in windows it acts well;
the doc says like :
"the system function does a fork() and the parent wait the exit of the child
which uses a shell to execute the command just like in C' system "




xcm

2006-03-21, 7:56 am

and I know "cd" is a internal command of shell.

I only want to know if system() always pass the command to shell or derectly
use exec family to execute the command.



Anno Siegel

2006-03-21, 7:56 am

xcm <kukucm@163.com> wrote in comp.lang.perl.misc:
> and I know "cd" is a internal command of shell.
>
> I only want to know if system() always pass the command to shell or derectly
> use exec family to execute the command.


That is explained in "perldoc -f system" (and, perhaps, "perldoc -f exec").

The answer is that "system 'cd'" does not use a shell. Since (under Unix,
normally) a command named "cd" doesn't exist, the call fails with the
error message you've seen. If you change it to "system 'cd;'" Perl
will use a shell and you won't see an error. It still doesn't make
sense to do that.

Anno
--
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
Lukas Mai

2006-03-21, 7:56 am

19840129 <kukucm@163.com> schrob:
> system("cd") return -1 and set errno msg "cd :no this file or
> directory"
> so it is not like c, "cd" may not be send to shell to explain.
> but the perldoc -f system says that it is.
> so is it bug? if not ,can somebody tell me why even system("cd") has no
> use?


perldoc -f system:
[...]
| If there is only one scalar argument, the argument is checked for shell
| metacharacters, and if there are any, the entire argument is passed to
| the system's command shell for parsing (this is "/bin/sh -c" on Unix
| platforms, but varies on other platforms). If there are no shell
| metacharacters in the argument, it is split into words and passed
| directly to "execvp", which is more efficient.

"cd" doesn't contain shell metacharacters, so perl tries to exec it
directly, which fails for obvious reasons. A simple "workaround" is to
use a shell metacharacter, as in system("cd;"); however, this still
won't change the working directory of your script. For that you should
see perldoc -f chdir.

HTH, Lukas
19840129

2006-03-21, 7:56 am

Thanks all.

Tad McClellan

2006-03-21, 7:56 am

19840129 <kukucm@163.com> wrote:

> system("cd") return -1 and set errno msg "cd :no this file or
> directory"


> sorry for my poor English



Please don't paraphrase error messages, copy them verbatim.


--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
Tad McClellan

2006-03-21, 7:56 am

xcm <kukucm@163.com> wrote:

> and I know "cd" is a internal command of shell.



So then,

system 'cd';

is totally useless.

What is it that you are really trying to accomplish?

Do you know that you can change the current working directly in
a manner that may actually be useful for something?

perldoc -f chdir


> I only want to know if system() always pass the command to shell or derectly
> use exec family to execute the command.



The docs for system() answer your question:

Note that argument processing varies depending on the
number of arguments. If there is more than one argument in LIST,
or if LIST is an array with more than one value, starts the program
given by the first element of the list with arguments given by the
rest of the list. If there is only one scalar argument, the argument
is checked for shell metacharacters, and if there are any, the
entire argument is passed to the system's command shell for parsing
(this is C</bin/sh -c> on Unix platforms, but varies on other
platforms). If there are no shell metacharacters in the argument,
it is split into words and passed directly to C<execvp>, which is
more efficient.

Q: How many args are you passing to system()?

A: One.

Q: Does that single argument contain any shell metacharacters?

A: No.

Q: Will system() invoke a shell then?

A: No.

So attempting to run a shell builtin will fail, because there is no shell.


You could call it with one arg that _does_ contain shell metacharacters:

system 'cd ~'


Or, probably better, call system with a list, and explicitly
call the shell that you want:

system '/bin/bash', '-c', 'cd'


--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
Joe Smith

2006-03-26, 3:59 am

xcm wrote:
> "Josef Möllers" <1700-820@onlinehome.de> ??????:dvohdt$cr9$1@online.de...
> directory
>
>
> en ,I know it,but why system("cd") return -1 ?
> it is not the behavior as perldoc -f system() describes


It is behaving exactly as documented in 'perldoc -f system()'.

perl -le 'print system("foobar")," error=$!"'
-1 error=No such file or directory

That error is correct; there is no executable file by that name in
any of the directories specified by $ENV{PATH}.

perl -le 'print "$_/cd does ", -f "$_/cd" ? "" : "not ", "exist" for split /:/,$ENV{PATH}'

The "cd" command is built-in to the shell, it is _not_ an executable binary.

-Joe
Bart Lateur

2006-03-27, 7:59 am

19840129 wrote:

>system("cd") return -1 and set errno msg "cd :no this file or
>directory"
>so it is not like c, "cd" may not be send to shell to explain.
>but the perldoc -f system says that it is.
>so is it bug? if not ,can somebody tell me why even system("cd") has no
>use?


single word command lines do not use the shell, they try to directly
call the program.

And there is no program called "cd", as it is a built-in from the shell.

If you were to add another "word", it should more or less work, like:

system("cd .")

I'm saying "more or less", because the net result of this command would
still be nothing. You see, even if the child program changes its current
directory, the parent process, your Perl script, wouldn't see it. Parent
processes don't inherit from their children.

Why aren't you just using the builtin, chdir()?

<http://perldoc.perl.org/functions/chdir.html>

--
Bart.
Peter J. Holzer

2006-03-27, 7:00 pm

Bart Lateur wrote:
> single word command lines do not use the shell, they try to directly
> call the program.
>
> And there is no program called "cd", as it is a built-in from the
> shell.


Actually, POSIX requires that a program called "cd" exists. It does
exist on HP-UX and there is probably some obscure package in your
favourite Linux distribution which contains it (which nobody ever
installs because "cd" as a command is extremely useless).

hp

--
_ | Peter J. Holzer | Löschung von at.usenet.schmankerl?
|_|_) | Symin WSR/LUGA |
| | | hjp@hjp.at | Diskussion derzeit in at.usenet.gruppen
__/ | http://www.hjp.at/ |
Dan Mercer

2006-03-27, 9:59 pm


"Peter J. Holzer" <hjp-usenet2@hjp.at> wrote in message news:aqk90e.3pk.ln@teal.hjp.at...
: Bart Lateur wrote:
: > single word command lines do not use the shell, they try to directly
: > call the program.
: >
: > And there is no program called "cd", as it is a built-in from the
: > shell.
:
: Actually, POSIX requires that a program called "cd" exists. It does
: exist on HP-UX and there is probably some obscure package in your
: favourite Linux distribution which contains it (which nobody ever
: installs because "cd" as a command is extremely useless).

It is useful in find. Can't think of anything else.

Dan Mercer

:
: hp
:
: --
: _ | Peter J. Holzer | LC6schung von at.usenet.schmankerl?
: |_|_) | Symin WSR/LUGA |
: | | | hjp@hjp.at | Diskussion derzeit in at.usenet.gruppen
: __/ | http://www.hjp.at/ |


Anno Siegel

2006-03-28, 4:00 am

Dan Mercer <dmercer@mn.rr.com> wrote in comp.lang.perl.misc:
>
> "Peter J. Holzer" <hjp-usenet2@hjp.at> wrote in message
> news:aqk90e.3pk.ln@teal.hjp.at...
> : Bart Lateur wrote:
> : > single word command lines do not use the shell, they try to directly
> : > call the program.
> : >
> : > And there is no program called "cd", as it is a built-in from the
> : > shell.
> :
> : Actually, POSIX requires that a program called "cd" exists. It does
> : exist on HP-UX and there is probably some obscure package in your
> : favourite Linux distribution which contains it (which nobody ever
> : installs because "cd" as a command is extremely useless).
>
> It is useful in find. Can't think of anything else.


Changing your current directory is useful, not only in find. A
*program* that does nothing but change its directory is useless
because it has no effect on the calling process.

Does POSIX indeed demand the existence of a stand-alone cd command?
Perldoc POSIX doesn't mention it.

Anno
--
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
Peter J. Holzer

2006-03-28, 7:00 pm

Anno Siegel wrote:
> Dan Mercer <dmercer@mn.rr.com> wrote in comp.lang.perl.misc:
>
> Changing your current directory is useful, not only in find. A
> *program* that does nothing but change its directory is useless
> because it has no effect on the calling process.


The calling process can use the exit code of the cd program to check
whether the parameter is a directory which it can chdir into. So

find / -exec cd {} \; -print

prints all directories accessible to the invoking user. Whether that
is useful, is debatable. I never needed it in 20 years of using Unix,
but requirements differ.


> Does POSIX indeed demand the existence of a stand-alone cd command?


Yes, see
http://www.opengroup.org/onlinepubs....html#tag_01_13

| Table: Regular Built-In Utilities
[...]
cd
[...]
| However, all of the standard utilities, including the regular
| built-ins in the table, but not the special built-ins described in
| Special Built-In Utilities , shall be implemented in a manner so that
| they can be accessed via the exec family of functions as defined in
| the System Interfaces volume of IEEE Std 1003.1-2001 and can be
| invoked directly by those standard utilities that require it ( env,
| find, nice, nohup, time, xargs).

(cd is one of the regular builtins)

> Perldoc POSIX doesn't mention it.


POSIX.pm contains bindings to POSIX library functions, not to POSIX
commands.

hp

--
_ | Peter J. Holzer | Löschung von at.usenet.schmankerl?
|_|_) | Symin WSR/LUGA |
| | | hjp@hjp.at | Diskussion derzeit in at.usenet.gruppen
__/ | http://www.hjp.at/ |
Anno Siegel

2006-03-29, 4:00 am

Peter J. Holzer <hjp-usenet2@hjp.at> wrote in comp.lang.perl.misc:
> Anno Siegel wrote:
>
> The calling process can use the exit code of the cd program to check
> whether the parameter is a directory which it can chdir into. So
>
> find / -exec cd {} \; -print
>
> prints all directories accessible to the invoking user. Whether that
> is useful, is debatable. I never needed it in 20 years of using Unix,
> but requirements differ.


Well... yes. I'd probably check permissions, but "cd" may be simpler.

>
> Yes, see
> http://www.opengroup.org/onlinepubs....html#tag_01_13
>
> | Table: Regular Built-In Utilities
> [...]
> cd
> [...]
> | However, all of the standard utilities, including the regular
> | built-ins in the table, but not the special built-ins described in
> | Special Built-In Utilities , shall be implemented in a manner so that
> | they can be accessed via the exec family of functions as defined in
> | the System Interfaces volume of IEEE Std 1003.1-2001 and can be
> | invoked directly by those standard utilities that require it ( env,
> | find, nice, nohup, time, xargs).
>
> (cd is one of the regular builtins)


Thanks for the quotation.

I can't help feeling that in extending the requirement to a whole list,
one or the other function may have slipped in that doesn't make much
sense as a stand-alone program.

Anno
--
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
Dan Mercer

2006-03-29, 7:00 pm


"Anno Siegel" <anno4000@lublin.zrz.tu-berlin.de> wrote in message news:48se8qFl472lU1@news.dfncis.de...
: Dan Mercer <dmercer@mn.rr.com> wrote in comp.lang.perl.misc:
: >
: > "Peter J. Holzer" <hjp-usenet2@hjp.at> wrote in message
: > news:aqk90e.3pk.ln@teal.hjp.at...
: > : Bart Lateur wrote:
: > : > single word command lines do not use the shell, they try to directly
: > : > call the program.
: > : >
: > : > And there is no program called "cd", as it is a built-in from the
: > : > shell.
: > :
: > : Actually, POSIX requires that a program called "cd" exists. It does
: > : exist on HP-UX and there is probably some obscure package in your
: > : favourite Linux distribution which contains it (which nobody ever
: > : installs because "cd" as a command is extremely useless).
: >
: > It is useful in find. Can't think of anything else.
:
: Changing your current directory is useful, not only in find. A
: *program* that does nothing but change its directory is useless
: because it has no effect on the calling process.

In find it is a cheap way to make sure you can change to a directory -
equivalent to "-exec sh -c 'cd $1' {} {}" - and much clearer as well.
The -exec switch does not use a shell, so without /bin/cd then
"-exec cd {}" would fail disastrously. So it does have a use -
I've used it myself.

Dan Mercer
:
: Does POSIX indeed demand the existence of a stand-alone cd command?
: Perldoc POSIX doesn't mention it.
:
: Anno
: --
: If you want to post a followup via groups.google.com, don't use
: the broken "Reply" link at the bottom of the article. Click on
: "show options" at the top of the article, then click on the
: "Reply" at the bottom of the article headers.


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com