Ksh

Ksh腳本沒有父錯誤

  • March 18, 2018

我希望腳本打開一個文件並逐行讀取文件,然後計算每行/行的逗號數。如果這大於 $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

雖然我覺得您將變數與您剛剛設置的變數進行比較很奇怪,但核心問題是您使用數字比較運算符(-ne)來處理預期的文件名(文本)。相反,使用:

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

…我還引用了你的 variables

獎勵指向Steeldriver 的評論,這促使我進一步研究這一點。

根據我的測試,我相信 ksh 看到了ne 數字比較運算符,並且正在對兩個操作數$filename$1. 因此,$filename變成test.txt, ksh 認為這是可能的compound variable。由於test未設置,您會收到錯誤test.txt: no parent

引用自:https://unix.stackexchange.com/questions/430675