情况1

情况1

我正在读 ls 的 man,最后它谈到了 ls 的退出状态。它说:

   Exit status:
   0      if OK,
   1      if minor problems (e.g., cannot access subdirectory),
   2      if serious trouble (e.g., cannot access command-line argument).

但问题是我不明白他们的意思:

无法访问命令行参数

我从未遇到过无法访问传递给程序的参数的情况,并且我在网上搜索有关这种特定情况的信息,但除此之外我无法获得很多信息这个网站 这对我来说并不是很清楚,而且我无法重现该错误。我不确定我是否错过了理解 MAN 页面

答案1

对于 GNU ls,请使用源 Luke:http://git.savannah.gnu.org/gitweb/?p=coreutils.git;a=blob;f=src/ls.c;h=bf0c5941d7de699fc5a85d44461ef29192216d9d;hb=HEAD

你会发现很多情况下返回码都是2,有的很容易触发,如下图。

首先你可以读到:

 802 /* Exit statuses.  */
 803 enum
 804   {
 805     /* "ls" had a minor problem.  E.g., while processing a directory,
 806        ls obtained the name of an entry via readdir, yet was later
 807        unable to stat that name.  This happens when listing a directory
 808        in which entries are actively being removed or renamed.  */
 809     LS_MINOR_PROBLEM = 1,
 810 
 811     /* "ls" had more serious trouble (e.g., memory exhausted, invalid
 812        option or failure to stat a command line argument.  */
 813     LS_FAILURE = 2
 814   };

因此您已经可以看到,值 2 涵盖的情况比文档中编写的情况更多。

然后,如果您在代码中进一步搜索,LS_FAILURE您会发现不同的情况:

情况1

1896         case 'w':
1897           if (! set_line_length (optarg))
1898             die (LS_FAILURE, 0, "%s: %s", _("invalid line width"),
1899                  quote (optarg));
1900           break;

set_line_length将根据xstrtoumax给定宽度的返回方式做出反应。如果你仔细查看它的源代码,你可以得到一些边缘情况:

$ ls -w -1 >& /dev/null
$ echo $?
2
$ ls -w 1 >& /dev/null
$ echo $?
0

案例2

1964         case 'T':
1965           tabsize = xnumtoumax (optarg, 0, 0, SIZE_MAX, "",
1966                                 _("invalid tab size"), LS_FAILURE);
1967           break;

与之前的案例类似:

$ ls -T 1 >& /dev/null
$ echo $?
0
$ ls -T -1 >& /dev/null
$ echo $?
2

案例3

2106         default:
2107           usage (LS_FAILURE);

因此,如果您提供无效参数,这就是默认错误代码。看这个例子:

$ ls --unknown-option >& /dev/null
$ echo $?
2

案例4

2198               if (strchr (p1 + 1, '\n'))
2199                 die (LS_FAILURE, 0, _("invalid time style format %s"),
2200                      quote (p0));

当您提供无效的时间格式(有两个)时,就会发生这种情况\n

$ ls -l --time-style=+%T >& /dev/null ; echo $?
0
$ ls -l --time-style=+%T$'\n' >& /dev/null ; echo $?
0
$ ls -l --time-style=+%T$'\n'%T >& /dev/null ; echo $?
0
$ ls -l --time-style=+%T$'\n'%T$'\n' >& /dev/null ; echo $?
2

案例5

2218               /* The following is a manual expansion of argmatch_valid,
2219                  but with the added "+ ..." description and the [posix-]
2220                  prefixes prepended.  Note that this simplification works
2221                  only because all four existing time_style_types values
2222                  are distinct.  */
2223               fputs (_("Valid arguments are:\n"), stderr);
2224               char const *const *p = time_style_args;
2225               while (*p)
2226                 fprintf (stderr, "  - [posix-]%s\n", *p++);
2227               fputs (_("  - +FORMAT (e.g., +%H:%M) for a 'date'-style"
2228                        " format\n"), stderr);
2229               usage (LS_FAILURE);

使用无效的时间格式名称时触发:

$ LANG=C ls -l --time-style=whatever 
ls: invalid argument 'whatever' for 'time style'
Valid arguments are:
  - [posix-]full-iso
  - [posix-]long-iso
  - [posix-]iso
  - [posix-]locale
  - +FORMAT (e.g., +%H:%M) for a 'date'-style format
Try 'ls --help' for more information.

