以下代码旨在查找~/Downloads
. 我用. ./script.sh
. 即使用户提交的名称不完整,它也会找到它们。
#!/usr/bin/bash
echo -e "\nGive me the name of a nonexitent directory and I will look for it in ~/Downloads?\n"
read word
TOGOs=("$(find ~/Downloads -maxdepth 1 -iname "*$word*" -type d -execdir echo {} + | sed 's;./;/home/smith/Downloads/;g')"
"")
for togo in ${TOGOs[@]}
do
if [[ $togo != "" ]]; then
echo $togo
export togo && cd $togo && return 0
else
echo "Haven't found it in ~/Downloads ." && cd ~/Downloads #This line does not work
fi
done
该if
部分按预期工作 - 当我给它一个名称/子目录名称的一部分时~/Downloads/
,但是else
当我给它一个不存在的目录时,该块的部分永远不会被执行。else
当我摆脱循环时,我可以执行该部分,如下所示:
#!/usr/bin/bash
echo -e "\nGive me the name of a nonexitent directory and I will look for it in ~/Downloads?\n"
read word
TOGO=$(find ~/Downloads -maxdepth 1 -iname "*$word*" -type d -execdir echo {} + | sed 's;./;/home/smith/Downloads/;g')
if [[ $TOGO != "" ]]; then
echo $TOGO
export TOGO
cd $TOGO && return 0
else
echo "Haven't found it in ~/Downloads." && cd ~/Downloads
fi
为什么else
当我摆脱循环时手臂会被执行?如何在保留循环的同时执行我的代码?