Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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"


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"


      for TOKEN in ` echo $TOKENS `


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

share|improve this question
up vote 1 down vote accepted

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
share|improve this answer
I already set "$TOKEN$" in my code - its also not match the ".tmp" – yael Jan 9 '13 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 '13 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 '13 at 14:03
Use grep -c -- "$TOKEN$" and you won't need to escape the hyphen. – Dennis Williamson Jan 9 '13 at 21:11

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.