Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

A nightmare of quoted quotes 1

Status
Not open for further replies.

columb

IS-IT--Management
Feb 5, 2004
1,231
EU
Hi all

I've developped a script for checking that quotes are balanced in other scripts. It looks like
Code:
#!/bin/ksh

[[ $# -ne 1 ]] && { echo invalid param count; exit 1; }

[[ -r $1 ]] || { echo Unable to read $1; exit 1; }

awk -v CH=\' '{
 occ=0;
 while (y=index($0,CH))
 {
  occ++;
  y++;
  $0=substr($0,y);
 }
 if (occ%2)
  print "line " NR ": " occ " occurrences of " CH
}' $1

awk -v CH=\" '{
 occ=0;
 while (y=index($0,CH))
 {
  occ++;
  y++;
  $0=substr($0,y);
 }
 if (occ%2)
  print "line " NR ": " occ " occurrences of " CH
}' $1

awk -v CH=\` '{
 occ=0;
 while (y=index($0,CH))
 {
  occ++;
  y++;
  $0=substr($0,y);
 }
 if (occ%2)
  print "line " NR ": " occ " occurrences of " CH
}' $1
So far so good but, if ever a script screamed for a loop it's this one. I get as far as
Code:
#!/bin/ksh

[[ $# -ne 1 ]] && { echo invalid param count; exit 1; }

[[ -r $1 ]] || { echo Unable to read $1; exit 1; }

for quote in \' \" \`
do
  awk -v CH=
and then it all goes wrong.

Any ideas/pointers would be more than welcome.


On the internet no one knows you're a dog

Columb Healy
 
Ignore my post - it was so simple I overlooked it.

The answer was
Code:
#!/bin/ksh

[[ $# -ne 1 ]] && { echo invalid param count; exit 1; }

[[ -r $1 ]] || { echo Unable to read $1; exit 1; }

for quote in \' \" \`
do
  awk -v CH=$quote '{
   occ=0;
   while (y=index($0,CH))
   {
    occ++;
    y++;
    $0=substr($0,y);
   }
   if (occ%2)
    print "line " NR ": " occ " occurrences of " CH
  }' $1
done

On the internet no one knows you're a dog

Columb Healy
 
Thanks for posting your solution Columb.

I want to be good, is that not enough?
 
Thanks for the star Ken, in case anyone finds it useful here's a version with comments
Code:
#!/bin/ksh

#Script for checking that quotes in other scripts are balanced
#Script to be tested is passed as $1

#Check we have one, and only one passed script
[[ $# -ne 1 ]] && { echo invalid param count; exit 1; }

#Check we can read the script passed as $1
[[ -r $1 ]] || { echo Unable to read $1; exit 1; }

# For each type of quote print lies which have uneven numbers of that type of quote.
for quote in \' \" \`
do
  awk -v CH=$quote '{
   occ=0;
   #While CH found in $0
   while (y=index($0,CH))
   {
    #Increment count of CH and reset $0 to next char
    occ++;
    y++;
    $0=substr($0,y);
   }
   # If count of CH is not divisible by 2
   if (occ%2)
    #print line number and count of CH
    print "line " NR ": " occ " occurrences of " CH
  }' $1
done

On the internet no one knows you're a dog

Columb Healy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top