What is this error?

B

Binny V A

Hello Everyone,

I have made many shell scripts using perl. Some of them are available
at http://www.geocities.com/binnyva/code/perl/shell_scripts/index.html.
When I user perl to create shell scripts, I get a error message that I
don't understand.

It appears when this code is used

# Get a list of all matching files
my $list;
$list = `dir /b *.htm`;
my @LIST = split(/\s+/, $list);

This script shows the error...
dir: /b: No such file or directory (ENOENT)

Anyway the script works fine and does everything I would expect of it
- except for showing the error. I just want to know what that error is
and how to avoid it.

Thank You,
Binny V A.
http://www.geocities.com/binnyva
 
S

Sherm Pendley

Binny said:
# Get a list of all matching files
my $list;
$list = `dir /b *.htm`;
my @LIST = split(/\s+/, $list);

This script shows the error...
dir: /b: No such file or directory (ENOENT)

The "dir" command is often aliased to "ls -l" - that's a fairly common
alias on UNIX systems, and the "-l" option gives the long-format listing
similar to that produced by "dir" on DOS.

But an alias is a simple beast; it does a simple text substitution, and
doesn't know how to translate the "/b" switch to the correct "ls"
option. So the result after the alias is expanded is "ls -l /b" - which
attempts to list the contents of a non-existant directory /b.

To correct the problem, use the UNIX "ls" command directly. Check the
man page for ls to see what the option is that corresponds to the DOS
"dir /b" option.

This has *nothing* to do with Perl, by the way - you'd get precisely the
same results calling "dir /b" from a bash, Python, or Ruby script, or a
compiled C/C++ program.

sherm--
 
M

Michael Carman

Binny said:
When I user perl to create shell scripts, I get a error message that
I don't understand.

If you get an error from _perl_ that you don't understand, look it up in
the perldiag manpage. (Type "perldoc perldiag" at a command prompt.)
$list = `dir /b *.htm`;
[...]
dir: /b: No such file or directory (ENOENT)

This is not a perl error, it's an error from the program that you're
calling (dir). If you don't understand it, you should read the
documentation (if any) for the program, or ask in a forum that discusses
the program.

At any rate, the error appears to be perfectly descriptive -- it's
telling you that there are no files matching the provided pattern: *.htm
Anyway the script works fine and does everything I would expect of it
- except for showing the error. I just want to know what that error is
and how to avoid it.

The reason the error isn't causing problems is that (as an error
message) it's being printed on STDERR -- backticks only capture STDOUT.
Maybe you should leave the error as-is. After all, it might be important
to the user that no matching files were found. If you really want to
suppress the error, you'll have to look up how to redirect STDERR for
your shell. (This is not a perl issue.)

For this particular case, the simple solution is to not go to the shell
at all. You can get the file directly within Perl:

my @LIST = glob('*.htm');

See 'perldoc -f glob' (type it at a command prompt) for more information.

-mjc
 
B

Brendon Caligari

Binny said:
Hello Everyone,

I have made many shell scripts using perl. Some of them are available
at http://www.geocities.com/binnyva/code/perl/shell_scripts/index.html.
When I user perl to create shell scripts, I get a error message that I
don't understand.

It appears when this code is used

# Get a list of all matching files
my $list;
$list = `dir /b *.htm`;
my @LIST = split(/\s+/, $list);

This script shows the error...
dir: /b: No such file or directory (ENOENT)

:) The '/b' parameter is valid on MS-DOS but not on Linux or whatever
is being used. Also, to list the directory contents use 'ls' over 'dir'

In perl you can open a directory with 'opendir' and read the contents
with 'readdir'.

perldoc -f readdir

B
 
M

Michele Dondi

# Get a list of all matching files
my $list;
$list = `dir /b *.htm`;
my @LIST = split(/\s+/, $list);

Whatever you're trying to do, you shouldn't do it like this! You
should use

my @list = <*.htm>;

instead.


Michele
 
J

Joe Smith

Binny said:
$list = `dir /b *.htm`;
my @LIST = split(/\s+/, $list);

This script shows the error...
dir: /b: No such file or directory (ENOENT)

DIR is a built-in command in COMMAND.EXE or CMD.EXE on Windows.

Some versions of perl for Windows have problems when using
backticks on built-ins.

Don't expect to run MS-DOS commands on Unix-type systems.

linux% /usr/bin/dir /b *.htm
/usr/bin/dir: /b: No such file or directory
index.htm invite.htm

linux% /usr/bin/dir -1 *.htm
index.htm
invite.htm

-Joe
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top