0

I want the script to open a file and read the file line by line then count the number of commas per row/line. If that is more than the $2 parameter value, then write the offending line number (from my read loop) and the total commas found to a log file.

I'm not sure what's wrong, but I'm getting a no parent error.

#!/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

Output of 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

Contents of 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

Contents of the log fileuhs_sm_qa_csv.ksh.log:

Line#5 has 1 pipe in total
3
  • 1
    If you're expecting two parameters to the script, you should probably test that "$#" -eq 2 before assigning or using the parameters.
    – Jeff Schaller
    Commented Mar 16, 2018 at 19:58
  • 1
    This looks like a very roundabout way to count / act on the number of separators per line: it would be far simpler IMHO to use awk or perl and use the number of fields minus 1 e.g. awk -F'|' 'NF>n+1 {printf("Line# %d has %d pipe in total\n",NR,NF-1)}' n=2 filename Commented Mar 16, 2018 at 20:04
  • @steeldriver Hmmm how would I implement that into my script? Commented Mar 18, 2018 at 3:37

1 Answer 1

3

While I find it odd that you're comparing a variable to something that you just set it to, the core issue is that you're using a numeric comparison operator (-ne) for what's expected to be a filename (of text). Instead, use:

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

... where I've also quoted your variables.

Bonus points to steeldriver's comment, which prompted me to look a little further into this.

Based on my testing, I believe that ksh sees the ne numeric comparison operator and is performing a round of variable & arithmetic expansion on the two operands $filename and $1. As such, $filename turns into test.txt, which ksh recognizes as a possible compound variable. Since test is unset, you receive the error test.txt: no parent.

1
  • 1
    Presumably the specific error [: test.txt: no parent is because (in the context of the -ne numeric test) test.txt is being interpreted as a ksh93 compound variable ? Commented Mar 17, 2018 at 13:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .