Bash“;没有这样的文件或目录”;但它确实存在
我有这个文件夹结构Bash“;没有这样的文件或目录”;但它确实存在,bash,Bash,我有这个文件夹结构 myFolder - script.sh - test.txt 在我的script.sh中,我编写了以下代码: filepath = "test.txt" while read line || [ -n "$line" ] do this this this done < filepath 但是test.txt包含一行 为什么我会有这个错误?Bash实际上对空间非常敏感。此外,您需要确保将filepath引用为$filepath # no spaces
myFolder
- script.sh
- test.txt
在我的script.sh中,我编写了以下代码:
filepath = "test.txt"
while read line || [ -n "$line" ]
do
this this this
done < filepath
但是test.txt
包含一行
为什么我会有这个错误?Bash实际上对空间非常敏感。此外,您需要确保将filepath引用为
$filepath
# no spaces around the equals sign
filepath="test.txt"
while read line || [ -n "$line" ]
do
this this this
done < $filepath
# note the added $ above
#等号周围没有空格
filepath=“test.txt”
读第| |[-n“$line”]
做
这个这个
完成<$filepath
#注意上面添加的$
您是否有名为filepath
的命令?您已经编写了filepath=“test.txt”
,但它运行命令filepath
,并带有两个参数:=
和test.txt
(因为shell删除了双引号)。此外,重定向还会查找文件filepath
(再次);您可能打算编写$filepath
,它将查看变量中的值(如果变量已设置)。运行你的代码,看看它是怎么说的。一般来说,最好在重定向中使用双引号括住“$filepath”
,尽管显示的固定名称并不重要(没有要保留的空格等).请参阅--以前多次询问和回答的问题应作为副本关闭,以便其答案可以集中起来,而不是分散在整个知识库中。(虽然不是我的否决票——虽然最好不要添加这样的答案,但如果答案准确/在其他方面可以接受,那么否决这些答案也是不被接受的做法。也就是说,如果已知的重复答案与“社区维基”标志集一起添加,那么就不太像是试图为代表玩游戏了).找到正确的副本是一个完整的PITA。我想知道政府是否会改进识别副本的工具?
# no spaces around the equals sign
filepath="test.txt"
while read line || [ -n "$line" ]
do
this this this
done < $filepath
# note the added $ above