Performance 我似乎不能正常运行这个。。。它阻塞并且不显示输出
这是我的剧本:Performance 我似乎不能正常运行这个。。。它阻塞并且不显示输出,performance,bash,loops,while-loop,Performance,Bash,Loops,While Loop,这是我的剧本: while [[ $startTime -le $endTime ]] do thisfile=$(find * -type f | xargs grep -l $startDate | xargs grep -l $startTime) fordestination=`cut -d$ -f2 $thisfile | xargs cut -d ~ -f4` echo $fordestination startTime=$(( $startTime + 1 )) done
while [[ $startTime -le $endTime ]]
do
thisfile=$(find * -type f | xargs grep -l $startDate | xargs grep -l $startTime)
fordestination=`cut -d$ -f2 $thisfile | xargs cut -d ~ -f4`
echo $fordestination
startTime=$(( $startTime + 1 ))
done
我想你的cut和grep命令可能会被卡住。通过使用
[-n“$string”]
命令查看$string
是否为空,您可能应该确保它们的参数不是空的。在您的情况下,如果它是空的,它不会向命令中添加任何文件,以便以后使用它,这意味着该命令可能会等待来自命令行的输入(例如:如果$string
为空,并且您执行了grep regex$string
,grep将不会从$string
接收输入文件,而是等待来自命令行的输入)。下面是一个“复杂”版本,尝试显示可能出现错误的地方:
while [[ $startTime -le $endTime ]]
do
thisfile=$(find * -type f)
if [ -n "$thisfile" ]; then
thisfile=$(grep -l $startDate $thisfile)
if [ -n "$thisfile" ]; then
thisfile=$(grep -l $startTime $thisfile)
if [ -n "$thisfile" ]; then
thisfile=`cut -d$ -f2 $thisfile`
if [ -n "$thisfile" ]; then
forDestination=`cut -d ~ -f4 $thisfile`
echo $fordestination
fi
fi
fi
fi
startTime=$(( $startTime + 1 ))
done
这里有一个更简单的版本:
while [[ $startTime -le $endTime ]]
do
thisfile=$(grep -Rl $startDate *)
[ -n "$thisfile" ] && thisfile=$(grep -l $startTime $thisfile)
[ -n "$thisfile" ] && thisfile=`cut -d$ -f2 $thisfile`
[ -n "$thisfile" ] && cut -d ~ -f4 $thisfile
startTime=$(( $startTime + 1 ))
done
“-R”告诉grep递归搜索文件,&&
告诉bash仅在命令成功之前执行它后面的命令,并且&&
前面的命令是测试命令(在if
s中使用)
希望这有帮助=)它获取用户日期和时间输入,然后在匹配这些输入和输出$fordestination时查找特定的参数。请问printf“%s\\n”有什么用?当然。很抱歉编辑过度,我误解了您的意图,因此带有
printf
的第一个版本有点不正确。printf
命令类似于C中的printf函数。它被用来代替echo“$bla”
,因为如果$bla
以连字符(-
)开头,echo将解释为一个选项,而不是要打印的字符串。这就是为什么当要打印的字符串以变量开头时,使用printf%s//n“$bla”
更安全。