$ echo $?
2

案例6

2669 static void
2670 set_exit_status (bool serious)
2671 {
2672   if (serious)
2673     exit_status = LS_FAILURE;
2674   else if (exit_status == EXIT_SUCCESS)
2675     exit_status = LS_MINOR_PROBLEM;
2676 }

这种情况(严重 = true)可能会在多种情况下发生,例如,如果某处存在循环:

2747       /* If we've already visited this dev/inode pair, warn that
2748          we've found a loop, and do not process this directory.  */
2749       if (visit_dir (dir_stat.st_dev, dir_stat.st_ino))
2750         {
2751           error (0, 0, _("%s: not listing already-listed directory"),
2752                  quotef (name));
2753           closedir (dirp);
2754           set_exit_status (true);
2755           return;
2756         }

根据争论,这种情况也可能发生在许多其他情况下。 file_failure第一个参数是传递给的布尔值set_exit_status

子情况A

2710 /* Read directory NAME, and list the files in it.
2711    If REALNAME is nonzero, print its name instead of NAME;
2712    this is used for symbolic links to directories.
2713    COMMAND_LINE_ARG means this directory was mentioned on the command line.  */

...

2725   if (!dirp)
2726     {
2727       file_failure (command_line_arg, _("cannot open directory %s"), name);
2728       return;
2729     }

例如:

$ ls /thatDOESnotEXIST >& /dev/null
$ echo $?
2

子情况B

2736       /* If dirfd failed, endure the overhead of using stat.  */
2737       if ((0 <= fd
2738            ? fstat (fd, &dir_stat)
2739            : stat (name, &dir_stat)) < 0)
2740         {
2741           file_failure (command_line_arg,
2742                         _("cannot determine device and inode of %s"), name);

这是某种无法访问的目录(例如远程目录)。

子情况C

2771       if (print_hyperlink)
2772         {
2773           absolute_name = canonicalize_filename_mode (name, CAN_MISSING);
2774           if (! absolute_name)
2775             file_failure (command_line_arg,
2776                           _("error canonicalizing %s"), name);

或者

3189       if (print_hyperlink)
3190         {
3191           f->absolute_name = canonicalize_filename_mode (full_name,
3192                                                          CAN_MISSING);
3193           if (! f->absolute_name)
3194             file_failure (command_line_arg,
3195                           _("error canonicalizing %s"), full_name);

或者

3450 static void
3451 get_link_name (char const *filename, struct fileinfo *f, bool command_line_arg)
3452 {
3453   f->linkname = areadlink_with_size (filename, f->stat.st_size);
3454   if (f->linkname == NULL)
3455     file_failure (command_line_arg, _("cannot read symbolic link %s"),
3456                   filename);
3457 }

这些是某种损坏的硬/软链接。

子情况D

2836       else if (errno != 0)
2837         {
2838           file_failure (command_line_arg, _("reading directory %s"), name);

或者

2851   if (closedir (dirp) != 0)
2852     {
2853       file_failure (command_line_arg, _("closing directory %s"), name);

另一种情况是无法读取目录内容(如果在命令行上提供)

子情况E

3235       if (err != 0)
3236         {
3237           /* Failure to stat a command line argument leads to
3238              an exit status of 2.  For other files, stat failure
3239              provokes an exit status of 1.  */
3240           file_failure (command_line_arg,
3241                         _("cannot access %s"), full_name);

当尝试匹配文件时会发生这种情况,例如:

$ ls '*DOESnotEXIST*' >& /dev/null
$ echo $?
2

答案2

是的,man ls(对于 GNU ls)包含:

2 如果出现严重问题(例如,无法访问命令行参数)。

也许,这个词access应该读作stat。由于不存在而无法进行 stat ( ) 的参数(文件)stat filename将生成错误代码2

简而言之,对于 GNU ls:命令如下:

ls   NONE_existent_file    # will result in exit 2

尝试:

ls nofile
ls: cannot access nofile: No such file or directory
echo $?
2

或者尝试:

$ ls $(date); echo "exit status: $?"
ls: cannot access 'Wed': No such file or directory
ls: cannot access 'Oct': No such file or directory
ls: cannot access '24': No such file or directory
ls: cannot access '02:42:02': No such file or directory
ls: cannot access 'UTC': No such file or directory
ls: cannot access '2018': No such file or directory
exit status: 2

相关内容