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
yael
  • 2,433
  • 5
  • 31
  • 43

1 Answers1

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
faker
  • 17,496
  • 2
  • 60
  • 70