我创建了一个名为dir1的新目录
然后用触摸命令添加苹果、香蕉、胡萝卜、枣、蛋、鱼、葡萄火腿。
之后,我创建了文件Wildcards.sh (顺便说一句,我使用了nano来创建文件)。
#!/bin/bash
# This script will include wildcards
find . dir1
echo The contents of dir1 are:$find
echo然后我执行它来测试它是否有效。是的,但我不想这样。
我像./Wildcards.sh一样运行它
得到了
.
./ham
./egg
./grape
./date
./Wildcards.sh
./apple
./fish
./carrot
./banana
find: 'dir1': No such file or directory
The contents of dir1 are:应该像这样输出吗:The contents of dir1 are: apple banana carrot date egg fish grape ham
请帮我找出我的错误?
发布于 2020-07-18 18:47:22
当dir1是您的工作目录时,您无法找到dir1。您从未在变量"$find“中添加任何内容。
FIND=$(cd ~/dir1; echo *)
echo "The contents of dir1 are: $FIND"这里发生的是,您将主文件夹中的工作目录更改为dir1,然后让bash显示所有未隐藏的内容("*")。结果保存在变量FIND中(但是如果有错误,它们也会保存在FIND中)。如果您也想要隐藏的文件和目录,则如下所示:
FIND=$(cd ~/dir1; echo .* *)发布于 2020-07-18 19:27:12
给你,试试
result=$(find ~/dir1)
echo "The contents of dir1 are: $result"或
echo "The contents of dir1 are: $(find ~/dir1)"(如果我说的不正确,我对Bash不是最好的) result=$(find ~/dir1)运行find ~/dir1,然后将其存储在result中,$(command)运行()s内部的命令,并使用命令的STDOUT(标准输出/命令输出)作为临时变量,您也可以使用${result}代替D9,这允许使用echo "${result}asdf"这样的场景。希望这能有所帮助!请原谅我的格式不正确,这是我的第一篇文章:P
发布于 2020-07-18 21:22:52
你的剧本有更正。
#!/bin/bash
# This script has no wildcards
find_output="$(find dir1)"
echo 'The contents of dir1 are:'"$find_output"
echo我想补充另一个答案,因为所有现有的答案都有错误。
您的一些错误是:
find不需要.作为它的第一个参数。是的,经常是这样,但是…Shell变量应该是小写。这是有标准的。如果你把它们作为大写,你会时不时地收到意想不到的错误。
https://unix.stackexchange.com/questions/599188
复制相似问题