【发布时间】:2022-09-11 14:11:16
【问题描述】:
File 1 : sampleInputFile
Name, Xloc, YLoc, Zloc
John, 10.5, 80.1, 5.80
Mary, 30.4, 20.5, 9.20
Paul, 10, 1000.0, 10.6
File 2 : proj01.sh (bash script file)
File 3 : correspondingOutputFile (output would shown in this file)
If input cmd : proj01.sh sampleInputFile 1 (In 3rd param, 1 is john, 2 is Mary, 3 is Paul)
Format of the output : Mary 62.9 (as Mary is near to John, and we can round that to 63.0 also)
**challenge** :
1) Check if filename passed in 1st parameter exists, if not stop with some message.
2) check that the index passed in 2nd parameter is an integer and is valid, which means is it less or equally to the number of entries and > 0.
3) bash script (proj01.sh) is going to find the min dist from the index person to another person.
calculate distances using : d = sqrt ( (x1-x2)^2 + (y1-y2)^2 + (z1-z2)^2 )
注意:所有文件都在同一个目录中。
试图读取值,但不确定如何通过给定的索引行比较所有行:
{
read
while IFS=, read -r n x y z
do
echo "n: $n x: $x y: $y z: $z"
done
} < $inputFile
我非常感谢任何形式的帮助。
【问题讨论】:
-
shell 不能进行任何浮点计算,因此您的脚本将需要使用像
awk这样的外部工具,这意味着最好的方法是将整个逻辑编写在awk中,然后使用 shell 进行调用awk一次
标签: linux bash ubuntu coordinates nearest-neighbor