0

please advice what is wrong with my code, ( I run this code on both OS linux and solaris )

I don't get the line from:

    "file ended with .tmp"

example:

I need to verify if $FILE ended with .tmp or .old etc ...

code with the following example set the parameter: FILE=hosts.tmp

so I expected that grep will match the ".tmp" and because .tmp is the last characters

  • the same for other TOKENS as ",TEMP" or "previos" or "-OLD" ...etc

code ( ksh script )

     TOKENS=".tmp .old .previous -log temp ,TEMP -OLD previos"

     FILE=hosts.tmp

      for TOKEN in ` echo $TOKENS `

        do

        [[ ` echo $FILE  | grep -c '$TOKEN$'  ` -eq 1 ]] && echo "file ended with $TOKEN"

        done

1 Answer 1

1

Two problems with your code:

1) as the TOKEN gets passed to grep you need to escape the - like this:

TOKENS=".tmp .old .previous \-log temp ,TEMP"

2) you are using single quotes which means variables are not being expanded, replace with quotes:

[[ ` echo $FILE  | grep -c "$TOKEN$"  ` -eq 1 ]] && echo file ended with $TOKEN
4
  • I already set "$TOKEN$" in my code - its also not match the ".tmp"
    – yael
    Jan 9, 2013 at 13:50
  • I just took your code made my modifications and ran it on a Linux box, output was: file ended with .tmp
    – faker
    Jan 9, 2013 at 13:57
  • then you are using different code than the one you posted here. In the one you posted there is no trailing space.
    – faker
    Jan 9, 2013 at 14:03
  • 1
    Use grep -c -- "$TOKEN$" and you won't need to escape the hyphen. Jan 9, 2013 at 21:11

You must log in to answer this question.

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