我读了“ls的人”,最后它谈到了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).
但问题是我不明白他们的意思:
无法访问命令行参数
我从来没有遇到过这样的情况:我无法访问传递给我的程序的参数,我在网上搜索这个特定的情况,除了本站之外,我找不到很多其他的信息,这对我来说不是很清楚,我也无法重现错误。我不确定我是否错过了理解手册页
发布于 2018-10-24 03:05:19
对于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
,就会发现不同的情况:
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
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
2106 default:
2107 usage (LS_FAILURE);
因此,如果提供无效的参数,则这是默认错误代码。参见此示例:
$ ls --unknown-option >& /dev/null
$ echo $?
2
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案例52218 /* 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案例62669 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 }这种情况(严重=真)可以在多种情况下发生,例如,如果某个地方有一个循环: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的布尔值。子案例A2710 /* 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子案例B2736 /* 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);这是一种无法访问的目录(比如远程目录)。子案例C2771 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 }这些都是某种破碎的硬/软链接。子案例D2836 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);另一种情况是无法读取目录内容(如果在命令行中提供)子案例E3235 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\n' >& /dev/null ; echo $?
0
$ ls -l --time-style=+%TK123案例5K224A25使用无效的时间格式名称时触发:A26K127案例6K228A29这种情况(严重=真)可以在多种情况下发生,例如,如果某个地方有一个循环:A30根据论点,在许多其他情况下也可能发生这种情况。D31第一个参数是传递给D32的布尔值。K133子案例AK234A35例如:A36K137子案例BK238A39这是一种无法访问的目录(比如远程目录)。K140子案例CK241A42或A43或A44这些都是某种破碎的硬/软链接。K145子案例DK246A47或A48另一种情况是无法读取目录内容(如果在命令行中提供)K149子案例EK250A51当试图匹配文件时会发生这种情况,例如:A52\n'%T >& /dev/null ; echo $?
0
$ ls -l --time-style=+%TK123案例5K224A25使用无效的时间格式名称时触发:A26K127案例6K228A29这种情况(严重=真)可以在多种情况下发生,例如,如果某个地方有一个循环:A30根据论点,在许多其他情况下也可能发生这种情况。D31第一个参数是传递给D32的布尔值。K133子案例AK234A35例如:A36K137子案例BK238A39这是一种无法访问的目录(比如远程目录)。K140子案例CK241A42或A43或A44这些都是某种破碎的硬/软链接。K145子案例DK246A47或A48另一种情况是无法读取目录内容(如果在命令行中提供)K149子案例EK250A51当试图匹配文件时会发生这种情况,例如:A52\n'%TK123案例5K224A25使用无效的时间格式名称时触发:A26K127案例6K228A29这种情况(严重=真)可以在多种情况下发生,例如,如果某个地方有一个循环:A30根据论点,在许多其他情况下也可能发生这种情况。D31第一个参数是传递给D32的布尔值。K133子案例AK234A35例如:A36K137子案例BK238A39这是一种无法访问的目录(比如远程目录)。K140子案例CK241A42或A43或A44这些都是某种破碎的硬/软链接。K145子案例DK246A47或A48另一种情况是无法读取目录内容(如果在命令行中提供)K149子案例EK250A51当试图匹配文件时会发生这种情况,例如:A52\n' >& /dev/null ; echo $?
2
K123
案例5K224A25
使用无效的时间格式名称时触发:
A26K127
案例6K228A29
这种情况(严重=真)可以在多种情况下发生,例如,如果某个地方有一个循环:
A30
根据论点,在许多其他情况下也可能发生这种情况。D31
第一个参数是传递给D32
的布尔值。
K133
子案例AK234A35
例如:
A36K137
子案例BK238A39
这是一种无法访问的目录(比如远程目录)。
K140
子案例CK241A42
或
A43
或
A44
这些都是某种破碎的硬/软链接。
K145
子案例DK246A47
或
A48
另一种情况是无法读取目录内容(如果在命令行中提供)
K149
子案例EK250A51
当试图匹配文件时会发生这种情况,例如:
A52
发布于 2018-10-24 02:42:33
是的,man ls
(对于GNU ls)包含:
如果出现严重问题(例如,无法访问命令行参数)。
也许,单词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
https://unix.stackexchange.com/questions/477390
复制相似问题