我想在我的脚本中编写一个进度指示器函数,它循环“请等待”消息,直到完成调用它的任务.
我希望它是一个函数,以便我可以在其他脚本中重用它.
为了实现这一点,函数需要与其他函数松散耦合,即调用它的函数不必知道其内部代码.
这是我到目前为止所拥有的.此函数接收调用者的pid并循环,直到任务完成.
function progress() {
pid="$1"
kill -n 0 "${pid}" &> /dev/null && echo -ne "please wait"
while kill -n 0 "${pid}" &> /dev/null ; do
echo -n "."
sleep 1
done
}
在脚本中使用它时,它可以正常工作,例如:
#imports the shell script with the progress() function
. /path/to/progress.sh
echo "testing"
# $$returns the pid of the script.
progress $$&
sleep 5
echo "done"
$testing
$please wait.....
$done
问题是当我从另一个函数调用它时,因为函数没有pids:
function my_func() {
progress $$&
echo "my func is done"
}
. /path/to/progress.sh
echo "testing"
my_func
sleep 10
echo done
$testing
$please wait.....
$my func. is done.
$..........
$done