【发布时间】:2017-05-24 07:27:00
【问题描述】:
即使我的退出状态为 0,我的脚本似乎也无法运行
它一直有效,直到我用
makedrivetree ()
{
}
语法。
如果删除它并保留while 和done 之间的所有内容,它可以正常工作。
我已将它作为 makedrivetree 保存在我的 ~/bin 文件夹中
这是完整的功能:
#!/usr/bin/env bash
# a script to write the contents of a directory in tree form and save it to a .txt file
### CONSTANTS
DATE="$(date +%Y%m%d)"
### FUNCTIONS
makedrivetree ()
{
while [ "${*}" != "" ] ; do
#set $DRIVE as the first arugment sent to script
DRIVE="${1}"
#set $OUTPUT
OUTPUT="/Users/$USER/Desktop/$(basename "${DRIVE}")_contents_"${DATE}".txt"
#run tree
tree -I "*.dpx" --si --du -U -o "${OUTPUT}" "${DRIVE}"
#check $? for errors
if [ "$?" -eq "0" ] ; then
echo 'done. moving to next drive.'
else
echo 'error'
fi
shift
done
}
#call the function(s)
makedrivetree
echo 'makedrivetree exited with status' $?
exit
【问题讨论】:
-
你没有将参数传递给你的函数,这可能是问题所在。
-
嗨@Aaron你能详细说明或建议修复吗?谢谢!
-
哦,我明白了。当我调用该函数时,我必须执行
makedrivetree $@之类的操作。 -
是的,就是这样!
-
仅供参考 -- 而不是
tree ...; if [ "$?" -eq "0" ]; then ...,请考虑if tree ...; then-- 更短,更不容易出错(所以像额外的日志记录这样的东西不会破坏$?)。