这是我的脚本:
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 如果它之前的命令成功,则只执行它后面的命令,并且之前的命令&&是测试命令(在ifs 中使用)。
希望这会有所帮助=)