我从一段用于查找特定子文件夹的脚本中摘录了以下内容。同时,旋转器指示后台某些操作需要更长时间:
# runs find as background task and meanwhile display spinner
# writes to temp file ./.repos
echo -ne "Scanning for .git folders ... "
find -name ".git" -type d > .repos &
pid=$!
while [ -d /proc/$pid ]
do
for x in '-' '/' '|' '\'
do
echo -ne "\b"$x
sleep 0.01
done
done
# read-in result from ./.repos and delete the file
echo ""
repos=$(cat .repos)
rm .repos
一切都很好,但我不喜欢仅仅为了获取找到的文件夹列表而写入/读取文件。
我的问题是:我怎样才能直接将其作为变量来执行?
我尝试过类似
echo -ne "Scanning for .git folders ... "
repos=$(find -name ".git" -type d &
pid=$!
while [ -d /proc/$pid ]
do
for x in '-' '/' '|' '\'
do
echo -ne "\b"$x
sleep 0.01
done
done
)
echo "$repos"
但这不起作用,意味着旋转器从未被打印出来,但最终被添加到字符串本身中$repos
。
所以我想我理解了这个问题并尝试
echo -ne "Scanning for .git folders ... "
repos=$(find -name ".git" -type d) &
pid=$!
while [ -d /proc/$pid ]
do
for x in '-' '/' '|' '\'
do
echo -ne "\b"$x
sleep 0.01
done
done
echo "$repos"
现在旋转器按预期工作,但最终$repos
是空的
答案1
这应该可以满足您的需求:
echo -ne "Scanning for .git folders ... "
(
while :; do
for x in '-' '/' '|' '\\'
do
echo -ne "\b"$x
sleep 0.01
done
done
) &
sub=$!
repos=$(find -name ".git" -type d)
kill $sub
echo -ne "\nFound repos: "
echo "$repos" | wc -l
反过来做可能更容易。