“ls:未找到"运行“读取路径"后 [英] "ls: not found" after running "read PATH"
问题描述
我正在尝试编写一个小脚本来列出给定变量中的目录.但是,在将输入读入变量 PATH
后,我根本无法运行 ls
.
I'm trying to write a little script to list a directory from a given variable.
However, I can't run ls
at all after reading my input into the variable PATH
.
#!/system/bin/sh
echo "enter directory for listing"
read "PATH"
ls "$PATH" -R > list.txt
退出:
ls: not found
...并且不向 list.txt
写入任何内容.
...and writes nothing to list.txt
.
推荐答案
变量名 PATH
已被保留用于不同的目的:它列出了所有可能的位置搜索以查找未内置到壳.
The variable name PATH
is already reserved for a different purpose: It lists all the possible locations searched to find commands not built into the shell.
ls
就是这样一个命令.因此,当您更改 PATH
的值时,您会更改 shell 尝试查找 ls
可执行文件的方式;除非 PATH
的新值包含一个带有 ls
可执行文件的目录,否则任何进一步尝试运行 ls
(或其他未内置的命令)外壳)将失败.
ls
is such a command. Thus, when you change the value of PATH
, you change the way the shell tries to look for the ls
executable; unless the new value of PATH
includes a directory with a ls
executable in it, any further attempts to run ls
(or other commands not built into the shell) will fail.
相反,使用不同的变量名称 - 理想情况下,至少包含一个小写字符,以避免与(全大写)内置变量和环境变量发生冲突.
Instead, use a different variable name -- ideally, including at least one lower-case character, to avoid conflict with (all-uppercase) builtins and environment variables.
因此,一种更正的形式可能是:
Thus, one corrected form might be:
#!/system/bin/sh
echo "enter directory for listing"
IFS= read -r path
ls -R -- "$path" > list.txt
请注意,在这种情况下,-R
被移动到 "$path"
之前——而 GNU 系统允许可选参数在位置参数之后,许多较旧的UNIX 系统只会将标志(如 -R
)视为有效,如果它们在第一个非标志/选项参数之前被找到.
Note that the -R
is moved before the "$path"
in this case -- while GNU systems will allow optional arguments to be after positional arguments, many older UNIX systems will only treat flags (like -R
) as valid if they're found before the first non-flag/option argument.
这篇关于“ls:未找到"运行“读取路径"后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!