Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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
share|improve this question

1 Answer 1

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
1  
Use grep -c -- "$TOKEN$" and you won't need to escape the hyphen. –  Dennis Williamson Jan 9 '13 at 21:11

Your Answer

 
discard

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.