我制作了两个程序,我试图从另一个程序中调用一个程序,但是这个程序出现在我的屏幕上:
cp: cannot stat ‘PerShip/.csv’: No such file or directory
cp: target ‘tmpship.csv’ is not a directory该怎么办呢。这是节目。有人能帮帮我吗?
#!/bin/bash
shipname=$1
imo=$(grep "$shipname" shipsNAME-IMO.txt | cut -d "," -f 2)
cp PerShip/$imo'.csv' tmpship.csv
dist=$(octave -q ShipDistance.m 2>/dev/null)
grep "$shipname" shipsNAME-IMO.txt | cut -d "," -f 2 > IMO.txt
idnumber=$(cut -b 4-10 IMO.txt)
echo $idnumber,$dist#!/bin/bash
rm -f shipsdist.csv
for ship in $(cat shipsNAME-IMO.txt | cut -d "," -f 1)
do
./FindShipDistance "$ship" >> shipsdist.csv
done
cat shipsdist.csv | sort | head -n 1发布于 2022-01-14 19:37:48
提供的代码和错误消息表明,第二个脚本使用一个空的命令行参数调用第一个脚本。如果输入文件shipsNAME-IMO.txt包含任何空行,或者包含空第一个字段的任何行,则肯定会发生这种情况。开头或结尾的空行就能做到这一点。
我建议
read命令读取数据,并操作IFS来解析以逗号分隔的read输入和其他数据(通常是G 212
例如:
#!/bin/bash
# Validate one command-line argument
[[ -n "$1" ]] || { echo empty ship name 1>&2; exit 1; }
# Read and validate an IMO corresponding to the argument
IFS=, read -r dummy imo tail < <(grep -F -- "$1" shipsNAME-IMO.txt)
[[ -f PerShip/"${imo}.csv" ]] || { echo no data for "'$imo'" 1>&2; exit 1; }
# Perform the distance calculation and output the result
cp PerShip/"${imo}.csv" tmpship.csv
dist=$(octave -q ShipDistance.m 2>/dev/null) ||
{ echo "failed to compute ship distance for '${imo}'" 2>&1; exit 1; }
echo "${imo:3:7},${dist}"和
#!/bin/bash
# Note: the original shipsdist.csv will be clobbered
while IFS=, read -r ship tail; do
# Ignore any empty ship name, however it might arise
[[ -n "$ship" ]] && ./FindShipDistance "$ship"
done < shipsNAME-IMO.txt |
tee shipsdist.csv |
sort |
head -n 1注意,使管道的第二个脚本中的while循环成为管道的一部分将导致它在子subshell中运行。这有时是个难题,但在这种情况下不会造成任何问题。
https://stackoverflow.com/questions/70714217
复制相似问题