Ksh 脚本无父错误

Ksh 脚本无父错误

我希望脚本打开一个文件并逐行读取文件,然后计算每行/行的逗号数量。如果超过 $2 参数值,则将有问题的行号(来自我的读取循环)和找到的总逗号写入日志文件。

我不确定出了什么问题,但我收到了无父错误。

#!/bin/ksh
filename=$1 #First input parameter path with filename
pipe=$2  #Total Pipe Value

#Filename to parse
if [ $filename -ne $1 ]
then
   echo "Filename required"
   exit 1
fi

#Check pipe/comma
if [ $pipe -ne $2 ]
then
   echo "Filename and number of expected pipes required"
   exit 1
fi
if [ -f $1 ]
then
while read -r line
do
((i+=1))
count=${line//[^|]}
echo Line#"$i:" "${#count}" pipe per line compare to "$2" expected
done <$filename
fi
if [ $count > $2 ]
then
echo Line# "$i" has "${#count}" pipe in total > uhs_sm_qa_csv.ksh.log
fi
exit 0

输出script

[root@uhspaastream01 scripts]# ksh uhs_sm_qa_csv.ksh test.txt 10
uhs_sm_qa_csv.ksh[6]: [: test.txt: no parent
Line#1: 1 pipe per line compare to 10 expected
Line#2: 1 pipe per line compare to 10 expected
Line#3: 1 pipe per line compare to 10 expected
Line#4: 1 pipe per line compare to 10 expected
Line#5: 1 pipe per line compare to 10 expected

内容test.txt

cyberciti.biz|74.86.48.99
nixcraft.com|75.126.168.152
theos.in|75.126.168.153
cricketnow.in|75.126.168.154
vivekgite.com|75.126.168.155

日志文件的内容uhs_sm_qa_csv.ksh.log

Line#5 has 1 pipe in total

答案1

虽然我觉得你将一个变量与你想要的东西进行比较很奇怪只是将其设置为,核心问题是您正在使用数字比较运算符(-ne)来表示预期的文件名(文本)。相反,使用:

if [ "$filename" != "$1" ]

...我也在那里引用你的变量

奖励积分为钢铁司机的评论,这促使我进一步研究这一点。

根据我的测试,我相信 ksh 看到了ne 数字$filename比较运算符 and 正在对两个操作数and执行一轮变量和算术扩展$1。因此,$filename变成test.txt,ksh 将其识别为可能的compound variable。由于test未设置,您会收到错误test.txt: no parent

相关内容