Bash 尽管输出正确,脚本仍然冻结
Bash script freezing despite outputting correctly
我有一个包含大量记录的文本文件,格式如下:
Ford:Mondeo:1997:Blue:5
我试图通过 bash 脚本对其中大约 100 辆汽车进行排序,我希望提取 1994 年至 1999 年间制造的所有汽车。这是我目前所拥有的:
awk -F: '=="1994"' | awk -F: '<="1999"' $CARFILE > output/1994-1999.txt
输出文件包含所有正确的信息,没有重复等,但它冻结并且之后没有回显确认。我必须 ctrl + D
摆脱脚本。
完整文件供参考:
#CS101 Assignment BASH script
#--help option
#case 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=
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: '=="Volkswagen" && =="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: '=="1994"' | awk -F: '<="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?
提前致谢。
表达式:
awk -F: '=="1994"' | awk -F: '<="1999"' $CARFILE > output/1994-1999.txt
表示:运行 awk
在 "something" 上,然后通过管道传输到另一个 awk
。但是您没有提供任何 "something",所以 awk
正在等待它。
这就像在说:
awk '{print}' | awk 'BEGIN {print 1}'
它确实打印了 1
但等待某种输入的到来。
刚加入条件:
awk -F: '=="1994" && <="1999"' $CARFILE > output/1994-1999.txt
关于脚本的其余部分:请注意,您没有使用很多双引号。当您的名称带有空格等时,它们是防止出现问题的好习惯。例如,您可以说 grep 'Vauxhall' "$CARFILE"
并允许 $CARFILE
包含诸如 "my car".
之类的内容
您可以通过将脚本粘贴到 ShellCheck 来找出此类错误。
我有一个包含大量记录的文本文件,格式如下:
Ford:Mondeo:1997:Blue:5
我试图通过 bash 脚本对其中大约 100 辆汽车进行排序,我希望提取 1994 年至 1999 年间制造的所有汽车。这是我目前所拥有的:
awk -F: '=="1994"' | awk -F: '<="1999"' $CARFILE > output/1994-1999.txt
输出文件包含所有正确的信息,没有重复等,但它冻结并且之后没有回显确认。我必须 ctrl + D
摆脱脚本。
完整文件供参考:
#CS101 Assignment BASH script
#--help option
#case 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=
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: '=="Volkswagen" && =="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: '=="1994"' | awk -F: '<="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?
提前致谢。
表达式:
awk -F: '=="1994"' | awk -F: '<="1999"' $CARFILE > output/1994-1999.txt
表示:运行 awk
在 "something" 上,然后通过管道传输到另一个 awk
。但是您没有提供任何 "something",所以 awk
正在等待它。
这就像在说:
awk '{print}' | awk 'BEGIN {print 1}'
它确实打印了 1
但等待某种输入的到来。
刚加入条件:
awk -F: '=="1994" && <="1999"' $CARFILE > output/1994-1999.txt
关于脚本的其余部分:请注意,您没有使用很多双引号。当您的名称带有空格等时,它们是防止出现问题的好习惯。例如,您可以说 grep 'Vauxhall' "$CARFILE"
并允许 $CARFILE
包含诸如 "my car".
您可以通过将脚本粘贴到 ShellCheck 来找出此类错误。