问题描述
我正在尝试编写bash脚本以生成文件列表.我以为我会在一个循环中简单地调用查找".不幸的是,在循环中时,它生成了找不到:命令未找到"错误,我也不知道为什么.
I'm trying to write a bash script to generate lists of files. I figured I'd simply call 'find' within a loop. Unfortunately it generated a "find: command not found" error when in the loop and I don't know why.
为了简短起见,此精简版复制了该问题,而不会在不相关的代码中陷入困境.
To keep things short, this cut-down version replicates the issue without bogging us down in irrelevant code.
#!/bin/bash
IFSprev=$IFS
IFS=$'|'
PATHS='openvpn|vms'
SOURCE='/mnt/store/'
#find "${SOURCE}vms" -type f
for PATH in ${PATHS}
do
echo -----------------------------------
find "${SOURCE}${PATH}" -type f
done
IFS=$IFSprev
在进行故障排除时,我添加了第一个"find"命令……并添加了该命令,随后它在循环中起作用.如果我再次注释掉,循环中的查找"将恢复为抛出错误.
While troubleshooting, I added the first 'find' command... with that added, it subsequently works within the loop. If I comment it out again, the 'find' in the loop reverts to throwing the error.
鉴于我要将循环输出重定向到文件,我可以使用额外的"find"命令,甚至可以重定向到null.但是,我是一个好奇的家伙,我真的很想找出问题所在.
Given I'm gonna be redirecting the loop output to a file, I can live with the extra 'find' command or even just redirect to null. However, I'm an inquisitive kinda guy and I really want to find what the problem is.
感谢您的任何输入.
推荐答案
它正在$PATH
中寻找find
...您的脚本已被破坏.
It's looking for find
in $PATH
... which your script has destroyed.
这篇关于查找“找不到命令".在bash循环中执行时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!