尽管正确输出,但是Bash脚本冻结

时间:2015-08-18 12:00:28

标签: bash awk scripting

我有一个文本文件,其中包含许多格式如下: Ford:Mondeo:1997:Blue:5

我试图通过bash脚本排序大约100个,我想要提取1994年到1999年之间生产的所有汽车。这是我到目前为止所拥有的:

awk -F: '$3=="1994"' | awk -F: '$3<="1999"' $CARFILE > output/1994-1999.txt

输出文件包含所有正确的信息,没有重复信息等,但它会冻结,之后不会回显确认信息。我必须ctrl + D离开剧本。

以下是完整的参考文件:

#CS101 Assignment BASH script

#--help option
#case $1 in
# --help | carslist.txt)
#    cat <<-____HALP
#   Script name: ${0##*/} [ --help | carslist.txt ]
#   This script will organise the given text file and save various #sections to new files.
# No continuation checks are used but when each part is finished, a #confirmation message will print before the script continues.
#____HALP
#        exit 0;;
#esac

CARFILE=$1
while [ ! -f "$CARFILE" ]
do
echo "We cannot detect a car file to load, please enter the new filename and press [ENTER]"
read CARFILE
done
echo "We have detected that you're using $CARFILE as your cars file, please continue."

if [ -f output ]
then
echo "Sorry, a file called 'output' exists in the working directory. The script will now exist."
elif [ -d output ]
then
echo "The directory 'output' has been detected, instead of creating a new one we'll be working in there instead."
else
mkdir output
echo "We couldn't find an existing file or directory named 'output' so we've made one for you. Aren't we generous?"
fi
grep 'Vauxhall' $CARFILE > output/Vauxhall_Cars.txt
echo "We've saved all Vauxhall information in the 'Vauxhall_Cars.txt' file. The script will now continue."
grep '2001' $CARFILE > output/Manufactured_2001.txt
echo "We've saved all cared manufactured in 2001 in the 'Manufactured_2001.txt' file. The script will now continue."
awk -F: '$1=="Volkswagen" && $4=="Blue"' $CARFILE > output/Blue_Volkswagen.txt
echo "We've saved all Blue Volkswagens cars in Blue_Volkswagen.txt. The script will now continue"
grep 'V' $CARFILE > output/Makes_V.txt
echo "All cars with the make starting with 'V' have been saved in Makes_V.txt. The script will now continue."
awk -F: '$3=="1994"' | awk -F: '$3<="1999"' $CARFILE > output/1994-1999.txt
echo "Cars made between 1994 and 1999 have been saved in 1994-1999.txt. The script will now continue."

运行命令为bash myScript.sh carslist.txt

有人能告诉我为什么输出正确后它会冻结吗?

Just noticed that a record of 1993 has slipped through the cracks, is there a way of formatting the dates in the line above so it's only between 1994-1999?

提前致谢。

1 个答案:

答案 0 :(得分:2)

表达式:

awk -F: '$3=="1994"' | awk -F: '$3<="1999"' $CARFILE > output/1994-1999.txt

意思是:在&#34;&#34;&#34;上运行awk然后管道到另一个awk。但是你没有提供任何&#34;某些东西&#34;,所以awk正在等待它。

这就像说:

awk '{print}' | awk 'BEGIN {print 1}'

它确实打印了1但等待某种输入。

加入条件:

awk -F: '$3=="1994" && $3<="1999"' $CARFILE > output/1994-1999.txt

关于脚本的其余部分:请注意,您没有使用多个双引号。当你有空格等名称时,它们是一个很好的做法,可以防止出现问题。例如,你可以说grep 'Vauxhall' "$CARFILE"并允许$CARFILE包含类似&#34;我的汽车&#34;。< / p>

您可以通过在ShellCheck中粘贴脚本来找出这类错误。